簡體   English   中英

如何使用 PowerShell 取消和刪除隊列中的等待構建

[英]How can I cancel & delete a waiting build in the queue using PowerShell

由於長時間運行構建,各種下一行構建需要更長的時間來執行。

有沒有一種方法可以取消和刪除隊列中的等待構建,並讓位於使用 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"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM