在 Azure DevOps Pipelines (YAML) 中通过手动批准跳过阶段 [英] Skip stage with manual approval in Azure DevOps Pipelines (YAML)

查看:34
本文介绍了在 Azure DevOps Pipelines (YAML) 中通过手动批准跳过阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

 - 阶段:ApplyShared依赖:PlanShared工作:- 工作:CheckSharedChanges脚步:- 任务:DownloadPipelineArtifact@2输入:工件名称:TerraformBuild下载路径:$(System.DefaultWorkingDirectory)- 重击:|# 使用文件来指示 TF 计划的变化,因为# 你不能在 Azure DevOps 的阶段之间传递变量if [ -f ".shared-changes";];然后echo '##vso[task.setvariable variable=shared_changes]yes'菲名称:检查- 部署:ApplyShared依赖:CheckSharedChanges# 这个条件好像被忽略了,如果有说明书的话# 在舞台上批准条件:eq(dependencies.CheckSharedChanges.outputs['Check.shared_env'], 'yes')displayName: '应用 - 共享'# 我们为此环境配置了手动批准(检查),# 所以管道停止并要求操作员批准部署环境:'基础设施共享'

据此

For our Terraform Deployment, we use an Azure DevOps pipeline that has 3 stages:

  1. plan
  2. apply (manual approval)
  3. test

For the apply stage we use a deployment job with an environment that has a manual approval (check). What we would like to have is "skipping" the apply and test stage, if the plan stage has shows no changes. Therefore we try to use the following yaml configuration for the apply stage:

  - 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'

According to this issue on the MS Developer Community, a condition on a stage with an approval is not checked before the approval, so the approach does not work.

My question is: do you know any other way to implement this?

Edit

There now exists a hacky workaround for this issue, see this SO post

解决方案

A stage can consist of many jobs, and each job can consume several resources. Before the execution of a stage can begin, all checks on all the resources used in that stage must be satisfied. Azure Pipelines pauses the execution of a pipeline prior to each stage, and waits for all pending checks to be completed. That's why the condition doesn't work in your scenario. Check more information here:

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

There is already a similar idea on roadmap, you may track the following link:

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

Currently, you may consider starting a manual run and skip a few stages in your pipeline:

这篇关于在 Azure DevOps Pipelines (YAML) 中通过手动批准跳过阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