What is DLL version using Git commit ID?

Mladen Bukejlovic Date 23-Feb-2018 5 minutes to read
DLL Versioning Using Git Commit ID

Table of contents

    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:

    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:

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

    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.

    mladen-bukejlovic-_authorsphoto.png
    Mladen Bukejlovic Software Developer

    Mladen likes to consider himself as a problem solver, rather than a software developer. He's always looking for an interesting problem to solve, no matter which realm it comes from. He believes that solutions for problems from one realm can usually be adapted and applied to problems from a completely different one, which, as a result, helps to grow the overall experience of a person a lot faster.