[英]How to queue a new build via Azure DevOps REST API using a specific commit?
我知道如何使用此 Azure DevOps 通常將構建排隊 REST API: https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure- devops-rest-5.1 。 我還看到您可以在請求正文中指定一個分支以用於新構建。 但是,我還沒有找到一種方法來為特定提交觸發新構建。 Azure Pipelines 網站有一個選項可以為特定的分支、標記或提交觸發新構建,所以我假設必須有一種方法可以通過 REST API 來執行此操作。
有人知道嗎?
您可以在此處找到一個通用示例: How to QUEUE a new build using VSTS REST API
只需使用commit id
將sourceVersion
添加到 body 。 PowerShell 示例:
$org = "<ORG_NAME>"
$teamProject = "<TEAM_PROJECT_NAME>"
$user = ""
$token = "<PAT>" #https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
$buildId = "BUILD_DEFINITION_ID"
$commitId = "COMMIT_ID"
$queueBuild = "https://dev.azure.com/$org/$teamProject/_apis/build/builds?api-version=5.1"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
Write-Host $queueBuild
$body = '
{
"definition": {
"id": "buildId"
},
"sourceVersion" : "commitId"
}
'
$body = $body -replace "buildId", $buildId
$body = $body -replace "commitId", $commitId
$bodyJson=$body | ConvertFrom-Json
Write-Output $bodyJson
$bodyString=$bodyJson | ConvertTo-Json -Depth 100
Write-Output $bodyString
$result = Invoke-RestMethod -Uri $queueBuild -Method POST -ContentType "application/json" -Body $bodyString -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
使用 cURL 和更新的 API 版本(6.0,但似乎至少可以工作到 API 版本 7.1-preview.7):
YOUR_PAT_TOKEN_ENCODED_IN_BASE64=... // https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=Windows#create-a-pat
AZURE_ORGANIZATION=...
AZURE_PROJECT=...
PIPELINE_ID=...
FULL_GIT_SHA=...
curl --location \
--request POST 'https://dev.azure.com/$AZURE_ORGANIZATION/$AZURE_PROJECT/_apis/build/builds?api-version=6.0' \
--header 'Authorization: Basic $YOUR_PAT_TOKEN_ENCODED_IN_BASE64' \
--header 'Content-Type: application/json' \
--data-raw '{
"definition": {
"id": "$PIPELINE_ID"
},
"sourceVersion" : "$FULL_GIT_SHA"
}'
不知道您的 PIPELINE_ID 是什么? Go 到 Azure 管道網站,點擊你的管道並查看 URL: https://dev.azure.com/yourorganization/yourproject/_build?definitionId=42 -> definitionId
是你想要的。
(注意:還有一個sourceBranch
參數,但我沒有設法讓它工作,Azure 一直忽略它並使用默認分支......)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.