Azure Devops disable concurrent runs on the same pipeline

Viewed 11604

I am have 2 build agents and 8 pipelines in azure devops. If a run starts for pipeline A, and another run is triggered for the same pipeline Azure Devops will start a second run on the other agent without waiting for the first to finish.

How do I make Azure Devops wait until the first run is finished before starting the second one?

Edit: using yaml pipelines rather than old build/release pipelines.

4 Answers

I think you can use adding demands to the pool on the agent job of pipelines, so that it will run with one same agent because of the same specified condition.

First, add one Capability in the agent.

enter image description here

And then, in your YAML, add demands to the pool.

pool:
  name: {agent pool name}
  demands: Limit -equals DisAbleParallel 

The format of it is demands:{CapabilityName} -equals {CapabilityValue}.

While you specified the agent demands, the pipeline will just run with this agent. While A is running, the second one will not run at same time because the previous is running, the agent is being using. So that, the second one will run until previous ends.

The linked-to PowerShell Task works well. I personally set the sleep polling to 60 seconds but other than that this works with one main exception. It won't work if they're queued simultaneously or within a similar starting time. If more than one build is queued before this task completes when no jobs are actually building yet, it will just get stuck until cancelled or times out.

That means if you can wait for a minute or two before building, this will work.

  • I don't recommend it for large teams all working on the same repository.
  • I also don't recommend it for incredibly long builds. I'm not sure, but I think the max timeout for builds is 360 minutes. So if they're long and queued up in numbers you'll risk chain-failures.

https://developercommunity.visualstudio.com/t/prevent-parallel-execution-of-the-same-build-defin/365730#T-N859271

Preventing concurrent runs may be done by setting up an environment with an Exclusive Lock check. That allows working with Microsoft-hosted agents, not just self-hosted agents. It also does not require pipelines to be assigned to specific agents the way Demands do.

  1. In Pipelines/Environments define an environment, say "my_environment". ADO nav bar image

  2. To the environment add a check of type "Exclusive Lock".

  3. In your YAML, reference environment "my_environment" using this form:

    lockBehavior: sequential
    stages:
    - stage: Stage
      jobs:
      - deployment: Job
        environment: my_environment
        strategy:
          runOnce:
            deploy:
              steps:
                ...

This will force multiple queues of the same pipeline to run sequentially in queue order. Different pipelines must use different environments, otherwise they will block each other.

More information here: https://docs.microsoft.com/en-us/azure/devops/release-notes/2021/sprint-190-update#azure-pipelines-1

Related