Monday 5 November 2018

Custom Control Conditions in Azure DevOps Pipeline Steps

Azure DevOps pipelines support default conditions allowing you to execute a step “Only when all previous tasks have succeeded”, “Even if a previous task has failed, unless the build was canceled”, “Even if a previous task has failed, even if the build was canceled” or “Only when a previous task has failed”. These conditions facilitate execution of  steps in a build or release pipeline to cater different scenarios. However, to support scenarios not covered by the default conditions you can implement custom control conditions in Azure DevOps pipeline steps. There is a good explanation of custom conditions in this article. Let’s explore two advance scenarios that can be handled with custom conditions, which are not explained in the article.

You may want to avoid executing a step is the branch is not starting with a given branch name pattern.  For example if you want to skip branches start with features/ you can add the custom condition as show below. Note the usage of “not” and the “startsWith” in combination to achieve the condition required.

and (succeeded(), not(startsWith(variables['Build.SourceBranch'], 'refs/heads/features/')))

Next lets look at how to write complex condition. Below specification says step should be executed when al previous steps succeeded, AND when it is NOT a pull request build AND the branch is either “master” or “develop” or a branch starts with “version/”. So the first two conditions should be true and branch name condition should be true, by being one of the three branch conditions being true. Note the usage of () and conditions “and”, “or” , “ne”, “eq” and “startsWith”. Further note the branch names in below condition is used with BitBucket cloud as the source control repo.

and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'), or(eq(variables['Build.SourceBranch'], 'master'), eq(variables['Build.SourceBranch'], 'develop'), startsWith(variables['Build.SourceBranch'], 'version/')))

No comments:

Popular Posts