How to get a branch name with a slash in Azure DevOps?

13,090

Solution 1

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.

Solution 2

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/", ""

Share:
13,090

Related videos on Youtube

hey
Author by

hey

Updated on June 12, 2022

Comments

  • hey
    hey almost 2 years

    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?

    • hey
      hey over 4 years
      It is so hard to decide which one is more correct lol
    • PatrickLu-MSFT
      PatrickLu-MSFT over 4 years
      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.
  • hey
    hey over 4 years
    Do you have some additional logic for cases where you build on a PR or a tag?
  • Chris
    Chris almost 2 years
    Yes, but you must use Build.SourceBranch !!! The var in your example SourceBranchName only returns the last path segment in the ref.
  • lvilasboas
    lvilasboas almost 2 years
    Aside from the error that @Chris pointed out, this should be the accepted answer