Paul Hacker .Net, TFS, Etc…

September 17, 2013

TFS 2012 Build custom versioning build template

Filed under: Team System — phacker @ 7:08 pm

There are quite a few posts out there for creating a custom workflow that increments the version number so I am not walking through all the steps to do it, but rather why did it. I found the following posts as excellent resources to start with:

I started with Bryan’s blog and went from there. I had some requirements that I had to follow which is why I created a new one.

The requirements were:

1. Must write the version number to a file and not check in stamped assemblyinfo files

2. There must be a way to set a revision increment. Basically they needed to set a value to increase the revision number by. There strategy was even revision number for patches and odd for full installs.

3. I had to set an Environment variable with the new version number

The following is what I came up with for the Execute method:

protected override void Execute(CodeActivityContext context)


var sourcesDirectory = context.GetValue(SourcesDirectory);

Int32 major = context.GetValue(Major);

Int32 minor = context.GetValue(Minor);

Int32 build = context.GetValue(Build);

Int32 revision = context.GetValue(Revision);

Int32 revisionIncrement = context.GetValue(RevisionIncremnent);

string versionFileLocation = context.GetValue(VersionFileLocation);

var assemblyInfoFileMask = context.GetValue(AssemblyInfoFileMask);

var oldValue = “”;

//if there is no file then create it and add a default version number that uses the revision number we set in the build

//otherwise just get the current value

if (!File.Exists(versionFileLocation))


oldValue = “0.0.0.” + revision.ToString();





oldValue = File.ReadAllText(versionFileLocation);



// since all we need is the revision number lets get it.

var revisionNumber = oldValue.Split(‘.’);

int newRevision = int.Parse(revisionNumber[3].ToString()) + revisionIncrement;

//find all files that match the assembly mask we set

foreach (var file in Directory.EnumerateFiles(sourcesDirectory, assemblyInfoFileMask, SearchOption.AllDirectories))


string text = File.ReadAllText(file);

var newVersion = new Version(major, minor, build, newRevision );

// we want to find ‘AssemblyVersion(“”)’ etc

foreach (var attribute in new[] { “AssemblyVersion”, “AssemblyFileVersion” })


var regex = new Regex(attribute + @”\(“”\d+\.\d+\.\d+\.\d+””\)”);

var match = regex.Match(text);

if (match.Success) text = regex.Replace(text, attribute + “(\”” + newVersion + “\”)”);



System.Environment.SetEnvironmentVariable(“VersionNumber”, newVersion.ToString(), EnvironmentVariableTarget.User);

//update the version text file with new version number

File.WriteAllText(versionFileLocation, newVersion.ToString());



The code is pretty similar to what you see in the blogs i referenced.

I added the following arguments to the xaml file: RevisionIncrement and VersionFileLocation

RevisionIncrement – The value you want to increase the revision number by in each build

VersionFileLocation  – The path to the file were we will track the version.

When I use the template in a build definition it looks like so:


I also ran into a gotcha that the posts do not talk about.

The issue/solution are posted here.

Basically you have to update the xaml code as outlined in the post. Now you will need to add the line of code every time that you update the xaml workflow as every change you make to the xaml wipes out that line.

Hope this helps anyone looking to create a custom build workflow.


Blog at