简体   繁体   English

Azure DevOps 多阶段管道等待批准

[英]Azure DevOps Multi-Stage Pipelines Stuck Waiting for Approvals

I'm using hosted Azure DevOps with our code in Azure Git Repos.我将托管 Azure DevOps 与 Azure Git Repos 中的代码一起使用。 We used to use the "Classic" UI-based pipeline editor, but are moving to YAML templates for our build/release stages.我们曾经使用“经典”基于 UI 的管道编辑器,但现在正在为我们的构建/发布阶段转向 YAML 模板。

In the past I configured CI/CD so that when code is committed to the master branch via a pull request, it would fire off a build and then a Development deployment.过去,我配置了 CI/CD,这样当代码通过拉取请求提交到主分支时,它会触发构建,然后是开发部署。 The other release stages would wait for approval before the code moved to that stage.其他发布阶段将在代码移动到该阶段之前等待批准。 A new release would cancel any previous releases that haven't been deployed to their respective environments.新版本将取消尚未部署到各自环境的任何先前版本。

With YAML deployment stages what I'm finding is that when the master branch triggers a build, it deploys to the Development environment, but the pipeline is stuck in a waiting state because the other stages haven't been approved.在 YAML 部署阶段,我发现当 master 分支触发构建时,它会部署到开发环境,但管道停留在等待状态,因为其他阶段尚未获得批准。 As a result, the run isn't marked as "complete", and eventually the other stages will time out and be marked as failed.因此,运行不会被标记为“完成”,最终其他阶段将超时并被标记为失败。 Additionally, previous runs of the pipeline are not cancelled, so multiple runs are stacked up in a waiting state.此外,管道的先前运行不会被取消,因此多个运行会堆积在等待状态。

Ideally what I'd like to see is that a new build will cancel all previous runs of the pipeline.理想情况下,我希望看到的是新构建将取消管道的所有先前运行。 I'd like to see the run marked as "complete" once it deploys to Development, and be able to deploy to other stages manually after the fact.我希望在部署到 Development 后将运行标记为“完成”,并且能够在事后手动部署到其他阶段。

Has anybody else out there wanted to do the same thing?有没有其他人想做同样的事情? Am I just thinking about this all wrong and should be doing it a different way?我只是想这一切都错了,应该以不同的方式做吗?

Manually deploy to stages is not support in yaml pipeline currently.目前 yaml 管道不支持手动部署到阶段。 Please check this open issue .请检查这个未解决的问题

You can try adding dependsOn and condition for each stage.您可以尝试为每个阶段添加dependsOncondition For below example yaml pipeline.对于下面的示例 yaml 管道。 Stage Build will start to run only after stage Start successfully complete, Then Stage Build will wait for approval, Stage Release willnot be triggered until Stage Build is approved and successfully finished. Stage Build只有在 Stage Start成功完成后才会开始运行,然后 Stage Build 将等待批准,Stage Build 被批准并成功完成后才会触发 Stage Release。

You can define the pr trigger and set autocancel=true (the default is true)to cancel previous runs if new changes were pushed to the same pr.您可以定义pr 触发器并设置autocancel=true (默认值为 true)以在新更改被推送到同一个 pr 时取消之前的运行。

The batch property for trigger can achieve a similar effect. trigger批处理属性可以达到类似的效果。 It will not start a new run if the current pr in still in building.如果当前 pr 仍在构建中,它将不会开始新的运行。

trigger:
  batch: boolean # batch changes if true (the default); start a new build for every push if false
  branches:
    include:

_ _

pr:
  autoCancel: true
  branches:
    include:
    - master

stages:
- stage: Start
  jobs:
    - job: A
      pool:
        vmImage: windows-latest
      steps:
      - powershell: |
          echo "i am job a"

