1
votes

I have a YAML-based, Azure DevOps pipeline. Within that pipeline I have stages each representing a deployment to an environment. This includes provisioning of infrastructure in Azure, deloyment of the app and running automated acceptance tests.

It's possible for engineers to run manual pipelines from multiple branches as well as have the master branch triggered pipeline all running in parallel. This means an environment deployment stage can be running in parallel across multiple running instances of the pipeline.

I'd like to have a stage wait to be the only instance running. What's the best way to solve this with YAML pipelines?

1
What do you mean by "I'd like to have a stage wait to be the only instance running."? If you are looking for the way to force your stages run in paralllel, please check this doc and you will see add the dependsOn: [] property to your stages will cause them run in parallel. Or if i misunderstood your question?Yang Shen - MSFT
Hi Yang. I don't mean within the context of a single pipeline instance, I mean across all instances of a pipeline. I don't want MyPipeline running the stage 'Deploy to QA' more than one at a time. I only ever want one instance of that stage running.Sio
If you mean you want to run the stage 'Deploy to QA' in parallel running pipeline instances by order(one at a time), i don't think this is possible since the stages in these instances have no such direct relationship to force them run by order.Yang Shen - MSFT
Yea, something like that. I was hoping maybe Environments gave some option to synchronise. Classic multi threading problem I guess. We’ll have to talk to each other instead :DSio

1 Answers

2
votes

You can configure an Exclusive Lock for the environment in your pipelines.

  1. Go to Azure DevOps > Your organization> Your project > Pipelines > Environments
  2. Select an environment (or create one) where you want to synchronize deployments
  3. On the top right corner there is a dropdown menu, go to "Approvals and checks"
  4. Again in the top right corner there is a big plus button, click this and select "Exclusive lock"

This ensures that only one pipeline instance can use the stage where this environment is used in azure-pipelines.yaml at any given time.

In azure-pipelines.yaml you should refer to that environment by name using the environment field in the deployment job.

Here is an example of deployment for the environment named staging. The same name should be used in the steps above when you create an environment in the portal.

stages:
  - stage: Deploy
    jobs:
      - deployment: DeployStaging
        environment: staging
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
                - script: deploy-script.sh

Some additional resources: