'Using 'replace' expression in Azure Devops pipeline

I'm trying to build and tag a container image based on the git branch I'm building. ADO has two options for the branch name: Build.SourceBranch eg. refs/heads/rc/1.0.0 or Build.SourceBranchName eg. 1.0.0

However I want to tag my image using rc1.0.0, and I figured that should be possible by initially in my yml defining a variable using:

variables:
  temp: ${{replace('$(Build.SourceBranch)', 'refs/heads/', '')}}
  dockertag: ${{replace('$(temp)','/','')}}

However when I bash: echo '$(dockertag)' it produces refs/heads/master for the master branch where I would have expected master instead.

I'll setup a separate pipeline to test this, but I figured possibly I'm doing something obvious wrong?



Solution 1:[1]

Replacing characters in strings and branches has been covered here: How to replace in variable strings inside azure-pipelines.yaml?

In short try:

variables:      
  suffix: $[replace(variables['build.sourcebranchname'], 'refs/heads/', '')]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Meyer Elphick Frandsen