0

There is an environment variable in TFS 2013 for Branch name. However my company still uses TFS 2012.

Does anyone know how I can get the branch name during build with TFS 2012?

Is there a class with the branch name property in the TFS Build api?

I haven't found one yet.

Thank you in advance

user5855178
  • 567
  • 1
  • 7
  • 17

1 Answers1

0

You can access the paths that are configured for this build through the IBuildDetails.

Just use the VersionControlServer class to query the BranchObjects, then check which one is the root for your working folder.

Sample Code:

IBuildDetail buildDetail = context.GetExtension<IBuildDetail>();

 var workspace = buildDetail.BuildDefinition.Workspace;
 var versionControlServer = buildDetail.BuildServer.TeamProjectCollection.GetService<VersionControlServer>();

 var branches = versionControlServer.QueryRootBranchObjects(RecursionType.Full);
 var referencedBranches = listOfFilePaths.GroupBy(
                file =>
                branches.SingleOrDefault(
                     branch => file.ServerItem.StartsWith(branch.Properties.RootItem.Item)
                )
            ).Where(group => group.Key != null);

More details please refer jessehouwing's reply in this question: Include branch name in post build event on Team Build

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62