An Azure Pipeline task is a single task to be performed in an Azure Pipeline. An Azure Pipeline Job is a grouping of tasks that run sequentially on the same target. In many cases, you will want to only execute a task or a job if a specific condition has been met. Azure Pipeline conditions allow us to define conditions under which a task or job will execute. For more information on Azure Pipeline conditions, see Azure Pipeline Conditions.
In this blog, I will detail a common situation in which pipeline conditions are helpful, the configuration of this condition, and will include documentation links for more information.
Common Use Case: Pull Request Validation
I’ve been working with an Azure Build Pipeline that first tests several pieces of Python code, publishes the test results to the pipeline, and then packages up a Helm chart and three container images. These artifacts are then pushed to Azure Container Registry.
On this pipeline, I have configured a trigger so that the Pipeline is run both when code is committed to the master branch of the associated repository AND when a pull request is made against the master branch of the repository. This is cool because the pipeline will now run all unit tests when a pull request is created, and provide test results for review prior to merging the pull request.
Things look good, however, I found that when a pull request is made, not only are the tests running, but the artifacts are built and pushed to the Azure Container Registry. This is not what I want to occur. I want the artifact jobs to only run once a pull request has been merged to master.
Digging into execution conditions for my artifact jobs, I found that the default condition is,
Only when all previous jobs have succeeded which seems to be the culprit here.
Rather than executing when all previous jobs were successful, I want to only execute the artifact jobs when the previous jobs were successful and the trigger was not a pull request. For this configuration, we can use custom conditions.
Task and Job Conditions
Task and job conditions allow us to build custom and if needed complex conditions under which a task or job will run. Conditions are built using a series of pipeline expressions. Details on expression capability and syntax can be found at the Expression documentation.
After some experimentation, I found that I can change the condition from
Only when all previous jobs have succeeded, to
Custom condition using variable expressions, and then provide the following condition to meet my expected result. This condition will trigger when the dependant jobs were successful and the build reason is not equal to a pull request.
and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
Here is what the condition looks like in my build pipeline.
If using a YAML based pipeline, the configuration would look similar to this.
- job: Build Artifacts dependsOn: Run Tests condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
This is just one simple example. Using the expression language you should be able to finely control the execution behavior of you Azure build and release pipelines.
Feel free to reach out in comments or on Twitter at @nepeters.