0

I am aware of how to generally queue a build for Azure DevOps using this REST API: https://learn.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?

Daniel
  • 126
  • 1
  • 8

2 Answers2

2

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://learn.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)}
Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
0

To do the same using cURL and a more recent API version (6.0, but seems to work at least up to API version 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"
  }'

Don't know what your PIPELINE_ID is? Go to the Azure Pipeline website, click on your pipeline and look at the URL: https://dev.azure.com/yourorganization/yourproject/_build?definitionId=42 -> the definitionId is the one you want.

(Note: there is also a sourceBranch parameter but I did not manage to get it working, Azure keeps ignoring it and using the default branch...)

cmousset
  • 625
  • 7
  • 21