Azure Pipelines is an Azure DevOps service that allows anyone to easily build, test, and deploy with CI/CD. These pipelines offer a ton of customization on their own with hundreds of available build tasks (steps), countless integrations, and triggers based on other builds completing or a set schedule. When it comes to customizing the pipeline tasks, however, things get a little more complicated.
What if you only want to run a specific pipeline task on Mondays? What if you want to run certain tasks if the build was kicked off manually? What if you have a custom variable and want to run a task based on its value? All of these situations are made possible by the use of custom conditions in Azure Pipelines. This useful setting is hidden away on each pipeline task and will unlock customization options for all your needs.
To start off, there are a few easy steps we need to follow:
- Log in to Azure DevOps and navigate to your project.
- On the left side, click on Pipelines.
- Create a new pipeline or edit an existing one.
- Add at least one build task to your pipeline.
- That’s it! It’s time to customize.
Note: For this tutorial, I am using the Classic UI in Azure instead of YAML. If you are using YAML, the general approach should be similar enough to follow along. Let’s continue!
Finding the Setting
Now that we have our pipeline open and in edit mode, let’s familiarize ourselves with the custom condition setting:
- Select any task in your pipeline.
- On the options panel on the right, locate the “Control Options” heading and click it to expand the section.
- Open the “Run this task” selector and choose “Custom conditions”.
- Notice how a new “Custom condition” input appears below. This is the setting we will be focusing on!
Note: The “Run this task” selector has some predefined options that allow for some basic customization. For example, you can select “Only when a previous task has failed” if you want the task to only run if the build fails. One use for this would be if you want to send a Slack message to your team notifying them of the failure. For more in-depth customization, I recommend using the “Custom conditions” option, as it makes the possibilities virtually endless.
Azure has some great documentation on custom conditions, and they even give some useful examples to get you started. Reading through the examples will help you understand the expressions and how they are constructed. If you still have questions after looking at the examples, check out the documentation on expressions within Azure DevOps to understand the syntax for variables, functions, and more.
As we continue, I will show off some specific examples of useful custom conditions and then show you how to unlock even more possibilities with custom conditions paired with PowerShell scripts.
For each example, I will give a brief explanation of what the custom condition does and then show the syntax. Feel free to skip to the example that suits your needs or scroll to the PowerShell section for maximum customization, like running a task on a specific day of the week.
When a variable is set to false
Explanation: You only want to run a task if one of your pipeline variables is set to false.
Example: Run a task when system debug is set to false.
When a variable equals a specific value
Explanation: You only want to run a task when a variable equals a specific value.
Example: Send a Slack message if your notifications variable is set to public.
When a build is kicked off manually
Explanation: You only want to run a task if the build is queued manually through the Azure Pipelines UI or via the Azure API.
Example: Update the npm packages each time the build is run manually.
When a build is triggered from a schedule
Explanation: You only want to run a task when the build is queued via a schedule that is set on the Triggers tab.
Example: Publish the test results when the build is run on a schedule so that the number of results is consistent each week.
Custom PowerShell Script: When it’s a specific day of the week
Explanation: If the above options don’t provide enough customization, PowerShell scripting may be your answer. A PowerShell script in your pipeline allows you to generate a variable and set its value to anything you want. You can get the value from an API call, function, date formatter, etc. After creating the variable, you can use it in your task’s custom condition and run or ignore the task based on its value.
- While editing your pipeline, click the + button on the agent job to add a new task.
- In the “Add tasks” window, search for and add the “PowerShell” task (make sure this task is above the task that will use the custom condition).
- Select the PowerShell task
- Choose “Inline” for the Type.
- Write a script to generate a variable that you can use in your custom condition. In the below example, I am creating a variable to store the current day of the week.
- Create a new pipeline variable in Powershell to store the value you set in the previous step. This allows other pipeline tasks to use that variable’s value.
- Select your task that will use the custom condition and set the custom condition to look for the value you are expecting in the new variable.
Example: Run a task only on Mondays that deletes the previous week’s cached files.
# Creates a variable ($day) that stores the current day of the week
$day = $(Get-Date -Format dddd);
# Creates a new pipeline variable (DayOfWeek) that stores the current day of the week ($day)
Write-Host "##vso[task.setvariable variable=DayOfWeek]$day"
The above examples are just a small preview of all the possibilities that custom conditions bring to Azure Pipelines. If you are passionate about customization, I am sure you will find even more unique ways of customizing pipelines to fit your needs. Thanks to Microsoft’s great documentation and examples, I was able to quickly learn about this feature and find practical uses for it in my daily work.
Did you know about custom conditions before reading this article? How do you plan on using custom conditions to improve your build pipelines? Are you still having issues with understanding this feature? We are here to help, and we love feedback, so please send us an email with your comments or questions. Happy customizing!