How can I auto increment the C# assembly version via our CI platform (Hudson)?
Here's what I did, for stamping the AssemblyFileVersion attribute.
Removed the AssemblyFileVersion from AssemblyInfo.cs
Add a new, empty, file called AssemblyFileInfo.cs to the project.
Install the MSBuild community tasks toolset on the hudson build machine or as a NuGet dependency in your project.
Edit the project (csproj) file , it's just an msbuild file, and add the following.
Somewhere there'll be a <PropertyGroup>
stating the version. Change that so it reads e.g.
<Major>1</Major>
<Minor>0</Minor>
<!--Hudson sets BUILD_NUMBER and SVN_REVISION -->
<Build>$(BUILD_NUMBER)</Build>
<Revision>$(SVN_REVISION)</Revision>
Hudson provides those env variables you see there when the project is built on hudson (assuming it's fetched from subversion).
At the bottom of the project file, add
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')" />
<Target Name="BeforeBuild" Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')">
<Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)" />
<AssemblyInfo CodeLanguage="CS" OutputFile="AssemblyFileInfo.cs" AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" AssemblyConfiguration="$(Configuration)" Condition="$(Revision) != '' " />
</Target>
This uses the MSBuildCommunityTasks to generate the AssemblyFileVersion.cs to include an AssemblyFileVersion attribute before the project is built. You could do this for any/all of the version attributes if you want.
The result is, whenever you issue a hudson build, the resulting assembly gets an AssemblyFileVersion of 1.0.HUDSON_BUILD_NR.SVN_REVISION e.g. 1.0.6.2632 , which means the 6'th build # in hudson, buit from the subversion revision 2632.
A simple alternative is to let the C# environment increment the assembly version for you by setting the version attribute to major.minor.*
(as described in the AssemblyInfo file template.)
You may be looking for a more comprehensive solution, though.
EDIT (Response to the question in a comment):
From AssemblyInfo.cs
:
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
Here is an elegant solution that requires a little work upfront when adding a new project but handles the process very easily.
The idea is that each project links to a Solution file that only contains the assembly version information. So your build process only has to update a single file and all of the assembly versions pull from the one file upon compilation.
Steps:
- Add a class to you solution file *.cs file, I named min SharedAssemblyProperties.cs
- Remove all of the cs information from that new file
- Cut the assembly information from an AssemblyInfo file: [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]
- Add the statement "using System.Reflection;" to the file and then paste data into your new cs file (ex SharedAssemblyProperties.cs)
- Add an existing item to you project (wait... read on before adding the file)
- Select the file and before you click Add, click the dropdown next to the add button and select "Add As Link".
- Repeat steps 5 and 6 for all existing and new projects in the solution
When you add the file as a link, it stores the data in the project file and upon compilation pulls the assembly version information from this one file.
In you source control, you add a bat file or script file that simply increments the SharedAssemblyProperties.cs file and all of your projects will update their assembly information from that file.