jobs.deployment.strategy.runOnce 定义

runOnce 部署策略通过一次执行其每个步骤来推出更改。

runOnce:
  preDeploy: # Pre deploy hook for runOnce deployment strategy.
    steps: [ task | script | powershell | pwsh | bash | checkout | download | downloadBuild | getPackage | publish | template | reviewApp ] # A list of steps to run.
    pool: string | pool # Pool where pre deploy steps will run.
  deploy: # Deploy hook for runOnce deployment strategy.
    steps: [ task | script | powershell | pwsh | bash | checkout | download | downloadBuild | getPackage | publish | template | reviewApp ] # A list of steps to run.
    pool: string | pool # Pool where deploy steps will run.
  routeTraffic: # Route traffic hook for runOnce deployment strategy.
    steps: [ task | script | powershell | pwsh | bash | checkout | download | downloadBuild | getPackage | publish | template | reviewApp ] # A list of steps to run.
    pool: string | pool # Pool where route traffic steps will run.
  postRouteTraffic: # Post route traffic hook for runOnce deployment strategy.
    steps: [ task | script | powershell | pwsh | bash | checkout | download | downloadBuild | getPackage | publish | template | reviewApp ] # A list of steps to run.
    pool: string | pool # Pool where post route traffic steps will run.
  on: # On success or failure hook for runOnce deployment strategy.
    failure: # Runs on failure of any step.
      steps: [ task | script | powershell | pwsh | bash | checkout | download | downloadBuild | getPackage | publish | template | reviewApp ] # A list of steps to run.
      pool: string | pool # Pool where post on failure steps will run.
    success: # Runs on success of all of the steps.
      steps: [ task | script | powershell | pwsh | bash | checkout | download | downloadBuild | getPackage | publish | template | reviewApp ] # A list of steps to run.
      pool: string | pool # Pool where on success steps will run.

引用此定义的定义: jobs.deployment.strategy

属性

preDeploypreDeployHook
为 runOnce 部署策略预部署挂钩。

deploydeployHook
为 runOnce 部署策略部署挂钩。

routeTrafficrouteTrafficHook
用于 runOnce 部署策略的路由流量挂钩。

postRouteTrafficpostRouteTrafficHook
发布 runOnce 部署策略的路由流量挂钩。

ononSuccessOrFailureHook
runOnce 部署策略的成功或失败挂钩。

备注

runOnce 是最简单的部署策略,其中所有生命周期挂钩(即 preDeploydeployrouteTrafficpostRouteTraffic)都执行一次。 然后,执行 on:successon:failure

生命周期挂钩的说明

preDeploy:用于在应用程序部署开始之前运行初始化资源的步骤。

deploy:用于运行部署应用程序的步骤。 下载工件任务将仅在部署作业的 deploy 挂钩中自动注入。 要停止下载工件,请使用 - download: none,或通过指定下载管道工件任务来选择要下载的特定工件。

routeTraffic:用于运行向更新版本发送流量的步骤。

postRouteTraffic:用于在路由流量后运行步骤。 通常,这些任务按定义的时间间隔监视更新版本的运行状况。

on: failureon: success:用于运行回滚操作或清理的步骤。

示例

以下示例 YAML 代码片段演示了使用部署策略对部署作业的简单用法 runOnce 。 此示例包含签出步骤。


jobs:
  # Track deployments on the environment.
- deployment: DeployWeb
  displayName: deploy Web App
  pool:
    vmImage: ubuntu-latest
  # Creates an environment if it doesn't exist.
  environment: 'smarthotel-dev'
  strategy:
    runOnce:
      deploy:
        steps:
        - checkout: self 
        - script: echo my first deployment

每次运行此作业时,系统会根据 smarthotel-dev 环境记录部署历史记录。

注意

  • 还可以创建具有空资源的环境,并将其用作抽象 shell 来记录部署历史记录,如前面的示例所示。

下一个示例演示管道如何引用要用作部署作业目标的环境和资源。

jobs:
- deployment: DeployWeb
  displayName: deploy Web App
  pool:
    vmImage: ubuntu-latest
  # Records deployment against bookings resource - Kubernetes namespace.
  environment: 'smarthotel-dev.bookings'
  strategy: 
    runOnce:
      deploy:
        steps:
          # No need to explicitly pass the connection details.
        - task: KubernetesManifest@0
          displayName: Deploy to Kubernetes cluster
          inputs:
            action: deploy
            namespace: $(k8sNamespace)
            manifests: |
              $(System.ArtifactsDirectory)/manifests/*
            imagePullSecrets: |
              $(imagePullSecret)
            containers: |
              $(containerRegistry)/$(imageRepository):$(tag)

此方法提供以下优势:

  • 记录环境中特定资源的部署历史记录,而不是记录环境中所有资源的历史记录。
  • 部署作业中的步骤自动继承资源的连接详细信息(在本例中为 Kubernetes 命名空间 smarthotel-dev.bookings),因为部署作业已链接到环境。 在为作业的多个步骤设置相同的连接详细信息的情况下,这很有用。

请参阅