Reputation: 3752
I know it is unconventional to trigger build pipeline from release pipeline, but in my case that is my need. In Azure DevOps I have a build pipeline that triggers release pipeline, then once all of the stages are done in my release, I would like to trigger another build pipeline. That build pipeline will be responsible to update a json file in my git repo. To be able to access the git repo and make changes/commit, I need to use a build pipeline. I have a workaround that clones the git repo in release pipeline and does the update, but that doesn't "feel right". I am hoping to find a better solution.
Note: In git commit message I will have [skip ci]
, so there won't be an infinite loop of triggers or pipelines...
Upvotes: 2
Views: 4887
Reputation: 12331
The easiest would be to try out any of these 3rd party tasks which lets you call a build pipeline from your release. But I cannot vouch for any.
If you have trust issues like me :) you could use the DevOps REST.API to trigger your build pipeline.
Basically, you need to make a POST request to trigger a pipeline:
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=6.0
For that, you could use a PowerShell task added as the last step in your release pipeline.
Also, remember to set up the OAuth token access permissions under "Run on agent" task's "Additional options".
Also, the build pipeline you are trying to trigger should have given the "Queue builds" permission for your build service.
Here's an similar script which you should be able to reuse:
$alias = "$(Release.TriggeringArtifact.Alias)"
$buildIdKey = "RELEASE_ARTIFACTS_" + $alias + "_BUILDID"
$buildId = (Get-item env:$buildIdKey).Value
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds?api-version=6.1-preview.6"
Write-Host "URL: $url"
$authHeader = "Bearer $(System.AccessToken)"
$requestHeader = @{
"Authorization" = $authHeader
"Accept" = "application/json"
}
$requestBody = '{
"definition": {
"id": ' + $buildId + '
}
}'
$pipeline = Invoke-RestMethod -Method 'Post' -Uri $url -ContentType "application/json" -Headers $requestHeader -Body $requestBody
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
Upvotes: 7