In this article, we are going to learn about executing a particular Pipeline Task based on conditions.

Introduction
Azure DevOps – Pipelines support conditional execution of a Task. It means we can control the execution of the task based on a condition and decide if we want to execute it. Azure DevOps supports the below types of conditions
- Built-In Condition
- Custom Condition
Scenario
An Azure DevOps Pipeline is a set of Tasks which are executed in a sequence by the Agent. Sometimes, it may be required to execute a particular task based on some logical condition. Below are some scenarios.
Built-In Conditions
- Execute one of the Pipeline tasks (say AuditLog) only when the rest of ALL pipeline Tasks fail. The AuditLog task could log some details about the failure of all tasks.
- Execute one of the Pipeline Tasks only when its dependent task(s) fails.
You can also have your own custom conditions to control the execution of the Tasks. Below are a few examples.
Custom Conditions
- Execute one of the Pipeline Tasks ONLY if the value of a variable is true.
- Execute one of the Pipeline Task ONLY if the current branch is develop.
- Execute the Pipeline ONLY if the pipeline is executed automatically (Continuous Integration).
Solution
Let’s first see how to leverage the built-in conditions and learn how to control them based on the built-in conditions. Below are the built-in conditions and their usage.
Note. By default, each Task (Jobs and Stages) has a built-in dependency on the previous Task, which means, that if the previous task fails, then the current task will not be executed.
Built-In Conditions
Below are the built-in conditions that can be used to control the execution of tasks.
| Condition Criteria | Description |
| succeeded() | Runs ONLY when its dependent task is Succeeded. It’s the default behavior. |
| failed() | Runs ONLY when its dependency has failed. |
| succeeded or failed() | Runs when its previous depended task is succeeded of failed. It won’t run when pipeline execution is cancelled. |
| always() | Runs ALL the time irrespective of the status of the previous dependent tasks or even if the pipeline execution is cancelled forcefully. |




Join the conversation! Your thoughts help the community grow.