Search code examples
azure-devopsazure-pipelinesazure-pipelines-build-taskazure-pipelines-yamlazure-devops-extensions

Azure devops pipeline: how to make agent finish current build before moving to the next


PR to main trigger a pipeline with 2 jobs, first job build time 18 min. When opening another PR the agent 'jump' to the other build before finishing the second job in the current build.

enter image description here

This is causing a delay.

Any idea how can we change the setting so the agent will do one build at a time?

note:

  • We only have one agent.
  • Yes, we can merge the 2 jobs into one, but the pipeline will be less readable.
  • Same problem when triggering build manually.

Solution

  • Based on your requirement, you want to make agent finish current build before moving to the next.

    I suggest that you can use the Exclusive Lock in YAML Pipeline.

    The exclusive lock check allows only a single run from the pipeline to proceed. All stages in all runs of that pipeline that use the resource are paused. When the stage using the lock completes, then another stage can proceed to use the resource. Also, only one stage will be allowed to continue.

    Here are the steps:

    Step1: Create an Environment in Pipelines -> Environments and add Exclusive Lock. Or you can create Variable Group or other protected resources and add the Exclusive Lock.

    enter image description here

    Step2: Add the protected resources in your YAML Pipeline and set the lockBehavior.

    YAML example:

    stages:
    - stage: A
      lockBehavior: sequential
      jobs:
      - job: Job
        steps:
        - script: xxx
    

    When you set the lockBehavior as sequential, the Pipeline runs will run sequentially.