Git short branch name in teamcity

I found that if you have one branch set up in the VCS, then %teamcity.build.branch% is not defined. And if you have multiple branches defined, then %vcsroot.branch% only reports on the the name of the default branch.

I ended up using a git command to give the current branch name.

Powershell:

$branch = (git symbolic-ref --short HEAD) | Out-String

Command Line:

FOR /F "tokens=* USEBACKQ" %%%%F IN (`git symbolic-ref --short HEAD`) DO (
SET branchName=%%%%F
)
ECHO %%branchName%%

TeamCity converts all %% -> % so that's why there are so many %

Set as environment variable

If you would like to use the branch name as an environment variable to be used in other steps of your project you can set the branch name by using Powershell as an example. First define env.currentBranch in your build configuration and then set it with the following powershell as your first build step.

$branch = (git symbolic-ref --short HEAD) | Out-String
Write-Host "##teamcity[setParameter name='env.currentBranch' value='$branch']"

I believe what you need is another variable. Try using %vcsroot.branch%. There is also %teamcity.build.branch%, but that one will contain "<default>" on the default branch. If you want more flexibility to choose exactly which part of the branch name gets selected, you can follow the instructions on this page:

http://confluence.jetbrains.com/display/TCD7/Working+with+Feature+Branches#WorkingwithFeatureBranches-branchSpec.