0
votes

I am aware of how to generally queue a build for Azure DevOps using this REST API: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-5.1. I can also see that you can specify a branch in the request body to use for the new build. However, I have yet to find a way to trigger a new build for a specific commit. The Azure Pipelines website has an option to trigger a new build for a specific branch, tag, or commit, so I assume there must be a way to do this via REST API.

Does anyone know?

1

1 Answers

1
votes

You can find a general example here: How to QUEUE a new build using VSTS REST API

Just add sourceVersion to body with commit id. Example for 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)}