As part of our build process, I needed to version the assembly with the latest build number. This is the first step in the build pipeline. Initially, I investigated doing this using the dotnet command as below.
Hide Copy Code
dotnet build MyProject.csproj --configuration Release /p:Version=%1
The %1 parameter is the latest build number and is passed into the script via a build step. This command will build the project using the arguments that have been specified and create the build articles in the bin folder. The built assembly that the command has created in the bin folder will correctly have the version number set as per the command. So if %1 has been set to 188.8.131.52 then right-clicking on the assembly (or EXE) in the bin folder will show a version number of 184.108.40.206. All of this works exactly as it should.
The problem I was having however, is that as part of our release pipeline, I deploy the web application to our Azure hosting. To deploy to Azure using the Azure deploy task you need to create a .zip file. I create the .zip file using an MSBUILD command. I don’t create the .zip file from the previous dotnet build command. The MSBUILD command uses the current project files and creates the .zip file from them. Therefore the .csproj file needs to have the correct version number before I run the MSBUILD command which then creates the .zip file.
I therefore needed to find a way to update the version number in the .csproj file before I ran the MSBUILD command. That way the version number of the assembly that gets deployed to our Azure hosting will have the correct version number.
Here is part of a .csproj file showing the version number.
Hide Copy Code
<!--Need to update the version number below-->
I began to investigate how to update the version number within the .csproj file directly. I thought I could implement a simple PowerShell (PS) script that would do this. Also, by updating the version number directly in the .csproj file (and it being the first step in the build pipeline) then every subsequent step that referenced the version number would be referencing the correct version number.
After some Googling and trial-and-error I came up with the following PS script.
Hide Copy Code
clsWrite-Host "Versioning started""Sources directory " + $Env:BUILD_SOURCESDIRECTORY
"Build number " + $Env:BUILD_BUILDNUMBER$csprojfilename = $Env:BUILD_SOURCESDIRECTORY+"\MyProject.csproj"
"Project file to update " + $csprojfilename[xml]$csprojcontents = Get-Content -Path $csprojfilename;"Current version number is" + $csprojcontents.Project.PropertyGroup.Version$oldversionNumber = $csprojcontents.Project.PropertyGroup.Version$csprojcontents.Project.PropertyGroup.Version = $Env:BUILD_BUILDNUMBER
$csprojcontents.Save($csprojfilename)"Version number has been udated from " + $oldversionNumber + " to " + $Env:BUILD_BUILDNUMBERWrite-Host "Finished"
The environment variables $Env:BUILD_SOURCESDIRECTORY and $Env:BUILD_BUILDNUMBER are both provided by the build process as part of the Microsoft build environment. You don’t need to do or set anything to have access to these. They are provided by the build environment straight out the box for free. In fact, there are many more such variables that are also available that you may find useful in your other build scripts and processes.
The PS script fetches the latest build number and the folder path of where the source files are located on the build server. It then reads the contents of the .csproj file as an XML document. The version number is set to the latest build number (as provided by the build environment) and then closes and saves the updated .csproj file. That’s it. That’s all you need to do to update the version number in your .csproj file.
I initially thought that this would involve some horrible string search and replace to update the .csproj file. Thankfully, PowerShell contains native support for manipulating XML files, and it was in fact much easier than I thought. So if you need to update your .NET project’s .csproj version number, feel free to use this script.