Coding Range

Queueing TFS Builds En Masse

April 2nd, 2014

Today I was trying to set up a stress-test for a Team Foundation Server build. What I would like for it to do is just run rolling builds until I arrive at work tomorrow to go over the logs and see how the test went.

Unfortunately, TFS’s rolling builds trigger doesn’t seem to trigger a build if there are no check-ins since the previous build started. The logical next options was to just queue a whole bunch of builds that would last roughly until tomorrow morning.

So what I could have done is go get a calculator1, figure out how many minutes there are until early tomorrow morning, then figure out how many 15-minute builds fit into that time and repeat the process of manually queueing a build N times until there are enough builds in the build queue to last until tomorrow morning.

Or being a programmer, I could script it. Microsoft provide a rather poorly documented but fully-featured .NET library for accessing TFS, which provoved useful.

using System;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;

namespace BuildMassQueue
{
         class Program
         {
                 static void Main(string[] args)
                 {
                          var collection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://my.tfs.server:8080/tfs/MyCollection"));
                          var buildServer = collection.GetService<IBuildServer>();
                          var buildDefinition = buildServer.GetBuildDefinition("MyTeamProject", "MyBuildDefinition");

                          // 8AM tomorrow morning
                          var endTime = new DateTime(2014, 04, 03, 08, 00, 00);
                          var startTime = DateTime.Now;
                          var difference = endTime - startTime;
                          var numBuildsThatCanFit = (int)(difference.TotalMinutes / 15);

                          Console.WriteLine("About to queue {0} builds. Press any key to continue...", numBuildsThatCanFit);
                          Console.ReadKey();

                          for (int i = 0; i < numBuildsThatCanFit; i++)
                          {
                                   var request = buildDefinition.CreateBuildRequest();
                                   buildServer.QueueBuild(request);
                          }
                          Console.WriteLine("Done!");
                 }
         }
}

The API documentation is pretty miserable, but once you get past that it’s surprisingly simple.


  1. Shameless plug for Numerical, the best calculator app I’ve ever used.