Use Branch Name in TFS 2015 Automated Build

2019-08-19 08:58发布

问题:

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?

回答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.



回答2:

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

example build:

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



标签: tfs tfsbuild