Azure DevOps多阶段流水线等待批准

时间:2020-01-30 00:40:43

标签: azure-devops yaml

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

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

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

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

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

2 个答案:

答案 0 :(得分:1)

yaml管道当前不支持手动部署到阶段。请检查此open issue

您可以尝试为每个阶段添加dependsOncondition。对于以下示例yaml管道。只有在成功完成阶段开始之后,阶段构建才开始运行,然后阶段构建将等待批准,直到阶段构建被批准并成功完成后,才会触发阶段发布。 / p>

您可以定义pr trigger并设置autocancel=true(默认值为true),以在新更改被推送到同一程序时取消以前的运行。

trigger的{​​{3}}属性可以达到类似的效果。如果当前的公关仍在建设中,它将不会开始新的运行。

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"

更新:batch

您可以在管道顶部添加一个powershell任务,以调用Cancel in progress builds via powershell scripts。下面的脚本获取所有正在进行的构建,并取消它们(当前构建除外)。

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

为了使您的管道有权取消当前正在运行的构建。您需要进入管道,单击3dots并选择“管理安全性”

build api

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

enter image description here

答案 1 :(得分:0)

我一直在研究这个问题一段时间,我认为有更好的解决方案。

在这里查看我对类似问题的回答:https://stackoverflow.com/a/61400536/275559