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)}