当我在 azure devops 中进行构建或构建失败时,防止触发计划

Prevent schedule from triggering when I have ongoing or failed builds in azure devops

在 azure devops 和 yaml 中,我可以像这样设置一个调度触发器:

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - main

如果 main 自上次成功构建以来有新代码,现在每晚都会触发。

但我的问题是我的构建很长 运行 多个阶段(acc、int、prod),我们在不同的日子手动批准。

那么,如果它已经有一个带有 x 提交的构建,我该如何防止它对新构建(使用相同的代码)进行排队?即使它正在进行或处于失败状态。我似乎在文档中找不到任何内容。

您无法开箱即用。但是,您可以将逻辑写入管道。

为此,您需要在管道中添加一个任务,该任务可以:

  1. 查询最后一个管道运行
  2. 根据最后一个运行的数据
  3. 评估当前管道是否应该继续

该步骤看起来像这样:

            - powershell: |
                $header = @{ Authorization = "Bearer $env:System_AccessToken" }
                $buildsUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/builds/builds"
                $builds = Invoke-RestMethod -Uri $buildsUrl -Method Get -Header $header
                $inProgressBuilds = $builds.value.Where({ ($.status -eq 'inProgress') -and ($_.definition.name -eq '$(Build.DefinitionName)') -and ($_.id -ne $(Build.BuildId)) })
                if ( $inProgressBuilds.Count -gt 0 ) {
                    throw 'Pipeline run already in progress.'
                }
              displayName: "Validate Current Pipeline Runs"