- stage: Build
  dependsOn: Start
  condition: succeeded()
  jobs:
  - deployment: Dev
    displayName: deploy Web App
    pool:
      vmImage: 'Ubuntu-16.04'
  # creates an environment if it doesn't exist
    environment: 'Dev'
    strategy:
    # default deployment strategy, more coming...
      runOnce:
        deploy:
          steps:
          - script: echo "i am dev environment"


- stage: Release
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: Environ
    displayName: deploy Web App
    pool:
      vmImage: 'Ubuntu-16.04'
  # creates an environment if it doesn't exist
    environment: 'Environment'
    strategy:
    # default deployment strategy, more coming...
      runOnce:
        deploy:
          steps:
          - script: echo "i am Environment environment"

Update: Cancel in progress builds via powershell scripts .更新: 通过 powershell 脚本取消正在进行的构建

You can add a powershell task at the top of your pipeline to call build api .您可以在管道顶部添加一个 powershell 任务来调用build api Below scripts get all the in progress builds and cancel them except current build.下面的脚本获取所有正在进行的构建并取消它们(当前构建除外)。

- task: PowerShell@2

      inputs:
        targetType: inline
        script: |

          $header = @{ Authorization = "Bearer $(system.accesstoken)" }
          $buildsUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=5.1"
          echo $buildsUrl
          $builds = Invoke-RestMethod -Uri $buildsUrl -Method Get -Header $header

          $buildsToStop = $builds.value.Where({ ($_.status -eq 'inProgress') -and ($_.definition.name -eq "$(Build.DefinitionName)") -and ($_.id -ne $(Build.BuildId))})

          ForEach($build in $buildsToStop)
          {
            echo $build.id
            $build.status = "cancelling"
            $body = $build | ConvertTo-Json -Depth 10
            $urlToCancel = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$($build.id)?api-version=5.1"
            echo $urlToCancel
            Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header $header
          }

In order your pipeline to have the permission to cancel the current running build.为了您的管道有权取消当前正在运行的构建。 You need go to your pipeline, click on the 3dots and choose Manage security您需要转到您的管道,单击 3 点并选择管理安全性

在此处输入图片说明

Then set the Stop build s permission to Allow for user Project Collection Build Service(projectName),然后为用户Project Collection Build Service(projectName)设置Stop build的权限为 Allow

在此处输入图片说明

I've been searching on this problem for a while and I think there's a better solution.我一直在寻找这个问题一段时间,我认为有一个更好的解决方案。

See my answer to a similar question here: https://stackoverflow.com/a/61400536/275559在此处查看我对类似问题的回答: https : //stackoverflow.com/a/61400536/275559

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 azure devops 仪表板,用于多级管道 - azure devops dashboard for multi-stage pipeline Azure DevOps 多阶段管道。 运行节流阶段的数量 - Azure DevOps Multi-Stage Pipelines. Running Throttled Amount of Stages Azure Devops 中的多阶段、多工件部署触发器 - Multi-stage, multi-artefact deployment triggers in Azure Devops 当“实验主题”和“多阶段管道”处于打开模式时,如何在自定义azure devops插件中添加自定义摘要标签? - How to add custom summary tab in the custom azure devops plugin, When Experimental Theme and Multi-stage Pipelines are in switched-on Mode? 使用多阶段的 Azure DevOps YAML 管道批准 - Azure DevOps YAML Pipeline Approval using Multi-Stage Azure DevOps多阶段流水线部署到特定环境 - Azure DevOps multi-stage pipeline deployment to specific environment Azure DevOps 多阶段 YAML 结合手动交互 - Azure devops multi-stage YAML combined with manual interaction 将映像从 azure devops 多阶段管道发布到 DockerHub - Publish image from azure devops multi-stage pipeline to DockerHub Azure Devops 多阶段流水线环境名称设置不正确 - Azure Devops multi-stage pipeline environment name not set properly 在 Azure DevOps 多阶段管道中部署预先存在的工件 - Deploy a preexisting artifact in a Azure DevOps Multi-Stage pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM