Azure Pipelines conditions and cancellation: keep branch checks from replacing success checks
Explain how custom Azure Pipelines conditions replace defaults. Evaluate failed and canceled dependencies, branch eligibility and cleanup without allowing an unintended deployment.
TL;DR: A custom Azure condition replaces the default condition. For a release that requires successful dependencies and a selected branch, include both requirements. Cleanup may intentionally run after failure or cancellation, but deployment must have its own stricter condition.
A true branch comparison says nothing about tests
A stage conditioned only on eq(variables['Build.SourceBranch'], 'refs/heads/main') asks whether the source is main. It does not also assert that the required build succeeded. When authors intend both conditions, they must express both, with the proper dependency graph.
For an ordinary dependent release job, a typical expression is and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main')). The Azure conditions reference explains the default replacement and cancellation cases. Read the job/stage context: a child cannot run when its parent is skipped merely because the child's own condition is true.
Define the truth table before YAML
This teaching policy requires both success and main-branch eligibility. Cleanup is separate and may remove temporary test resources if its own identity and cancellation budget permit it.
| Dependency outcome | Branch | Release allowed |
|---|---|---|
| Succeeded | Main | Yes, subject to resource checks |
| Failed | Main | No |
| Canceled | Main | No |
| Succeeded | Feature | No |
| Skipped prerequisite | Main | No under this required-evidence contract |
The table is an intended policy, not a complete emulator of Azure's result functions. Validate the installed service's behavior for partially successful, skipped and canceled jobs. If the policy requires strictly successful named dependencies, express and test those results explicitly instead of assuming every status is interchangeable.
A check on the current branch also does not prove the candidate passed required checks in another pipeline. Tie release evidence to the current immutable artifact and the actual dependencies supplying that evidence.
Cancellation is a control-flow event
always() can permit work to run despite a cancellation or failure in relevant contexts. That makes it useful for report collection or cleanup, not as a generic way to “make deploy run reliably.” Cleanup itself can be interrupted or lack a usable agent, so maintain a later reconciliation path for abandoned resources.
The placement matters. A condition on a step cannot revive a skipped job, and a condition on a job cannot revive a skipped stage. Start diagnosis at the first omitted parent rather than repeatedly changing the deepest step.
Resource-owned approvals and checks still control access to environments or service connections. They supplement correct dependency conditions; they do not make a branch-only success policy adequate. See Azure templates and resource checks for the ownership boundary.
Test with a harmless release marker
Use a disposable pipeline with a producer that can deliberately fail and a dependent job that only prints a marker. Exercise success on main, failure on main, cancellation and a feature branch. The marker should appear only for the approved positive case. Record what ran, what was skipped and why, then repeat with the faulty branch-only condition to show the specific regression.
Self-check: a deployment has always() and checks for main inside its shell script, but never checks the upstream test result. Can a failed main build deploy? Yes, if the job is admitted and the script proceeds. Put required success into the deployment's admission logic and verify the negative case.