I recently had a project that had me delving into the following blog post on assembly versioning: https://blogs.msdn.microsoft.com/jjameson/2009/04/03/best-practices-for-net-assembly-versioning/
The blog post had a couple of problems. The first that came to mind was that the linked examples were using Team Foundation Server, while I was using Atlassian Bamboo. The second is that it seemed like a lot of work, especially when dealing with a large number of existing projects in a legacy solution all churning version numbers. Jumping back into Google I found a new blog post: https://blogs.msdn.microsoft.com/jjameson/2009/04/03/shared-assembly-info-in-visual-studio-projects/
This seemed better, but I was not sure that putting all the assembly version information into one shared file is correct either. It seems like we’d be doing fine keeping our dlls at the default 1.0.0.0 version, I just wanted to tag the dlls with our Git commit SHA. Otherwise our repo would churn on build. Also, I’d prefer to do a full replace during my build on a file that only contains the information I want to change across my entire repo, leaving the rest alone. So I decided that modifying the AssemblyInformationalVersion in a CI script made the most sense for what I was trying to accomplish.
I searched further and found the MSBuild Community Tasks repo. However, I prefer PowerShell to MSBuild target XML “programming”.
Example below is using Bamboo build environment variables, but can easily be tailored to your CI build system. For those who know Bamboo, you might notice the Bamboo variables look a little different in PowerShell. For those new to Powershell, I’m using the backtick (`) as the string escape character.
$revision = $Env:bamboo_planRepository_revision $buildNumber = $Env:bamboo_buildNumber $text = "$revision - $buildNumber" $text > 'Version.txt' $assemblyText = "using System.Reflection;`n[assembly: AssemblyInformationalVersion(`"$text`")]" $assemblyText > 'SharedAssemblyInfo.cs'
You can add the Version.txt file as an ignored route in MVC if you’d like it to be served by ASP.NET. I created two dummy files in the top of my repo that whose contents would be replaced by this script. I included these shared files in all the projects by linking to them. I did move my dummy SharedAssemblyInfo.cs file into the Properties folder of my projects so it could sit side-by-side with the default AssemblyInfo.cs file. Now I can either look at Version.txt from a browser, or right click the dll directly on the machine to see the “Product Version” under the Details tab of the Properties modal.
Before You Go
Was this post was incorrect or incomplete? Leave a comment.
Was this post helpful to you? Subscribe for emails on full stack web development.