Add the build number to websites automatically

I always add the build number to websites automatically in my continuous integration environment. This allows developers and testers to ensure which build they are testing.

Jenkins Build Number To review, my continuous integration system always does the following:

  1. Build all the code from a clean environment
  2. Report on code duplication
  3. Recreate the test database from scratch
  4. Deploy all services (including services and websites)
  5. Run all unit tests
  6. Report on unit test coverage

Automatically deploying services and websites has always been easy using installutil and msbuild, respectively.

I wrote a simple tool, JenkinsTitleUpdater which I run before the deployment step in my build system. This is included in the deploy step of the msbuild script.

<Target Name="DeployCI">
    <Message Text="================= Deploying CI ================"></Message>
        <Exec Command="JenkinsTitleUpdater.exe ..\src\Web\EHRProfiler\Views\Shared\_Layout.cshtml"></Exec>
    <MSBuild Projects="$(MainSln)" Properties='DeployOnBuild=true;PublishProfile=EHRProfilerTest;Configuration=Test;UserNam=;Password=' />
</Target>

The JenkinsTitleUpdater uses the HtmlAgilityPack. It allows XmlDocument-style navigation of HTML pages. This example updates the _Layout.cshtml page to append the Jenkins build number.

HtmlDocument doc = new HtmlDocument();
if (!File.Exists(args[0])) throw new ArgumentException("File does not exist " + args[0] + "\r\nWe are at " + Environment.CurrentDirectory);

doc.Load(args[0]);

HtmlNode root = doc.DocumentNode;

HtmlNode titleNode = root.ChildNodes["html"].ChildNodes["head"].ChildNodes["title"];

string buildname = " -- ";

if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BUILD_DISPLAY_NAME")))
{
    return -1;
}

buildname += Environment.GetEnvironmentVariable("BUILD_DISPLAY_NAME");

titleNode.InnerHtml += buildname;

doc.Save(args[0]);

Source code and binaries are available here : JenkinsTitleUpdater.