Skip to content
Latchkey

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 condition removes the implicit succeeded() - 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 and succeededOrFailed() to run despite earlier failures.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →