Recording the git commit hash in your assembly with TeamCity

If you’re using SVN, you can very easily configure TeamCity to insert the commit revision number into the assembly by using the built-in AssemblyInfo Patcher with a pattern like 1.0.0.%build.vcs.number%. This works because SVN and .NET both use integers for their version numbers.

Git, on the other hand, uses a 40-character SHA-1 string as a unique ID for commits. As you can see, this will not fit in a .NET assembly version:

Example git commit: c9d183c8570143142ca61c555360e7f0732efc09Example git commit (short version): c9d183c857Example .NET assembly version:

One place it will fit is the AssemblyInformationalVersion attribute, which accepts any string (thanks Joshka for the tip!). Here’s a quick MSBuild script to do it (using the AssemblyInfo task from MSBuild Community Tasks).

<Project ToolsVersion="4.0" xmlns="">  <PropertyGroup>    <MSBuildCommunityTasksPath>packagesMSBuildTasks.</MSBuildCommunityTasksPath>        <!-- Use the short version of the git hash. -->    <GitCommitShortHash>$(build_vcs_number.Substring(0, 7))</GitCommitShortHash>  </PropertyGroup>  <Import Project="$(MSBuildCommunityTasksPath)MSBuild.Community.Tasks.Targets"/>  <Target Name="GenerateAssemblyInfo">    <AssemblyInfo CodeLanguage="CS"      OutputFile="MyAppPropertiesAssemblyInfo.cs"      AssemblyInformationalVersion="$(GitCommitShortHash)" />  </Target></Project>

Then in C# you can use something like the following to display it on startup or your About page:

public void Main(){    var gitCommit = Assembly.GetExecutingAssembly()        .GetCustomAttributes(inherit: false)        .OfType<AssemblyInformationalVersionAttribute>()        .Single().Configuration;    Console.WriteLine("My App starting up (git commit {0})", gitCommit);    ...}

(Note Josh Flanagan has an alternative approach where he finds a way to convert the git commit into an int so you can use it in the AssemblyVersion).