powershell - 如何使用 PowerShell 取消和删除队列中等待的构建

标签 powershell tfs build azure-pipelines azure-devops-rest-api

由于长时间运行的构建,各种下一个在线构建需要更长的时间来执行。

有没有办法可以取消和删除队列中等待的构建,并为使用 PowerShell 或 REST API 的最新触发的构建让路?

最佳答案

以下代码片段遍历所有 TFS 构建,获取正在进行中且未启动的构建,然后取消它们。

$tfsUrl = "http://{server}:{port}/{organization}/{collection}/{project}" # TFS Base URL
$BuildDefsUrl = "$tfsUrl/_apis/build/definitions?api-version=2.0" # TFS build definitions URL
$BuildsUrl = "$tfsUrl/_apis/build/builds"  #TFS Builds URL

$Builds = (Invoke-RestMethod -Uri ($BuildDefsUrl) -Method GET -UseDefaultCredentials).value | Select id,name # get all builds 
#for filtering use : |  Where-Object {$_.name -like "*Your Pattern*"}

foreach($Build in $Builds)
{
    $command = "$($BuildsUrl)?api-version=3.2-preview.3&resultFilter=inprogress&definitions=$($Build.id)&queryOrder=finishTimeDescending"
 
    $Ids = (((Invoke-RestMethod -Method Get -Uri $command -UseDefaultCredentials).value) | where status -like "*notStarted*").id # get waiting builds id's

    foreach($id in $Ids)
    {
            $uri =  "$($BuildsUrl)/$($id)?api-version=2.0" # TFS URI
            $body = '{"status":4}' # body 
            $result = Invoke-RestMethod -Method Patch -Uri $uri -UseDefaultCredentials -ContentType 'application/json' -Body $body -Verbose #cancel  build
    }
} 

上面的例子已经很老了。下面的代码片段适用于 Azure DevOps-

$PATToken = "PAT_GOES_HERE"
$AuthHeader= @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PATToken)")) }

$azureDevops = "https://dev.azure.com/{organization}/{project}"
$BuildsUrl = "$azureDevops/_apis/build/builds" 

$filterBuilds = "$($BuildsUrl)?statusFilter=notStarted&api-version=6.0"
    
(Invoke-RestMethod -Method Get -Uri $filterBuilds -Headers $AuthHeader).value | % {
  $uri =  "$($BuildsUrl)/$($_.id)?api-version=6.0" # Azure Devops URI
  $body = '{"status":4}' # body 
  $result = Invoke-RestMethod -Method Patch -Uri $uri -Headers $AuthHeader -ContentType 'application/json' -Body $body -Verbose #cancel  build
  Write-Output "$($_.definition.name) cancaled"
}

关于powershell - 如何使用 PowerShell 取消和删除队列中等待的构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583196/

相关文章:

excel - 乘法表Powershell

powershell - 使用 Get-ChildItem Powershell cmdlet 的 -Depth 属性

visual-studio-2013 - Visual Studio 2013 和 TFS Build 2015 : Devenv. exe 无法生成 MSI 文件

eclipse - PDE Build 无法解析它所依赖的插件中的类

delphi - SerAndDeser.pas(298) : W1036 Variable 'tForm' might not have been initialized

apache - Powershell 不退出

powershell - 设置-Acl : Requested registry access is not allowed

TFS 2010 问题与跟踪生成中的变更集是门控 checkin 的结果

tfs - 使用 git tf 或 git tfs 恢复丢失的 tfs 变更集

Gradle:从 .class 文件创建 .jar 并在构建 EAR 时将其包含在 EAR_dir/lib 中