Azure DevOps多阶段流水线等待批准 [英] Azure DevOps Multi-Stage Pipelines Stuck Waiting for Approvals

查看:46
本文介绍了Azure DevOps多阶段流水线等待批准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将托管的Azure DevOps与我们在Azure Git Repos中的代码一起使用.我们曾经使用基于UI的经典"管道编辑器,但在我们的构建/发布阶段将使用YAML模板.

过去,我配置了CI/CD,以便当通过拉取请求将代码提交到master分支时,它将触发构建,然后进行Development部署.其他发布阶段将在代码移至该阶段之前等待批准.新发行版将取消尚未部署到各自环境的所有先前发行版.

在YAML部署阶段中,我发现,当master分支触发构建时,它会部署到Development环境中,但是由于其他阶段尚未获得批准,因此管道处于等待状态.结果,运行未标记为完成",最终其他阶段将超时并标记为失败.此外,管道的先前运行不会被取消,因此多个管道会在等待状态下堆叠起来.

理想情况下,我希望看到的是新版本将取消管道的所有先前运行.我希望将运行部署到开发后,将其标记为完成",并能够在事后手动将其部署到其他阶段.

还有其他人想做同样的事情吗?我是不是只是在想这一切错,应该以不同的方式来做?

解决方案

当前在yaml管道中不支持手动部署到阶段.请检查

然后将停止构建权限设置为允许用户 Project Collection Build Service(projectName)

I'm using hosted Azure DevOps with our code in Azure Git Repos. We used to use the "Classic" UI-based pipeline editor, but are moving to YAML templates for our build/release stages.

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

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. Please check this open issue.

You can try adding dependsOn and condition for each stage. For below example yaml pipeline. 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.

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.

The batch property for trigger can achieve a similar effect. It will not start a new run if the current pr in still in building.

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.

You can add a powershell task at the top of your pipeline to call 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

Then set the Stop builds permission to Allow for user Project Collection Build Service(projectName),

这篇关于Azure DevOps多阶段流水线等待批准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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