{"id":185,"date":"2017-06-05T09:00:08","date_gmt":"2017-06-05T13:00:08","guid":{"rendered":"http:\/\/langstonsoftware.com\/?p=185"},"modified":"2024-01-30T19:09:16","modified_gmt":"2024-01-31T00:09:16","slug":"automatic-versioning-of-csharp-mvc-applications","status":"publish","type":"post","link":"https:\/\/langstonsoftware.com\/2017\/06\/05\/automatic-versioning-of-csharp-mvc-applications\/","title":{"rendered":"Automatic Versioning of C# MVC Applications"},"content":{"rendered":"
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\/<\/a><\/p>\n 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\/<\/a><\/p>\n 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.<\/p>\n I searched further and found the MSBuild Community Tasks<\/a> repo. However, I prefer PowerShell to MSBuild target XML “programming”.<\/p>\n 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<\/a>. For those new to Powershell, I’m using the backtick (`) as the string escape character.<\/p>\n$revision = $Env:bamboo_planRepository_revision\r\n$buildNumber = $Env:bamboo_buildNumber\r\n$text = "$revision - $buildNumber"\r\n$text > 'Version.txt'\r\n\r\n$assemblyText = "using System.Reflection;`n[assembly: AssemblyInformationalVersion(`"$text`")]" \r\n\r\n$assemblyText > 'SharedAssemblyInfo.cs'<\/pre>\n