Azure DevOps管道:取消队列中的多个待处理作业

Pet*_*ger 5 azure-devops

在 Azure DevOps 管道中,如何取消作业池的所有待处理作业。我有很多工作在排队,但看不到在哪里可以取消我正在等待的所有工作。

Lev*_*SFT 6

Azure Devops 尚不具备从 UI 部分批量取消所有待处理作业的功能。

您可以编写脚本来调用rest api来取消所有待处理的作业作为解决方法。查看以下步骤:

首先,使用list build rest api获取所有待处理的作业。

https://dev.azure.com/{organization}/{project}/_apis/build/builds?statusFilter=notStarted&api-version=5.1

然后,使用update build api取消待处理的作业:

PATCH https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1

请参阅以下 powershell 脚本以供参考:

检查此处以获取将在以下脚本中使用的个人访问令牌。

$url= "https://dev.azure.com/{organization}/{project}/_apis/build/builds?statusFilter=notStarted&api-version=5.1"

$pat="Personal Access Token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))

$pendingJobs=Invoke-RestMethod -Uri $url-Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method get -ContentType "application/json" 

$jobsToCancel = $pendingJobs.value

#Pending jobs donot consume the job agents in the agent pool. To filter the definition name to cancel pending jobs for a particular pipeline, you can use below filter criteria. 
#$jobsToCancel = $pendingJobs.value | where {$_.definition.Name -eq "{Name of your pipeline }"}

#call update api to cancel each job.
ForEach($build in $jobsToCancel)
{
   $build.status = "Cancelling"
   $body = $build | ConvertTo-Json -Depth 10
   $urlToCancel = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$($build.id)?api-version=5.1"
   Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
}
Run Code Online (Sandbox Code Playgroud)

您还可以向 Microsoft 开发团队提交新功能请求(单击Suggest a feature并选择azure devops)以支持批量取消待处理作业。希望他们会考虑在未来的冲刺中添加此功能。