4

I've got TeamCity building a configuration across multiple Mercurial branches. As part of the build process, I want to name some output based on the branch being built from.

I've tried using $(TEAMCITY_BUILD_BRANCH), to try to obtain the teamcity.build.branch property from TeamCity, but I only get an empty string. I successfully used $(BUILD_NUMBER) to get the build.number property into the script, so I'm a little confused to what is required.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166

2 Answers2

5

Just create a build parameter:

system.branch_name = %teamcity.build.vcs.branch.Your_Project%

Any non-alpha-numerical chars in the project name should be replaced by "_". In your msbuild use $(branch_name) to reference the parameter.

Works like charm :-)

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
  • tried it - it gets \refs\head\whatever, where 'whatever' is an actual branch name – chester89 Feb 21 '14 at 11:22
  • @chester89 You can use brackets to specify name submatch: for example `refs/head/(master)`, `refs/head/release/(*)` or `refs/head/(feature/*)` ... to get "master", "1.2.3", and "feature/some-name" respectively. – gregmac May 22 '14 at 16:32
  • while using the brackets around the branch name made only the branch name (without "refs/head/") appear in the TeamCity UI, the %teamcity.build.vcs.branch.Your_Project% parameter still gets resolved to a value that includes refs/heads. I'm on version 8.0.6 of TeamCity. – Dav Evans Feb 04 '15 at 23:42
  • @Alexander Marinov: How can I pass the branch name to a script that needs to be run from teamcity as a build step – Nevin Raj Victor Jun 03 '15 at 06:30
2

$(vcsroot_url) if you have only one vcs root attached to build configuration.

If this is not what you want - try to get all known properties with as described here: http://confluence.jetbrains.net/display/TCD7/Predefined+Build+Parameters

Reference-only Server Properties:

You can get the full set of reference-only server properties by adding system.teamcity.debug.dump.parameters property to the build configuration and examining "Available reference-only server properties" section in the build log.

UPD: Also you can check this "what's new" link.

VCS branch parameter For Git and Mercurial, TeamCity provides additional build parameters with names of VCS branches known at the moment of build starting. If a build took a revision from the refs/heads/bugfix branch, TeamCity will add a configuration parameter with the following name: teamcity.build.vcs.branch.<simplified VCS root name>=refs/heads/bugfix

Where is the name of the VCS root where all non-alpha numeric characters are replaced with _.

According to this - you should be able to access branch as following msbuild variable $(teamcity_build_vcs_branch_).

Hope this time I understood your issue properly.

PS: Sorry for initial misguiding - I didn't know about such subtle differences between DVCS and CVCS in TeamCity. Anyway - my initial answer could also help you - you can get all variables and then find exact var name with proper data you want

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Alexey Shcherbak
  • 3,394
  • 2
  • 27
  • 44
  • @Alexy I'm using Mercurial, so the branch name isn't a URL, but a name within the repository. I don't quite understand what is required to reference the given build parameter in an MSBuild script. – Paul Turner Oct 11 '12 at 14:25
  • If you build with TeamCity - it will emit some of it's own environment variables to msbuild context, and you can access it like it your own previously defined properties. It's same way BUILD.NUMBER from TeamCity become $(BUILD_NUMBER) available to your msbuild script. Teamcity usually emits many usefull info - you can check docs for full details. As soon as you asked about accessing branch name - I thought that you can extract it from vcs url. Unfortunately - I don't have Teamcity with Mercurual as VCS, only svn here. I found some more clarifications on that and will append it to my answer – Alexey Shcherbak Oct 11 '12 at 15:26
  • I've tried `$(teamcity_build_vcs_branch_)`, both upper and lower case, with and without the trailing underscore: none of these have worked, whilst $(BUILD_NUMBER) still functions. – Paul Turner Oct 25 '12 at 16:54
  • just noticed - last part of variable was eaten by sanitizer $(teamcity_build_vcs_branch_your-vcsroot-name-here). Also "You can get the full set of reference-only server properties by adding system.teamcity.debug.dump.parameters property to the build configuration and examining "Available reference-only server properties" section in the build log." – Alexey Shcherbak Oct 25 '12 at 18:35