Git short branch name in teamcity

23,316

Solution 1

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.

Solution 2

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']"
Share:
23,316
Ömer Faruk Aplak
Author by

Ömer Faruk Aplak

Updated on July 09, 2022

Comments

  • Ömer Faruk Aplak
    Ömer Faruk Aplak almost 2 years

    I'm using teamcity 8.x.x version.I configured my Teamcity for continuous deployment. I'm need a feature branching deployment. I see this document "http://confluence.jetbrains.com/display/TCD8/Working+with+Feature+Branches".

    I'm trying this document implementing on my Teamcity. I have a problem.

    My deployment config use "OctoPack" (nuget). My nuget package needs build count and branch name. example: 1.0.0.356-feature-1.

    I'm try this versioning,

    %build.number%-%teamcity.build.vcs.branch.VCS_ROOT_ID% ----> 1.0.0.356-refs/head/feature-1

    this version not support nuget versioning. nuget not comparative "/".

    I need this,

    %build.number%-%teamcity.build.vcs.SHORT_BRANCH_NAME.VCS_ROOT_ID% ---> 1.0.0.356-feature-1

    how can I ?

    Thanks

    • Lars Stenberg
      Lars Stenberg about 10 years
      Can you share how you solved this?
    • Ömer Faruk Aplak
      Ömer Faruk Aplak about 10 years
      Hello, try this, %build.number%-%teamcity.build.branch%
    • bigbearzhu
      bigbearzhu over 7 years
      @LarsStenberg, I know this maybe too late, but I was thinking the same question when I saw the accepted answer. So I add this comment if anyone else need help in the future. Actually what you need is the branch specification to give you the correct logical branch name. This is the clearly mentioned here: confluence.jetbrains.com/display/TCD10/…. Then you can use %teamcity.build.branch% as it is basically the logical branch name.
    • JamesQMurphy
      JamesQMurphy over 5 years
      @bigbearzhu Can you please add that as an answer? I'd do it myself but you should get the credit for it.
  • chester89
    chester89 about 10 years
    thank you very much, been looking everywhere for this
  • starmandeluxe
    starmandeluxe over 6 years
    Actually it's %teamcity.build.branch%. %vcsroot.branch% just gives you whatever the default is set to, which is usually much less useful than what most people are looking for.
  • Mr. Girgitt
    Mr. Girgitt almost 5 years
    to deal with the <default> branch name - if one wants to use branch name e.g. as a part of the zip file artifacts would go to - there is a useful post here: stackoverflow.com/questions/42922880/…
  • AndyMoose
    AndyMoose almost 4 years
    Honestly don't know why to this date this isn't built-in to TC