1

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.

  • Hi friend, is there any update about this ticket? Feel free to let me know if the answer could give you some help. Just a remind of [this](https://stackoverflow.com/help/someone-answers) . – Walter Oct 12 '20 at 09:29

1 Answers1

0

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.

Walter
  • 2,640
  • 1
  • 5
  • 11
  • Using the rest api, I will get the latest build id. But, in cases such as we have two build queued, one from the trigger build task and one manually. Then the rest api I use will return the one that is manually queued right, where as I will need the one which the trigger build task have queued. So, is there any way that the trigger build task itself can return the id or url? – Tejaswini N Oct 02 '20 at 07:53
  • I looked into the task as you mentioned. Will try the Store trigger build id and check once. – Tejaswini N Oct 02 '20 at 07:58
  • @Tejaswini N Sorry I didn't notice Store triggered build id's in a variable option in this task. You just need to enable this option and use $(TriggeredBuildIds) variable. – Walter Oct 02 '20 at 08:14