
Azure DevOps Conditions for Pipeline Tasks and Stages are a powerful tool to help you automate your CI/CD pipelines. They allow you to control the execution of pipeline tasks and stages based on specific conditions.
Conditions can be applied to individual tasks or entire stages, giving you fine-grained control over your pipeline's workflow. You can use conditions to skip tasks or stages if certain prerequisites are not met.
Conditions can be based on various factors, such as environment variables, build properties, and file existence. For example, you can use a condition to skip a task if a specific environment variable is not set.
By using conditions effectively, you can make your pipelines more robust, efficient, and easier to maintain.
Recommended read: Azure Pipelines or Condition
Conditions for Running a Stage, Job, or Step
Conditions for running a stage, job, or step in Azure DevOps are crucial to ensure that your pipeline executes as intended. By default, stages, jobs, and steps run if all direct and indirect dependencies succeed.
You can specify conditions in the pipeline definition YAML to control when a stage, job, or step runs. There are several conditions available, including "succeededOrFailed()", "always()", and "failed()". The "succeededOrFailed()" condition allows a stage, job, or step to run even if a previous dependency fails, unless the run is canceled.
The "always()" condition ensures a stage, job, or step runs even if a previous dependency fails and the run is canceled. On the other hand, the "failed()" condition makes a stage, job, or step run only if a previous dependency fails.
You can also use variables in conditions, such as setting and using an "isMain" variable to designate the main branch. Conditions are evaluated to determine whether to start a stage, job, or step, so nothing computed at runtime inside that unit of work is available.
Here's a summary of the conditions you can use:
Remember to take into account the state of the parent stage or job when specifying conditions. Canceling a build doesn't mean that all its stages, jobs, or steps stop running, and which stages, jobs, or steps stop running depend on the conditions you specified and at what point of the pipeline's execution you canceled the build.
Expand your knowledge: Azure Rbac Conditions
Custom Conditions
Custom conditions in Azure DevOps allow you to specify when a stage, job, or step runs, giving you more control over your pipeline's execution.
You can write custom conditions as expressions in YAML pipeline definitions, and the agent evaluates them from the innermost function outward, resulting in a boolean value that determines whether the task, job, or stage should run.
To create custom conditions, you can use built-in functions like succeeded(), failed(), and always() to specify when a stage, job, or step runs.
You can also use variables in conditions, such as setting and using an isMain variable to designate main as the Build.SourceBranch.
Here are some examples of custom conditions:
- Run if the source branch is main, even if the parent or preceding stage, job, or step failed or was canceled: eq(variables['Build.SourceBranch'], 'refs/heads/main')
- Run if the source branch is main and the parent or preceding stage, job, or step succeeded: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
- Run if the source branch isn't main, and the parent or preceding stage, job, or step succeeded: and(succeeded(), ne(variables['Build.SourceBranch'], 'refs/heads/main'))
You can set a condition to run if a variable is set to true, even if the parent or preceding stage, job, or step failed or was canceled: eq(variables['System.debug'], true)
You can also use custom conditions to run a task only if a specific variable's value is true, or if the current branch is 'develop', or if the pipeline is triggered automatically (Continuous Integration).
Note that custom conditions are evaluated at pipeline compilation time, not during runtime, so you can't use variables set at runtime in your custom conditions.
If you're looking for more advanced customization, you can use PowerShell scripting to generate variables and set their values, which can then be used in your custom conditions.
See what others are reading: Azure Devops Pipeline Triggers
Build Cancellation Outcomes
When you cancel a build in Azure DevOps, it doesn't necessarily stop all its stages, jobs, or steps from running. The outcome depends on the conditions you specified and the point at which you canceled the build.
A stage, job, or step will run if its conditions evaluate to true, regardless of its parent's status.
If a stage, job, or step's parent is skipped, the task won't run, even if its conditions are met. This is because the parent's status affects the task's execution.
To control whether stages, jobs, or steps run when a build is canceled, include a job status check function in your conditions.
Canceling a build at a specific point in the pipeline's execution can have different outcomes for the stages, jobs, or steps that follow.
A different take: Azure Devops Script Step
Stage Examples
In Azure DevOps, stages can have conditions that override their default dependencies. This means a stage can run regardless of the status of its preceding stage, as long as its condition is met.
A stage's condition can be set to run whenever the source branch is main, regardless of the status of its preceding stage. If you queue a build on the main branch and cancel it while the preceding stage is running, the dependent stage still runs.
Stages can also have a default condition that evaluates to false when the preceding stage is canceled, causing the dependent stage to be skipped. This can be seen when a stage has a job with a condition that evaluates to true, but the stage's default condition is succeeded(), which evaluates to false when the preceding stage is canceled.
In some cases, a stage's condition can override its default dependency, even if its preceding stage is canceled. This is because the stage's condition is evaluated separately from its dependency, allowing it to run regardless of the preceding stage's status.
If a stage has a condition that requires the source branch to be main, it will run even if its preceding stage is canceled, as long as the source branch is indeed main. This allows for more flexibility in your pipeline's dependencies and conditions.
Curious to learn more? Check out: Azure Devops Branch Policies
Job Examples
In Azure DevOps, conditions can be set to control when a stage or job runs. A stage can have a condition that overrides its default dependency on the previous stage.
A stage can be configured to run regardless of the status of the previous stage, as long as a specific condition is met. For instance, if a stage has a condition that checks if the source branch is main, it will run as long as the build is queued on the main branch.
If a stage has a default condition that checks if the previous stage succeeded, it will be skipped if the previous stage is canceled. This is because the default condition evaluates to false when the previous stage is canceled.
Even if a job within a stage has a condition that evaluates to true, the entire stage will be skipped if the default condition fails. This can be seen in a scenario where a job in stage2 has a condition that evaluates to true, but the stage has a default condition that checks if the previous stage succeeded.
The condition eq(variables['Build.SourceBranch'], 'refs/heads/main') evaluates to true when the build is queued on the main branch. This allows a stage to run regardless of the status of the previous stage, as long as the build is on the main branch.
For more insights, see: Azure Dev Ops Status
Step Examples
You can have conditions on steps, which is super useful for controlling the flow of your pipeline. This allows you to run certain steps only when specific conditions are met.
For example, step 2.3 has a condition set to run whenever the source branch is main. If you queue a build on the main branch and cancel it while steps 2.1 or 2.2 are running, step 2.3 still runs, because the condition evaluates to true.
Conditions can be used to skip steps that are not necessary, which can save time and resources. This is especially useful when working on large pipelines with many steps.
A unique perspective: Azure Devops Delete Branch
Condition Settings
Conditions in Azure DevOps are a powerful tool to control the flow of your pipeline. They allow you to specify when a stage, job, or step runs based on various conditions.
You can set conditions to run a stage, job, or step based on the status of its dependencies. By default, stages, jobs, and steps run if all direct and indirect dependencies succeed. This status is the same as specifying condition: succeeded(). But you can overwrite this default condition with your own custom conditions.
The following table shows example condition settings to produce various outcomes:
You can also use the "failed()" condition to run a stage, job, or step only when a previous dependency fails.
Built In Conditions
By default, stages, jobs, and steps run if all direct and indirect dependencies succeed, which is the same as specifying condition: succeeded(). This status is also known as the succeeded status function.
You can also make stages, jobs, and steps run even if a previous dependency fails, unless the run is canceled. This is done by using the succeededOrFailed() condition in the YAML.
The always() condition allows stages, jobs, and steps to run even if a previous dependency fails, and even if the run is canceled.
On the other hand, the failed() condition makes stages, jobs, and steps run only when a previous dependency fails.
Here are some examples of built-in conditions:
Solution
In Azure DevOps, conditions play a crucial role in managing task execution. Built-in conditions allow you to control execution based on the status of previous tasks or pipeline cancellation.
There are several built-in conditions available, including succeeded(), failed(), succeeded or failed(), and always(). Each condition has a specific purpose and can be applied to Jobs, Tasks, or Stages.
The failed() condition, for instance, can be used to run a task even if its dependent task fails. This is demonstrated in the example where Job1 fails due to a typo, but Job2 still runs because it depends on Job1 and a failed() condition is specified.
To create a custom condition, you can set up a variable and control task execution based on its value. This is achieved by referencing the variable using the 'Variables' construct and using functions like 'eq' to compare its value.
Here are the built-in conditions and their usage:
By understanding and applying these conditions, you can effectively manage task execution in Azure DevOps and streamline your pipeline processes.
Finding and Setting Conditions
To find and set conditions in Azure DevOps, start by selecting any task in your pipeline and expanding the Control Options section on the right-hand panel. From there, click on the Run this task selector and choose Custom conditions.
A new Custom condition input will appear below, allowing for in-depth customization. For example, you can set a condition to run if a variable is set to true, even if the parent or preceding stage, job, or step failed or was canceled, as shown in the example: eq(variables['System.debug'], true).
You can also use the "Custom conditions" option to set conditions to run if a variable is null (empty string), which is equivalent to null in Azure Pipelines.
On a similar theme: Azure Devops Extension Example
Finding the Setting
To find the setting for custom conditions, start by selecting any task in your pipeline. This will open up a panel on the right side of the screen where you can access various options.
On this options panel, locate the Control Options heading and click it to expand the section. This will give you access to more advanced settings.
Below the Control Options heading, you'll find the Run this task selector, which allows you to choose when and under what conditions the task should run.
Additional reading: Azure Conditional Access
You can select from predefined options, such as "Only when a previous task has failed", which can be useful for sending notifications to your team. However, for more in-depth customization, it's recommended to use the "Custom conditions" option.
By choosing Custom conditions, a new Custom condition input will appear below, where you can set specific conditions for the task to run.
Condition Settings
Conditions play a crucial role in determining when a stage, job, or step runs in a pipeline.
To set conditions, you can specify them in the pipeline definition YAML. By default, stages, jobs, and steps run if all direct and indirect dependencies succeed. This status is the same as specifying condition: succeeded().
The default condition can be overwritten by specifying your own conditions, which can cause your stage, job, or step to run even if the build is canceled. Make sure the conditions you write take into account the state of the parent stage or job.

You can use variables in conditions, such as the isMain variable to designate main as the Build.SourceBranch. The following YAML example shows the always() and failed() conditions.
The following table shows example condition settings to produce various outcomes:
Advanced Conditions
You can set conditions to run stages, jobs, or steps based on various criteria, including the source branch, build reason, and variable values.
The default condition is to run if all direct and indirect dependencies succeed. However, you can override this by specifying your own condition in the YAML file.
You can use the always() function to run a stage, job, or step even if previous dependencies fail or the build is canceled.
The failed() function is useful when you want to run a stage, job, or step only if a previous dependency fails.
You can also use variables in conditions, such as the isMain variable to designate main as the Build.SourceBranch.
Here are some example condition settings that produce various outcomes:
You can use the eq() function to check if a variable is equal to a specific value, and the ne() function to check if a variable is not equal to a specific value.
Remember that conditions are evaluated at the start of a stage, job, or step, so nothing computed at runtime is available for use in conditions.
Featured Images: pexels.com


