56

Azure Devops offers two variables containing information about the current git branch name: $(Build.SourceBranchName) and $(Build.SourceBranch).

While SourceBranch contains the full reference to the branch, SourceBranchName is expected to contain only the short branch name.

Unfortunately, the behavior is a bit unexpected when the branch name contains a slash (/):

+---------------------------------------------------------------------------------------------------------+
| Situation                     | Git branch name  | Build.SourceBranch          | Build.SourceBranchName |
|---------------------------------------------------------------------------------------------------------|
| branch name contains no slash | mybranch         | refs/heads/mybranch         | mybranch               |
| branch name contains slash    | release/mybranch | refs/heads/release/mybranch | mybranch               |
+---------------------------------------------------------------------------------------------------------+

The part of the branch name before the slash is not considered as part of the branch name. My colleague pointed out that this is the documented behavior of Azure Devops:

Git repo branch or pull request: The last path segment in the ref. For example, in refs/heads/master this value is master. In refs/heads/feature/tools this value is tools.

I am not sure if this behavior is particularly useful: I want to checkout the branch, and need the branch name to include the slash. Also, If the part before the slash is stripped off, there might well be confusion about the actual path, as the name could be ambiguous.

I need the branch name including the slash. Is there any simple way to get it? Do I always have to work with the full ref in order to be on the safe side?

2
  • It is so hard to decide which one is more correct lol
    – hey
    Commented Feb 9, 2020 at 22:07
  • Hi hey thanks for your kindly response. Both reply is ok, same concept just with different implementation. You could choose one more apply to your situation. Up to you. Commented Feb 10, 2020 at 12:24

7 Answers 7

55

You can use replace expression to remove refs/heads/ from Build.SourceBranch

  variables:
    BRANCH_NAME: $[replace(variables['Build.SourceBranch'], 'refs/heads/', '')]

Use like this (edited typo)

$(BRANCH_NAME)
3
  • 4
    Yes, but you must use Build.SourceBranch !!! The var in your example SourceBranchName only returns the last path segment in the ref.
    – Chris
    Commented Jun 16, 2022 at 12:37
  • 1
    Aside from the error that @Chris pointed out, this should be the accepted answer
    – lvilasboas
    Commented Jun 24, 2022 at 12:39
  • Heads up, the answer assumes the trigger is a branch and not a tag
    – CervEd
    Commented Mar 26 at 17:11
36

I always use Build.SourceBranch in my scripts. Just assign it to a new variable and remove refs/heads/ from the start. I use only for CI and PR:

  1. For CI. I use Build.SourceBranch variable without refs/heads. I work with PowerShell:

$branchSource = "$(Build.SourceBranch)"
$branchSourcePath = $branchSource -replace "refs/heads/", ""

  1. For PRs. I use System.PullRequest.SourceBranch variable without refs/heads because Build.SourceBranch contains the path to the remote PR. The replacement is the same as in the first option just use the right variable.

$branchSource = "$(System.PullRequest.SourceBranch)"
$branchSourcePath = $branchSource -replace "refs/heads/", ""

1
  • Do you have some additional logic for cases where you build on a PR or a tag?
    – hey
    Commented Jan 29, 2020 at 1:10
15

When you build on a PR, you could use System.PullRequest.SourceBranch and System.PullRequest.TargetBranch variables.

System.PullRequest.TargetBranch

The branch that is the target of a pull request. For example: refs/heads/master. This variable is initialized only if the build ran because of a Git PR affected by a branch policy.

Use Predefined Build Variables

Besides, you could also define your own variable based on your needs if you want to use full or short path.

Just create a bash script that assigns the shorter branch name to a variable.

# Bash script
BRANCH_NAME=$(echo "$(System.PullRequest.TargetBranch)" | awk -F/ '{print $NF}')
echo "##vso[task.setvariable variable=PullRequest_Target_Branch;]$BRANCH_NAME"

Then you could reference $(PullRequest_Target_Branch) in your pipeline later.

0
10

It's wild there is no proper solution to this yet.

Here is what we have, based on combining a few things I've found googling. This is for branches and PR's

variables:

  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
    BRANCH_NAME: $[ replace(variables['Build.SourceBranch'], 'refs/heads/', '') ]
    DOCKER_IMAGE_TAG: $[ replace(replace(variables['Build.SourceBranch'], 'refs/heads/', ''), '/', '_') ]

  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
    BRANCH_NAME: $[ replace(variables['System.PullRequest.SourceBranch'], 'refs/heads/', '') ]
    DOCKER_IMAGE_TAG: $[ replace(replace(variables['System.PullRequest.SourceBranch'], 'refs/heads/', ''), '/', '_') ]

BRANCH_NAME will include the slash. So refs/heads/mybranch will be mybranch but refs/heads/feature/mybranch will be feature/mybranch.

Also added DOCKER_IMAGE_TAG here as we needed to replace the / with _ for docker images tags

2
  • One year later an there is still no better solution. Thanks for sharing.
    – Bennimi
    Commented Apr 27, 2023 at 23:03
  • my guess is that it's triggered by a tag your also going to want to deal with refs/tags/
    – CervEd
    Commented Mar 6 at 16:07
0

Can be used below in the azure-pipelines.yml

branch=$(Build.SourceBranch)
export branch=$(echo "${branch}" | sed 's/refs\/heads\///g')
0

I have a same needs, but I need to get the branch name in a bash script and run the script. Here is what I come up with:

# remove the "refs/heads/" part from the branch name
branch_name=${BUILD_SOURCEBRANCH#"refs/heads/"}

Notice that the predefined variable any.variable will be turned to env variable ANY_VARIABLE in the shell environment (source here). So the pre-defined variable Build.SourceBranch becomes BUILD_SOURCEBRANCH in bash script.

2
  • Note that the double-quote here is superfluous, and possibly confusing. This is called parameter substitution and exists in many different flavors. If you trigger on something that's not a branch, say a tag where the BUILD_SOURCEBRANCH starts with refs/tags/ then you're going to have to handle that differently
    – CervEd
    Commented Mar 6 at 16:16
  • to handle more than just refs/heads/ you should be able to use refs/*/ in the substitution instead
    – CervEd
    Commented Mar 6 at 16:20
0

Extending from @TSR's answer, which is the accurate one actually, what worked for me was:

variables:
  - group: production-shared-variables
  - name: branch_name
    value: $[replace(variables['Build.SourceBranch'], 'refs/heads/', '')]

if I had it as he put in the snippet i was receiving the following error

/azure-pipelines.yml (Line: 15, Col: 5): Unexpected value 'branch_name'

Then in my steps i to checkout the branch, i have the following

- task: CmdLine@2
  displayName: 'Checkout branch'
  inputs:
    script: |
      cd $(Build.SourcesDirectory)/my_project

      git fetch origin $(branch_name) && git checkout $(branch_name)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.