通过


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

性能

preDeploy preDeployHook
RunOnce 部署策略的预部署挂钩。

deploy deployHook
runOnce 部署策略的部署挂钩。

routeTraffic routeTrafficHook
runOnce 部署策略的路由流量挂钩。

postRouteTraffic postRouteTrafficHook
RunOnce 部署策略的 Post 路由流量挂钩。

on onSuccessOrFailureHook
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),因为部署作业链接到环境。 在为作业的多个步骤设置相同的连接详细信息的情况下,这非常有用。

另请参阅