How to Use Conditions and Expressions in Azure Pipelines
The condition key gates a stage, job, or step on an expression built from functions like succeeded(), eq(), and and().
Attach condition: with an expression. Expressions combine status functions (succeeded(), failed()) with comparisons over variables and branch refs.
Run a step only on main after success
Combine the implicit success check with a branch comparison explicitly, since a custom condition replaces the default.
azure-pipelines.yml
steps:
- script: ./deploy.sh
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
- script: ./notify-failure.sh
condition: failed()Gotchas
- Setting a custom
conditionremoves the implicitsucceeded()- add it back or the step runs even after failures. - Runtime conditions use
variables['Name']; compile-time template logic uses${{ if }}instead. - Use
always()for cleanup steps andsucceededOrFailed()to run despite earlier failures.
Related guides
How to Use Trigger and Branch Filters in Azure PipelinesControl which pushes and pull requests run an Azure Pipeline with trigger and pr branch include/exclude filte…
How to Create a Multi-Stage Pipeline in Azure PipelinesBuild a build-test-deploy multi-stage pipeline in Azure Pipelines with dependsOn chaining and condition so de…