Prevent schedule from triggering when I have ongoing or failed builds in azure devops

Viewed 178

In azure devops and yaml I can set a schedule trigger like this:

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - main

And it will now trigger every night if main have new code since the last successful build.

But my problem is that I have builds that are long running with multiple stages (acc, int, prod) that we manually approve on different days.

So how do I prevent it from queueing new builds (with the same code) if it already have a build with x commit? Even it it's ongoing or in failed state. I can't seem to find anything in the documentation.

1 Answers

You can't accomplish this out of the box. However, you can write the logic into your pipeline.

To accomplish this, you would need to add a task into your pipeline that can:

  1. Query for the last pipeline run
  2. Evaluate if the current pipeline should proceed based on the last run's data

The step would look something like this:

            - powershell: |
                $header = @{ Authorization = "Bearer $env:System_AccessToken" }
                $buildsUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/builds/builds"
                $builds = Invoke-RestMethod -Uri $buildsUrl -Method Get -Header $header
                $inProgressBuilds = $builds.value.Where({ ($.status -eq 'inProgress') -and ($_.definition.name -eq '$(Build.DefinitionName)') -and ($_.id -ne $(Build.BuildId)) })
                if ( $inProgressBuilds.Count -gt 0 ) {
                    throw 'Pipeline run already in progress.'
                }
              displayName: "Validate Current Pipeline Runs"

Related