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:
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!
Now that we have our pipeline open and in edit mode, let’s familiarize ourselves with the custom condition setting:
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.
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.
Custom Condition:
eq(variables['system.debug'], 'false')
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.
Custom Condition:
eq(variables['notifications'], 'public')
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.
Custom Condition:
eq(variables['Build.Reason'], 'Manual')
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 Condition:
eq(variables['Build.Reason'], 'Schedule')
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.
Set Up:
Example: Run a task only on Mondays that deletes the previous week’s cached files.
PowerShell Script:
# 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"
Custom Condition:
eq(variables['DayOfWeek'], 'Monday')
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!
We love to make cool things with cool people. Have a project you’d like to collaborate on? Let’s chat!
Stay up to date on what BizStream is doing and keep in the loop on the latest in marketing & technology.