0

My TFS repo has the following structure:

Project
 - Dev
    - 1.0.0_Branch1
    - 1.1.0_Branch2
    - N.0.0_BranchN

The branches are actual branches in TFS.

I have an automated CI build set up for this project on a TFS server. The issue I am having is that I need the build name in TFS to use the name of the branch.

I have tried a number of the build definition variables listed here , for examle $(SourceBranchName) and and $(Build.SourceBranchName) but all of these are instead using the name of the Project (Project in this example).

I would like the build to be named along the lines of 1.0.0_Branch1.1 with the last number being the revision.

The reason I would like to do this is so that when I generate Nuget packages at the end of the build, they can be versioned using the version of the branch that is being built. Currently they are being versioned using the date which means that the highest version is only ever the most recently built, which may not be the case in practice. I don't want to have to manually set the versions each time.

All assemblies that are being built are correctly versioned to match the branch that they are within.

Is there a way to achieve this?

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
ChrisW
  • 83
  • 3
  • 11

2 Answers2

3

For build definitions, $(SourceBranchName) can be used in the build number format:

usage of SourceBranchName example build: build performed with SourceBranchName

The environment variable is BUILD_SOURCEBRANCHNAME so it will be available as $(BUILD_SOURCEBRANCHNAME) in msbuild.

The list of available variables is found at: https://www.visualstudio.com/en-us/docs/build/define/variables#predefined-variables

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • This is what I am using (as $(Build.SourceBranchName)) but it does not work as expected. It is always only listing the Project name instead of the actual branch. – ChrisW May 19 '17 at 16:12
  • in msbuild? or build definition? I think it is unclear **what** you are trying to name – Martin Ullrich May 19 '17 at 16:18
  • This is in the build definition. I have tried a number of build number formats without success. The most recent being $(Build.SourceBranchName)$(Rev:.r) which is creating a build called Project.1 when I would expect it to create 1.0.0_Branch1.1 – ChrisW May 19 '17 at 16:22
  • Added screenshots of example usage – Martin Ullrich May 19 '17 at 16:30
  • Apologies for the delay in replying. Here is a screenshot of my setup: http://imgur.com/a/Z9q93 When I run a build I get it named Project.1 – ChrisW May 23 '17 at 10:39
  • Just $(Branch) is also a useful variable. Holds - as the name suggests - the name of the branch. – Vincent65535 Sep 22 '20 at 19:01
1

This could be caused by the "Mappings" setting you configured under "Repository" tab in your build definition. When you build with TFVC repository, the BUILD_SOURCEBRANCHNAME variable is filled with the last path segment in the root server path of the workspace. So I'm wondering that the mapped server path in your build definition is "$/Project" rather than "$/Project/Dev/1.0.0_Branch1".

TFVC repo branch: The last path segment in the root server path for the workspace. For example in $/teamproject/main this value is main.

Refer to this link for details: Predefined Variables.

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • That is exactly it! What I have been misunderstanding here is at what level a build definition should be targeted. I was working on the assumption that one build definition could cover all branches in a project but only run against a given branch. I guess I will need to create a new build definition for each branch and map it directly to that branch's folder? – ChrisW May 26 '17 at 12:24