Branch Name as Variable in Azure DevOps Pipelines with YAML

In many CI/CD scenarios it’s necessary to adjust the build, test or deployment process depending on which GIT branch has triggered the pipeline. In our case we build lots of Angular apps automatically with their desired target environment, e.g.

ng build --configuration="production" or
ng build --configuration="staging"

depending on where the artifact should be deployed afterwards. This can be achieved by adding some YAML that looks like this:

If you want to copy the code, please do so from “view raw” since the pretty-fied version might cause some issues in YAML.

– script: ng build –configuration=dev
displayName: 'Build app (dev)'
condition: eq(variables['build.sourceBranch'], 'refs/heads/dev')
– script: ng build –configuration=staging
displayName: 'Build app (staging)'
condition: eq(variables['build.sourceBranch'], 'refs/heads/staging')
– script: ng build –configuration=production
displayName: 'Build app (production)'
condition: eq(variables['build.sourceBranch'], 'refs/heads/main')

However, this will not work for builds triggered by Pull Requests since in those cases the source branch info will look something like this:

BUILD_SOURCEBRANCH = refs/pull/101/merge
BUILD_SOURCEBRANCHNAME = merge
BUILD_SOURCEVERSIONMESSAGE = Merge pull request 101 from dev/myDevBranch into master

So to get the correct branch name for your builds you’ll need to distinguish between PRs and “regular” builds before deciding on how to build your app. For our pipeline decided to create a variable “branchName” which we can then use in our conditions.

variables:
${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
branchName: $[ replace(variables['Build.SourceBranch'], 'refs/heads/', '') ]
${{ if startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
branchName: $[ replace(variables['System.PullRequest.TargetBranch'], 'refs/heads/', '') ]
script: ng build –configuration=dev
displayName: 'Build app (dev)'
condition: eq(variables['branchName'], 'dev')
script: ng build –configuration=staging
displayName: 'Build app (staging)'
condition: eq(variables['branchName'], 'staging')
script: ng build –configuration=production
displayName: 'Build app (production)'
condition: eq(variables['branchName'], 'main')

Original Photo by Mike Benna on Unsplash

Thanks to David for the idea!

One thought on “Branch Name as Variable in Azure DevOps Pipelines with YAML”

  1. Looks like you have a typo in:
    variables:
    ${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/heads/’) }}:
    branchName: $[ replace(variables[‘Build.SourceBranch’], ‘refs/heads/’, ”) ]
    ${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/pull/’) }}:
    branchName: $[ replace(variables[‘System.PullRequest.TargetBranch’], ‘refs/heads/’, ”) ]
    Should probably be:
    variables:
    ${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/heads/’) }}:
    branchName: $[ replace(variables[‘Build.SourceBranch’], ‘refs/heads/’, ”) ]
    ${{ if startsWith(variables[‘Build.SourceBranch’], ‘refs/pull/’) }}:
    branchName: $[ replace(variables[‘System.PullRequest.TargetBranch’], ‘refs/pull/’, ‘
    ‘) ]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.