本文介绍了当一个新的构建排队时,如何取消所有先前的构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Azure DevOps,如何在启动新版本时取消当前版本?

With Azure DevOps how can I cancel the current build when a new one is started?

我想节省构建时间.有时,在我的master分支上进行合并,然后由另一个开发人员进行一次合并,然后由另一个开发人员再次进行合并.我不需要逐个构建.我只能保留最新版本.因此,当一个新的构建排队时,这个可以取消所有以前的构建.

I want to save my build time. On my master branch sometime I do a merge then another developer do one and another do one again. I don't need to do build by build. I can keep only the latest build. So when a new build is queued this one can cancel all previous build.

是否可以设置他的构建定义?

Is it possible to setup his build definition to do so?

推荐答案

Azure DevOps中没有此类功能.

There is no such feature in Azure DevOps.

最接近的使用批处理CI构建"-运行构建时,系统等待构建完成,然后将尚未构建的所有更改的另一个构建排队.

The closest thing it's to use "Batching CI builds" - when a build is running, the system wait until the build is completed, then queues another build of all changes that have not yet been built.

在yaml构建中启用它,然后将其添加到trigger部分:

Yo enable it in yaml build add this in the trigger section:

batch: true

在calssic编辑器中,转到触发器"选项卡,然后选中正在进行构建时批处理更改"复选框.

In the calssic editor, go to "Triggers" tab and mark the checkbox "Batch changes while a build is in progress".

您可以在构建开始时运行PowerShell脚本,以从相同的定义中取消正在运行的构建:

You can run a PowerShell script in the beginning of the build that cancel the running builds from the same definition:

$header = @{ Authorization = "Bearer $env:System_AccessToken" }
$buildsUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/builds/builds"
$builds = Invoke-RestMethod -Uri $url -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)
{
   $build.status = "Cancelling"
   $body = $build | ConvertTo-Json -Depth 10
   $urlToCancel = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(builds.id)"
   Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header $header
}

我使用OAuth令牌进行授权(在工作选项上启用它),并使用内联脚本($(varName)而不是$env:varName).

I used OAuth token for authorization (enable it on the job options) and in inline script ($(varName) and not $env:varName).

现在,如果您有一个正在运行的构建,而其他人触发了另一个开始运行的构建,则在此步骤中,第一个构建将被取消.

Now, if you have one build that running and someone else trigger another build that started to run, in this step the first build will be canceled.

这篇关于当一个新的构建排队时,如何取消所有先前的构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:46