Search code examples
continuous-integrationazure-pipelinesazure-pipelines-yamlpulumipulumi-azure

Make azure pipeline stage noncancellable


Is there a way to make an azure pipeline stage or job noncancellable?

Description:

We have a pipeline in the lines of:

stages:
- stage: BuildAndUnitTest
  .........
  .........
- stage: DeployAndTest // deploys to a shared resource and runs tests against it
  lockBehavior: sequential
  jobs:
  - job: DeployInfrastructureUsingPulumi
  .........
  .........
  - job: RunTestsAndCleanUp
  .........
  .........
  - job: DestroyInfrastructureUsingPulumi
  .........
  .........

In the pipeline note that DeployAndTest stage makes changes to a shared resource. It deploys cloud infrastructure, runs some tests then destroy the infrastructure.

The next run should take place only after the clean and destroy are finished.

  • Is there a way to ensure once the pipeline enters the DeployAndTest stage it cannot be cancelled manually or due to new push to branch when it runs as part of a pull request build?
  • Or is there a better technique to achieve this kind of pipeline run?

Solution

  • You can add the condition "always()" to the stages and jobs that you want to be noncancellable.

    enter image description here

    For example:

    stages:
    - stage: A
      displayName: 'Stage A'
      jobs:
      - job: A1
        displayName: 'Job A1'
        . . .
    
      - job: A2
        displayName: 'Job A2'
        dependsOn: A1
        condition: always()
        . . .
    
    - stage: B
      displayName: 'Stage B'
      dependsOn: A
      condition: always()
      jobs:
      - job: B1
        displayName: 'Job B1'
        . . .
    
      - job: B2
        displayName: 'Job B2'
        dependsOn: B1
        . . .
    
    • When Stage A is running, if cancel it, Job A2 and Stage B will still run.
    • When pipeline is running, if cancel the whole pipeline run, Job A2 and Stage B will still run.