Making an Azure Pipeline Stage Non-Cancellable
Introduction
In a recent StackOverflow post, I sought advice on making a specific stage in an Azure Pipeline non-cancellable. This blog explores the question and summarizes the solutions proposed on the post as well as my co-worker Dan.
The Challenge
Developers often face the challenge of ensuring the reliability of each stage in an Azure Pipeline. My specific concern was making a stage non-cancellable, meaning it should not be interrupted once started.
Understanding Azure Pipelines
Azure Pipelines involve stages, each comprising one or more jobs. Jobs represent phases with multiple tasks. My focus was on enforcing a non-cancellable behavior for a particular stage.
Proposed Solution
Custom Conditions and PowerShell Scripts
Use always() to control stage behavior. A condition that always evaluates to true ensures a stage runs, making it non-cancellable.
stages:
- stage: CustomConditionStage
jobs:
- job: CustomConditionJob
condition: always()
steps:
- powershell: |
Write-Host "Running CustomConditionStage"
Conclusion
Making an Azure Pipeline stage non-cancellable is important in cases where we share use shared resources. Using Pulumi or Terraform state file backend storage for example or running tests on real cloud resources. In such cases we need to ensure such stages complete successfully and finish their cleanup before next one starts. In such cases the always()
function and exclusive locks are really handy.