Adding a Fake Build to the Team Build Server

Why would anyone want to add a fake a build to the team build server? Well, there is one very big reason - Integration with Team System. If you don't use Team Build V1 to build your sources (for whatever reason), you may want to at least store some build information in the Team Build server. This allows you to publish test results, associate work items with builds, and view your build information from within Visual Studio.

I saw an older post by someone else that had most of the code. Unfortunately, they were missing the line at the bottom that actually Completes the build. Without this line you won't be able to associate work items with a build number. The code is commented and should be very straight forward. Note that in V1 this process requires several Web Service calls not just one.

So, here is the code:

 using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.Build.Proxy;
using Microsoft.TeamFoundation.Client;
using Common = Microsoft.TeamFoundation.Build.Common;

namespace AddFakeBuild
{
    class Program
    {
        static void Main(string[] args)
        {
            AddBuild("https://TeamBuildRTMSP1:8080", "MsfAgile", "FakeBuild001");
        }

        static void AddBuild(String serverName, String teamProject, String buildNumber)
        {
            // Get the TeamFoundation Server
            TeamFoundationServer tfs = new TeamFoundationServer(serverName);

            //Construct build store and build controller objects
            BuildStore bStore = (BuildStore)tfs.GetService(typeof(BuildStore));
            BuildController bController = (BuildController)tfs.GetService(typeof(BuildController));

            //Create a build entry
            BuildData bd = new BuildData();

            //Fill in mandatory information for BuildData object
            bd.BuildNumber = buildNumber;
            bd.BuildType = "DummyBuildType";
            bd.TeamProject = teamProject;

            // Make sure that this drop location exists, otherwise 
            // test publish will fail
            bd.DropLocation = @"\\MySharedMachine\drops\" + buildNumber;
            bd.BuildMachine = "NoBuildMachine";
            bd.RequestedBy = Environment.UserName;

            // These string values are locale dependent in V1
            bd.BuildStatus = Common.BuildConstants.BuildStatus.BuildSucceeded;
            bd.BuildQuality = "Not Examined";

            // Add build entry to TeamBuild DB
            bStore.AddBuild(teamProject, bd);

            // Get the URI of the build
            string buildUri = bStore.GetBuildUri(teamProject, buildNumber);

            // Create platform/flavor information against which the test 
            // results will be published
            ProjectData pd = new ProjectData();

            // Fill in mandatory information for ProjectData object
            pd.FlavourName = "Debug";
            pd.PlatformName = "x86";
            pd.ProjectFile = "Dummy.sln";

            // Add project data entry for this build.
            bStore.AddProjectDetailsForBuild(buildUri, pd);

            // Fill in the finish time
            bStore.UpdateBuildFinishTime(buildUri, DateTime.Now);

            // Complete the build and fire the BuildCompletion Event
            bController.BuildCompleted(buildUri);
        }
    }
}

In my next post, I will show you how to do this in Orcas!