0

I am trying to run a pipeline based on build completion trigger. There are 4 build completion triggers enabled. Hence, the pipeline runs 4 times.

I have enabled "Batch changes while a build is in progress".

How do I make it run a single time once all build completion is complete?

user3441903
  • 77
  • 10
  • @LeoLiu-MSFT I created single pipeline combining the 4, so that only single run is done when build completion is complete – user3441903 Feb 22 '21 at 10:51

1 Answers1

0

Batch changes while a build is in progress only for CI builds. You can not apply it to build completion triggers. However, you can use PowerShell to run another pipeline: How to trigger a build from another build pipeline in azure devops

Just check active pipelines to skip many triggers. Here is example:

$user = ""
$token = $env:SYSTEM_ACCESSTOKEN

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$orgUrl = "$env:SYSTEM_COLLECTIONURI"
$teamProject = "$env:SYSTEM_TEAMPROJECT"
$currentBuildDefId = "$env:SYSTEM_DEFINITIONID"
$buildBodyTemplate = "{`"definition`": {`"id`": <build_id>}}"

$restApiQueueBuild = "$orgUrl/$teamProject/_apis/build/builds?api-version=6.0"
$restApiGetBuilds = "$orgUrl/$teamProject/_apis/build/builds?definitions=$currentBuildDefId&statusFilter=inProgress,notStarted&api-version=6.0"

function InvokeGetRequest ($GetUrl)
{   
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
}

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

function RunBuild($buildId)
{
    $buildBody = $buildBodyTemplate.Replace("<build_id>", $buildId)            
    Write-Host $buildBody
    
    $buildresponse = InvokePostRequest $restApiQueueBuild $buildBody
    Write-Host $buildresponse
}

$resBuild = InvokeGetRequest $restApiGetBuilds

if ($resBuild.count -gt 1)
{
    Write-Host $resBuild.count " builds in progress, skip the second build"
    return
}

RunBuild SECOND_BUILD_DEF_ID
Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31