Azure DevOps 多阶段管道卡在等待批准 [英] Azure DevOps Multi-Stage Pipelines Stuck Waiting for Approvals

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

问题描述

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

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

对于 YAML 部署阶段,我发现当主分支触发构建时,它会部署到开发环境,但由于其他阶段尚未获得批准,因此管道处于等待状态.结果,运行没有被标记为完成",最终其他阶段将超时并被标记为失败.此外,管道之前的运行不会被取消,因此多次运行会处于等待状态.

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

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

解决方案

目前 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天全站免登陆