0

Automatic CI Versioning using TeamCity and MSBuild

by Jon 4. October 2010 20:33

If you read my first post on getting started with TeamCity, and my follow up post on moving to MSBuild you will have a lovely CI system that builds your solution each you commit your changes.  Whilst this sounds fantastic, unfortunately of the assemblies that are built will all be versioned with the same version that is pulled from the AssemblyInfo.  Luckily it isn't a major change to get MSBuild to update your solution files with the version number stored in TeamCity every time a build occurs by tweaking your MsBuild File: (and installing MsBuild Community Tasks)

<?xml version="1.0" encoding="utf-8" ?> 
<Project  DefaultTargets="ReleaseBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

    <Target Name="ReleaseBuild">

        <Message Text="Set the correct $(BUILD_NUMBER)" /> 
	<FileUpdate Files='AssemblyInfo.vb' 
	  Regex='Version\("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"\)' 
	  ReplacementText='Version("$(BUILD_NUMBER)")' />

	<FileUpdate Files='AssemblyInfo.cs' 
	  Regex='Version\("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"\)' 
	  ReplacementText='Version("$(BUILD_NUMBER)")' />

        <Message Text="Building"/> 
        <MSBuild Projects="ProjectName.sln" Targets="Rebuild" /> 

    </Target>
</Project>

 

Shared Solution Level Assembly InfoThis change uses the FileUpdate Task in MsBuild Community Tasks to be performed on the two specified files, AssemblyInfo.vb and AssemblyInfo.cs.  The Regex will search for the word Version, followed by a valid four digit string version, and replace it with the build version configured in team city.

I use a Solution wide AssemblyInfo file called SolutionAssemblyInfo.vb or SolutionAssemblyInfo.cs to standardise Assembly Information across the solution by using a single shared file.  You can Create this shared file in Visual Studio by Adding a New Item to the Solution Directly, and then after you have created it you link it into each project by clicking on Add existing in each project and pressing the Arrow on the 'Add' Button and selecting 'Add as Link' instead.  This Shared Solution wide file contains the assembly info that is shared across the assemblies, each project will contain the link to shared assembly info file, and the existing assembly info file in the My Project folder.

Tags: , ,

ddd | development | MSSQL Server | Versioning

0

Using MsBuild with TeamCity, (dotnet 3.5). Look Ma no Visual Studio!

by Jon 12. September 2010 01:08

I am currently moving our TeamCity to a different Virtual Server using this post by Mikael Henriksson.  Its is a fresh server install I thought it would be a great excuse to blog the steps you need to take to get MsBuild Working on your TeamCity.  In my previous post I described how to get started with Team City in under 5 Minutes, this short post follows on from that.

MsBuild gives you more flexibility when compiling your projects/solution.  However it isn't especially clear from documentation how you get your server configured to use MSBuild, or even what a basic MsBuild file will look like.  I got there painfully through trial and error, if you want to get up and running with MSBuild in TeamCity without having to install Visual Studio 2008 at all on your server follow the following six steps:

1. Install the MsBuild Community Tasks via the Msi Installer

2. Download an Install Windows SDK for Windows Server 2008 and .NET Framework 3.5 on your server.  During the install you only need to Select the DotNet Development Tools to be installed.

3. Update the Build Targets so you can build the projects you work on your Server.  You need to copy the relevant files from your development machine to your Build Server by updating files in "C:\Program Files (x86)\MSBuild\Microsoft\"

4. Create the SolutionName.msbuild in the root directory of your solution, where SolutionName is the name of your solution.  You will need to update "SolutionName.sln" inside the MsBuild File to point at your solution.

<?xml version="1.0" encoding="utf-8"?> 

<Project DefaultTargets="ReleaseBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/> 

    <Target Name="ReleaseBuild"> 

        <Message Text="Building Your Solution"/> 

        <MSBuild Projects="SolutionName.sln" Targets="Rebuild" />

    </Target> 

</Project>

5. Go to your TeamCity and Edit your Build Runner so is is MsBuild, Change your Target to Release Build to Match the Target in the MsBuildFile, and update any command line parameters you may need. I am using an additional commandline parameter /p:OutDir=%teamcity.build.checkoutDir%\target\ which will compile a solution full of separate webapplications to a single target directory.

6. When you are happy you have the MsBuild File and TeamCity Build Runner correctly matching, commit your msbuild file to SVN.  Your Build will be automatically trigggered and MsBuild will be used to compile your solution instead of Visual Studio.  

This should give you alot more flexibility in future.  If this doesn't work for you or if I have made a mistake just leave me a comment and I will improve the post for others in future.

Tags: , ,

ddd | MsBuild | SVN | TeamCity.Net

Powered by BlogEngine.NET 2.0.0.36
Original Design by Laptop Geek, Adapted by onesoft, and finally some tiny tweaks by JonAlb