Blog

"Knowledge has no value unless you use it and share it"

Git Tagging for .NET

A few years ago we moved to git from svn at the start of a new project. We had a lot of devs going. We also had several QA involved, and there was a lot of code being churned out quickly.  Different branches were undergoing QA and UAT at once, so it could take a few minutes to track down the specific version that was deployed.

So What Did We Decide to Do?

We decided to start using the git hash, (git rev-parse head) to tag the assemblies. This was, in addition, to using the standard branch-build number combination. At the beginning of the development process, we found that using the commit hash was more reliable than using branches or tags. This was especially so while the build environments were being changed frequently. Since then, having this piece of information living along with your code provides additional peace of mind.

In the first iteration, we simply wrote the information into some text files; the solution we’re providing here is a much better approach:

1. Link a GlobalAssemblyInfo.cs file to your assemblies, containing some standard parameters:

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany("Greenfinch Technology")]
[assembly: AssemblyCopyright("Greenfinch Technology 2014")]
[assembly: AssemblyTrademark("")]
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("git-hash-here")]
[assembly: AssemblyInformationalVersion("build-job-here")]

Since the Assembly Version is left alone, writing these properties will not break compatibility between assemblies. This means you can continue to use whatever strategy you like. Visual Studio will generate warnings about using these during the build process and unfortunately ignores attempts at suppressing them.

You can add more AssemblyInfo files to contain things like assembly names, GUIDs etc. if you need.

2. During the CI process, we set two parameters using powershell:


param(
    [Parameter(mandatory=$true)]
    [ValidatePattern('[^"]+')]
    [string]$version
)

# Jump up one directory up from where the script file is located
Push-Location ((Split-Path $MyInvocation.MyCommand.Path) + '\..')

function UpdateDevInfo([string]$fileLocation)
{
    $githash = [string](git rev-parse HEAD)

    $content = (get-content $fileLocation)

    $content = ForEach-Object {
        $content -replace "AssemblyFileVersion`\(`"[^`"]+'`\)","AssemblyFileVersion(`"$githash`")"
    }

    $content = ForEach-Object {
        $content -replace "AssemblyInformationalVersion`\(`"[^']+`"`\)","AssemblyInformationalVersion(`"$version`")"
    }

    Set-Content $fileLocation $content
}

UpdateDevInfo("path\to\assemblyinfofile1.cs")
UpdateDevInfo("path\to\assemblyinfofile2.cs")

Pop-Location

Assembly provides methods for retrieving the strings in the application. The linker time stamp can similarly be retrieved. We also found it useful to dump the full set of information into all log files on application startup. This helps to track version changes when going back through the logs.

 

A Final Word

At Greenfinch Technology we are always actively working to keep up with modern technology. In our opinion. to stand still is to fall behind. You can be sure we have no intention of letting that happen to ourselves or to our clients.

If you would like a modern, forward-thinking IT company to help with your custom enterprise software solutionsweb or mobile app development, a BPM such as Siskin or any Cloud technologies please feel free to contact us at 01 818 2949 or email us using our contact form. We will help guide you safely through the process of advancing your company beyond your competitors.

VIEW ALL BLOGS