【问题标题】:Skip stage with manual approval in Azure DevOps Pipelines (YAML)在 Azure DevOps Pipelines (YAML) 中通过手动批准跳过阶段
【发布时间】:2020-08-13 15:46:34
【问题描述】:

对于我们的 Terraform 部署,我们使用具有 3 个阶段的 Azure DevOps 管道:

  1. 计划
  2. 申请(人工审批)
  3. 测试

对于应用阶段,我们使用具有手动批准(检查)环境的部署作业。如果计划阶段没有显示任何变化,我们想要的是“跳过”应用和测试阶段。因此,我们尝试在应用阶段使用以下 yaml 配置:

  - stage: ApplyShared
    dependsOn: PlanShared
    jobs:
      - job: CheckSharedChanges
        steps:
          - task: DownloadPipelineArtifact@2
            inputs:
              artifactName: TerraformBuild
              downloadPath: $(System.DefaultWorkingDirectory)
          - bash: |
              # using a file for indicating changes in TF plan, since
              # you cannot pass variables between stages in Azure DevOps
              if [ -f ".shared-changes" ]; then
                  echo '##vso[task.setvariable variable=shared_changes]yes'
              fi
            name: Check
      - deployment: ApplyShared
        dependsOn: CheckSharedChanges
        # this condition seems to be ignored, if there is a manual
        # approval on the stage
        condition: eq(dependencies.CheckSharedChanges.outputs['Check.shared_env'], 'yes')
        displayName: 'Apply - shared'
        # we configured a manual approval (check) for this environment,
        # so the pipeline stops and asks for an operator to approve the deployment
        environment: 'infra-shared'

根据这个issue on the MS Developer Community,在审批前不检查已审批阶段的条件,所以该方法行不通。

我的问题是:你知道有什么其他方法可以实现吗?

编辑

现在有一个解决此问题的 hacky 解决方法,请参阅 this SO post

【问题讨论】:

    标签: azure-devops yaml terraform azure-pipelines


    【解决方案1】:

    一个阶段可以包含许多作业,每个作业可以消耗多个资源。在开始执行阶段之前,必须满足对该阶段中使用的所有资源的所有检查。 Azure Pipelines 在每个阶段之前暂停管道的执行,并等待所有挂起的检查完成。这就是为什么该条件在您的场景中不起作用的原因。在此处查看更多信息:

    https://docs.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass

    路线图上已经有类似的想法了,你可以跟踪以下链接:

    https://developercommunity.visualstudio.com/idea/697467/manually-triggered-stages-in-yaml-multi-stage-pipe.html

    目前,您可以考虑启动 a manual run 并跳过管道中的几个阶段:

    【讨论】:

    • 感谢您的回复! “在开始执行一个阶段之前,必须满足对该阶段使用的所有资源的所有检查。” - 如果您将评估条件纳入其中,我认为我们会很好,因为如果无法满足条件,则可以跳过该阶段:)我在这里补充说developercommunity.visualstudio.com/comments/1010696/view.html
    【解决方案2】:

    现在可用:https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/manual-validation?view=azure-devops&tabs=yaml

    - task: ManualValidation@0
      timeoutInMinutes: 1440 # task times out in 1 day
      inputs:
        notifyUsers: |
          test@test.com
          example@example.com
        instructions: 'Please validate the build configuration and resume'
        onTimeout: 'resume'
    

    onTimeout 也可以设置为reject。如果这被插入,阶段/作业将处于挂起状态,直到有人进入并查看指令在屏幕上的位置。将非常接近为手动干预任务提供的课程版本。此任务仅在 YAML 管道中可用。

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2020-10-22
      • 1970-01-01
      • 2021-03-18
      • 2020-01-13
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2021-03-20
      相关资源
      最近更新 更多