Dominic Burford - Professional Profile
|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.
dotnet build MyProject.csproj --configuration Release /p:Version=%1The %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 126.96.36.199 then right-clicking on the assembly (or EXE) in the bin folder will show a version number of 188.8.131.52. 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.
</Project>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.
Write-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
"Version number has been udated from " + $oldversionNumber + " to " + $Env:BUILD_BUILDNUMBER
Write-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.
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.