Wednesday, May 18, 2011

Setting assembly version automatically when building in a Bamboo server

Scenario :
How to instruct your msbuild script to set the correct “AssemblyVersion” of your libraries using “AssemblyInfo.cs”  when building in a Bamboo server.In Puzzlepart it is vital to have the current build number and the current revision number in the assembly version of products.
Solution :
Assembly version syntax : <major version>.<minor version>.<build number>.<revision>
More info can be found here about the syntax. 
Step 1 : First you need to make sure the corresponding Build-Specific Variable are passed into the build script.
How ?
In Bamboo build configuration under the “Builder” tab you have “Options” where you can pass msbuild command line switches.
example : /p:BuildKey=${bamboo.buildKey} /p:BuildPlanName=${bamboo.buildPlanName} /p:BuildNumber=${bamboo.buildNumber} /p:Revision=${bamboo.custom.svn.revision.number}
image
Step 2 : Accessing those variables from the msbuild script
<PropertyGroup>
    <MajorVersion Condition="'$(MajorVersion)' == ''">1</MajorVersion>
    <MinorVersion Condition="'$(MinorVersion)' == ''">0</MinorVersion>
    <BuildNumber Condition="'$(BuildNumber)' == ''">0</BuildNumber>
    <Revision Condition="'$(Revision)' == ''">0</Revision>
    <BuildPlanName Condition="'$(BuildPlanName)' == ''">LocalBuild</BuildPlanName>
    <BuildKey Condition="'$(BuildKey)' == ''">NOKEY</BuildKey>


  </PropertyGroup>
Step 3 : Create the Assembly version using those properties
<PropertyGroup>
       <ComputedAssemblyName>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(Revision)</ComputedAssemblyName>
</PropertyGroup>
Step 4 : Use the assembly version in “AssemblyInfo” task
<AssemblyInfo CodeLanguage="CS"
      OutputFile="$(ApplicationName)\Properties\AssemblyInfo.cs"
      AssemblyTitle="ApplicationName"
      AssemblyDescription="Description"
      AssemblyCompany=""
      AssemblyProduct="$(ApplicationName)"
      AssemblyCopyright="Copyright © "
      ComVisible="false"
      CLSCompliant="false"
      Guid="248D49F7-BC5D-4413-8E4A-98B4654B1594"
      AssemblyVersion="$(ComputedAssemblyName)" />

2 comments:

Unknown said...

Hello
I'm interesting to use this feature with a Atlassian Bamboo server 4.1 and VisualStudio 2010. I'm puzzeling where the place the data you have described in your blog.

The bamboo stuff is fine for me; but must I enter the property groups in each VS project file? And where should I enter the assembly info task stuff?

Thanks so far. Kind regards
Michael

Indika Rathnasekara said...

Hi Michael
Sorry for the late reply.I have used a msbuild script and there you can define your properties and build targets.I hope you are familiar with msbuild scripts.http://msdn.microsoft.com/en-us/library/ms171452%28v=vs.90%29.aspx