Azure

Azure DevOps: How to Run a Pipeline Task with Conditions

We will explore how to execute a specific Pipeline Task under certain conditions in this article.

Introduction

Azure DevOps – Pipelines offer the ability to run a Task conditionally, meaning we can determine whether or not to run the task based on specific conditions. Azure DevOps supports the following types of conditions:

  • Built-In Condition
  • Custom Condition

Scenario

An Azure DevOps Pipeline consists of Tasks that are executed sequentially by the Agent. At times, there may be a need to run a specific task based on certain conditions. Here are some scenarios to consider:

Built-In Conditions

  1. Run a Pipeline task (such as AuditLog) only if all other pipeline Tasks fail. This task could log details about the failure of all other tasks.
  2. Run a Pipeline Task only if its dependent task(s) fail.

You can also create custom conditions to control how tasks are executed. Here are a few examples:

Custom Conditions

  1. Run a Pipeline Task ONLY if a specific variable’s value is true.
  2. Run a Pipeline Task ONLY if the current branch is ‘develop’.
  3. Run the Pipeline ONLY if it is triggered automatically (Continuous Integration).

Solution

Let’s begin by understanding how to utilize the built-in conditions and how to customize them based on specific 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. This means that if the previous task fails, the current task will not be executed.

Built-In Conditions

Here are the built-in conditions that can be used to manage task execution:

Condition Criteria Description
succeeded() Runs only when its dependent task succeeds (default behavior).
failed() Runs only when its dependent task fails.
succeeded or failed() Runs when its previous depended task succeeds or fails. Won’t run if the pipeline execution is cancelled.
always() Runs all the time regardless of the status of previous tasks or pipeline cancellation.

For example, the failed() condition can be used at the Job level to control task execution. It can also be applied to individual Tasks or Stages.

Individual Task

In the screenshot above, a pipeline with two jobs is shown where the 1st job intentionally contains an error. Here are the key points:

  1. Job1 fails due to a typo in Line 17.
  2. Job2 runs even though Job1 fails because it depends on Job1 and a failed() condition is specified.

Let’s now explore how to apply custom conditions to control task execution. In this scenario, you may want to run a Task / Job / Stage when a specific variable is set to true. Here’s how to set it up:

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

Variables

In the pipeline, add a condition to control execution only when the variable ‘ExecuteTaskBasedonCondtion’ is true.

ExecuteTask

The condition in the above screenshot determines whether the task should be executed based on the variable’s value.

If ‘ExecuteTaskBasedonCondition’ is true, the Task will run; otherwise, it will be skipped. Below is a screenshot showing the task being skipped when the variable is set to false.

More about the Condition Expression:

  1. The variable is referenced using the ‘Variables’ construct instead of the regular $(variable name) syntax.
  2. ‘eq’ is a function that returns true if both inputs are the same, otherwise false.

In this article, we have covered a basic condition. For more scenarios, refer to the official documentation here.

Summary

In this article, we have discussed how to manage task execution based on conditions.

Feel free to share your thoughts in the comments. Thank you for reading.

Learn more about Skrots and the services we provide at Skrots. For a detailed list of our services, visit Skrots Services. Also, explore more blogs on various topics at Blog at Skrots.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button