Menu Menu
DLL versioning using Git commit ID

DLL versioning using Git commit ID

Whenever we have to debug our live web application, deployed on an IIS web server, it's always helpful to know what version of the source code was used to build that version of the web app, because it allows us to inspect that particular version of the code on our local development environment, if there are any issues to be fixed. One of many ways to accomplish this is to embed the ID of the last git commit, used to build that version of the web app, within the VersionInfo resource of our web app DLL files. An example screenshot below shows how a version info could look like when implemented this way:

Example version dialog of a DLL

When you right-click a dll file in Windows Explorer and switch to the "Details" tab, you can see the VersionInfo data conveniently displayed. One of the fields displayed there is the "Product version", which we can use to store the last git commit id into. The idea is to configure Visual Studio to embed this data, during the build process, by setting up a pre-build event, which would update the Properties/AssemblyInfo.cs file, and set the correct git commit id into the "Product version" field.

So, open your solution in Visual Studio and, in the Solution Explorer, right-click your project and choose Properties:

Example version dialog of a DLL

When a new window displays, switch to the "Build Events" tab, as shown in the following screenshot:

Example version dialog of a DLL

Now, assuming we have git installed and we added a path to the git's "bin" folder to the global PATH environment variable (which should happen automatically, during the installation of git), we should type in the following powershell commands inside the "Pre-build event command line" text box:

powershell -Command "(gc $(ProjectDir)\Properties\AssemblyInfo.cs) -replace 'assembly: AssemblyInformationalVersion\(".*"\)',('assembly: AssemblyInformationalVersion(\"git-{0}\")' -f (git rev-parse --short HEAD)) | Out-String | Out-File -Encoding UTF8 $(ProjectDir)\Properties\AssemblyInfo.cs"

powershell -Command "(gc -Raw $(ProjectDir)\Properties\AssemblyInfo.cs) -replace '(\r\n){3,}','$1' | Out-File -Encoding UTF8 $(ProjectDir)\Properties\AssemblyInfo.cs"

The first PS command replaces the value of the "AssemblyInformationalVersion" field, in the file Properties/AssemblyInfo.cs, with the last git commit id, using the command "git rev-parse --short HEAD". The second PS command handles the excessive new lines at the end of the file, introduced by Power Shell in the first command (if you find a better solution for this, please let me know and I'll update this article).

At this moment, you can try to build your solution and take a look in the bin/ folder to see if your dll files now have the git commit id in their VersionInfo.

Latest blog posts
ITkonekt 2019 Overview
Front-End Day 2019: Our Impressions
Workshop with Uncle Bob
International Umbraco Conference in Novi Sad: Umbraco Roadshow 2019
How To Send an Email with Attachments Using EPiServer Forms