How to get the build id from the trigger build task provided by the Azure devops

2.7k views Asked by At

I use the task that is provided by the azure devops custom pipelines to trigger another build by passing the pipeline ID and other few trigger conditions. Below is the task that I use for your reference.

When I use this task, it is triggering a build in the pipeline I mention and gives the link for the triggered build in the logs. But I need the id or the link for that triggered build as an output so that I can use it later to check the status or etc. Would you please help on how to get this.

1

There are 1 answers

3
Walter On

You can use get latest build REST API in a powershell task to get the build id. Please add the following scripts in your powershell task:

$organization = "{Organization name}" 
$project = "{Project name}" 
$definitionid = "{definitionid}"
$pat = "{PAT}"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$baseUrl = "https://dev.azure.com/$organization/$project/_apis/build/latest/$definitionid?api-version=5.1-preview.1" 
$latestbuild = Invoke-RestMethod -Uri $baseUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Method GET 

Write-Host $latestbuild.id
$id1 = $latestbuild.id
Write-Host "##vso[task.setvariable variable=buildid]$id1"

You can use the variable buildid in subsequent tasks. For example: Write-Host $(buildid)

Update: You can enable the "Store triggered build id's in a variable" option in the Advanced Configuration of the trigger build task:

enter image description here

Then, you can use $(TriggeredBuildIds) in subsequent tasks.