Azure DevOps – Tips and Tricks – 3 – How to execute a Pipeline Task using Conditions

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

  1. Execute one of the Pipeline Task (say AuditLog) only when the rest of ALL pipeline Tasks fail. The AuditLog task could log some details about the failure of all tasks.
  2. Execute one of the Pipeline Tasks only when it’s dependent task(s) fails

You can also have your own custom conditions to control the execution of the Tasks. Below are few examples.

Custom Conditions

  1. Execute one of the Pipeline Task ONLY if the value of a variable is true.
  2. Execute one of the Pipeline Task ONLY if the current branch is develop
  3. 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 based on the built-in conditions. Below are the built-in conditions and their usage.

Note: By default, each Task (Jobs and Stages) have a built-in dependency on their previous Task, which means, if the previous task is failed, then the current task will not be execution

Advertisements

Built-In Conditions:

Below are the built-in conditions that can be used to control the execution of tasks.

Condition CriteriaDescription
succeeded()Runs ONLY when it’s dependent task is Succeeded. It’s the default behaviour.
failed()Runs ONLY when it’s dependency has failed.
succeededOrFailed()Runs when it’s previous depended task is succeeded of failed. It won’t run when pipeline execution is cancelled.
always()Runs ALL the times irrespective of the status of the previous dependent tasks or even if the pipeline execution is cancelled forcefully.
Advertisements

Below is an example of the usage of the failed() condition at a Job level. Similarly, it could be used at individual Task or even Stage level.

Azure DevOps – Tips and Tricks – 3 – How to execute a Pipeline Task based on a Condition – Pipeline for Jobs

The above screenshot is a pipeline with two jobs where the 1st job has an error which is created intentionally. Below are the points to note from the above pipeline.

  1. Job1 fails as there is a typo in Line: 17. It should be Write-Host instead of Write-Hosts
  2. Job2 gets executed even though Job1 is failed because it is dependent on Job1 and a condition failed() is specified.

Let’s now learn how to use Custom-Conditions to control the execution of Tasks. In this scenario, you may want to execute a Task / Job / Stage when the value of a variable is set to true. Let see how to configure it.

Create a variable as shown below and set it to true.

Advertisements
Azure DevOps – Tips and Tricks – 3 – How to execute a Pipeline Task based on a Condition – Variable

In the pipeline, add the condition as shown below to control the execution only when the variable ExecuteTaskBasedonCondtion is set to true

Azure DevOps – Tips and Tricks – 3 – How to execute a Pipeline Task based on a Condition – Custom Condition
Advertisements

In the above screenshot, a condition is used to control the execution of the Task based on the value of a variable.

If the value of the variable ExecuteTaskBasedonCondition is set to true then the Task will execute otherwise it will be skipped. Below is a screenshot which shows that the task is skipped when the value of the variable is set to false.

Azure DevOps – Tips and Tricks – 3 – How to execute a Pipeline Task based on a Condition – Custom Condition – Skipped

More above the Condition Expression:

  1. Here the variable is referred using the Variables construct instead of regular $(variablename) syntax.
  2. eq is a function that takes two parameters and return true if both of the inputs are same. Otherwise, returns false.

In this Tip, we have learnt how to add a very simple condition. For various scenarios, refer to the official documentation here.

Summary:

In this Tip, we have learnt how to control the execution of a Task / Job / Stage based on the conditions.

Feel free to share your feedback in the comments section. Thanks for reading.

Advertisements

Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups

WhatsApp

Telegram

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s