David Gard
David Gard

Reputation: 12047

Conditionally include the branch name in an Azure DevOps build name

I'm using Azure DevOps to build a pipeline, but I wish to change the name of the build. This is entirely possible according to the documentation.

With that in mind, I tested the following, which worked. So far so good...

name: '1.0.$(Rev:r)-$(Build.SourceBranchName)'

1.0.1-master

However, one of my requirements is to exclude the branch name if it's 'master', so I tried the following. This seemed to work in the first instance, as a 'master' did not have a branch name appended, but when I ran a feature branch I found that $(Build.SourceBranchName) was not being expanded.

variables:
  ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
    branchName: ''
  ${{ if ne(variables['Build.SourceBranchName'], 'master') }}:
    branchName: '-$(Build.SourceBranchName)'

name: '1.0.$(Rev:r)$(branchName)'

I've since tried ${{ variables.Build.SourceBranchName }} and $[variables.Build.SourceBranchName] as described in the documentation, but as written they token is either ignored or returns an empty string. The three formats leave me with the following build names.

1.0.1-$(Build.SourceBranchName)
1.0.1-
1.0.1-$[variables.Build.SourceBranchName]

The documentation around custom naming makes mention of a variable $(SourceBranchName), but I've tried this and it also fails.

What I find confusing is that the expressions in the variables: segment can access the variable values as described here, but seemingly the variables: segment itself is unable to.

Is it possible to conditionally name the build so that I can include/exclude the branch name as required?

Upvotes: 5

Views: 20622

Answers (2)

Eric Smith
Eric Smith

Reputation: 2560

The problem is how you are referencing $(Build.SourceBranchName) in the parse time expression. It is not available in that format under the context. Additional info here

This works for me!

variables:
  system.debug: true
  ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
    branchName: ''
  ${{ if ne( variables['Build.SourceBranchName'], 'master') }}:
    branchName: -${{ variables['Build.SourceBranchName'] }}

name: '1.0.$(Rev:r)$(branchName)'

Some good additional info in Lance's answer ##vso[build.updatebuildnumber] might be a good path, or you could use GitVersion.

Upvotes: 11

LoLance
LoLance

Reputation: 28146

Is it possible to conditionally name the build so that I can include/exclude the branch name as required?

An alternative workaround is to use PowerShell task to update the build number. Check this official document:

You can use something like: ##vso[build.updatebuildnumber]my-new-build-number.

Example:

name: 1.0.$(Rev:r)

steps:

- task: PowerShell@2
  condition: and(succeeded(), ne(variables['Build.SourceBranchName'], 'master'))
  inputs:
    targetType: 'inline'
    script: |
      $buildNumber = $Env:BUILD_BUILDNUMBER
      $revision= $buildNumber.Substring($buildNumber.LastIndexOf('.') + 1)
      Write-Host "##vso[build.updatebuildnumber]1.0.$revision-$(Build.SourceBranchName)"

Copy this to your yaml, then it can work to meet your requirements above. About how to use $(rev:r) in task, get hint from this issue, thanks to Shayki Abramczyk!

Hope all above helps :)

Upvotes: 1

Related Questions