Include branch name in post build event on Team Build

1.5k views Asked by At

I would like to perform the following steps in the TFS build process:

  • do post build event that will copy some files from my compiled projects to another predefined directory, I'd like that directory path to include the branch name.
  • I'd like to be able to refer to the branch name inside my xaml workflow template as well.
1

There are 1 answers

5
jessehouwing On

The first one is rather simple. When you're using the new TFS 2013 build server and process template, you can simply add a post-build powershell script in the Build Definition Configuration, check in the script and run it during the build.

The second one is dependent on whether you're using TFVC or Git, in the first case, use the VersionControlServer class to query the BranchObjects, then check which one is the root for your working folder. Be aware though, that in TFVC multiple branches can be referenced in one workspace, so there may be multiple answers to this query, depending on which file you use the find the branchroot. A custom CodeActivity would do the trick, similar to this check in a custom checkin policy.

The code will be similar to:

 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);

To get a list of all items in yo workspace, you can use Workspace.GetItems.

In case you're using Git, you have a few options as well. The simplest is to invoke the command line:

 git symbolic-ref --short HEAD   

or dive into LibGit2Sharp and use it to find the branch name based on the current working folder from a custom activity.

If you want to include this in an MsBuild task, this may well be possible as well. It goes a bit far for this answer to completely outline the steps required, but it's not that hard once you know what to do.

Create a custom MsBuild task that invokes the same snippet of code above, though instead of getting access to the workspace through BuildDetail.BuildDefinition.Workspace, but through the WorkStation class:

Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace workspace = info.GetWorkspace(collection);
VersionControlServer versionControlServer = collection.GetService<VersionControlServer>();

Once the task has been created, you can create a custom .targets file that hooks into the MsBuild process by overriding certain variables or copying data when the build is finished. You can hook into multiple Targets and define whether you need to do something before or after them.

You can either <import> these into each of your projects, or you can place it in the ImportAfter or ImportBefore folder of your MsBuild version to make it load globally. You can find the folder here:

C:\Program Files (x86)\MSBuild\{MsBuild Version}\Microsoft.Common.Targets\ImportAfter