mattb
mattb

Reputation: 482

Azure DevOps YAML Pipelines prevent newer runs if old one queued

I have a pipeline with a number of stages, e.g.:

Canary -> Dev -> UAT -> Prod

For each stage we have approvals set so a team member has to allow a build to proceed to the next stage. Sometimes we have long periods between stages being approved (e.g. someone needs to do some testing and feedback outside of devops we can proceed), therefore it can be hard to remember we have an old run pending approval unless we specifically check - which is prone to human error.

But if I have one build say queued at UAT awaiting push to production how can I prevent a second run which includes the changes from the first run (as they are both running off master) being allowed to proceeded.

E.g. I need newer jobs to be aware of older runs still pending approval to proceed.

Thanks!

Upvotes: 1

Views: 1006

Answers (1)

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31083

There is no default way to prevent newer runs if old one queued, but you can try the following workaround:

You could add a script in the pipeline to check state of the latest pipeline run. If the state is inProgress, cancel the build. If the state is completed, proceed the build.

You should use Runs - List api to get state of the latest pipeline run. The script looks like:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uri = "https://dev.azure.com/$org/$projectName/_apis/pipelines/$pipelineid/runs?api-version=6.0-preview.1"
$result = Invoke-RestMethod -Uri $uri -Method Get -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$latestrun = $result.value.state[0]
Write-Host  "$latestrun"

Then you could refer the following case to cancel the build if $latestrun is inProgress: Is it possible to cancel a Azure DevOps pipeline Job programmatically?

Upvotes: 1

Related Questions