Custom Task Conditions in Azure Pipelines

Here are a few examples of all the possibilities custom conditions bring to Azure Pipelines.

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.

Getting Started

To start off, there are a few easy steps we need to follow:

  1. Log in to Azure DevOps and navigate to your project.
  2. On the left side, click on Pipelines.
  3. Create a new pipeline or edit an existing one.
  4. Add at least one build task to your pipeline.
  5. That’s it! It’s time to customize.
Pipelines in Azure DevOps

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:

  1. Select any task in your pipeline.
  2. On the options panel on the right, locate the Control Options heading and click it to expand the section.
  3. Open the Run this task selector and choose Custom conditions.
  4. Notice how a new Custom condition input appears below. This is the setting we will be focusing on!
Azure DevOps 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.

Custom Conditions

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.

Examples

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.

Custom Condition:

				
					eq(variables['system.debug'], '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.

Custom Condition:

				
					eq(variables['notifications'], '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.

Custom Condition:

				
					eq(variables['Build.Reason'], 'Manual') 

				
			

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 Condition:

				
					eq(variables['Build.Reason'], 'Schedule') 


				
			

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.

Set Up:

  1. While editing your pipeline, click the + button on the agent job to add a new task.
  2. 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).
  3. Select the PowerShell task
  4. Choose “Inline” for the Type.
  5. 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.
  6. 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.
  7. 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.
Custom PowerShell Script: When it’s a specific day of the week steps
Custom PowerShell Script: When it’s a specific day of the week settings

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')

				
			

Conclusion

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!

About the Author

Tyler Pfohl

Tyler originates from the corn-filled land of Iowa and has loved technology since he was little. Originally he wanted to become a programmer but his older brother introduced him to the amazing world of QA in 2014. Ever since then, he has developed a hunger for ensuring high quality. His latest passion is using automation to quickly find issues on web pages. When he’s not making his computer work for him, you can usually find him playing video games, riding his bike, or trying a new craft beer.

Subscribe to Our Blog

Stay up to date on what BizStream is doing and keep in the loop on the latest in marketing & technology.