Implementing TeamCity for .NET Projects, Part 4: Using Build Scripts

May 24th, 2009

For more information on this series, please see the introductory post.  This post also assumes that you have completed parts 1-3.  The sample solution has been updated with new build scripts as of May 24, 2009.  If you have downloaded the solution previously, please download the updated solution.  You can explore the source or create a working copy.  A zip file has been provided for you to download to import into your own writeable repository.

For more advanced or complex build scenarios, the build it solution runners prove somewhat inflexible.  Once you have arrived at this conclusion, you will most likely start investigating some of the other build runner options.

For .NET projects, the two obvious choices are MSBuild and NAnt.  The new hotness for building .NET projects is Rake.  And, if you use a scripting language not built into TeamCity (such as FinalBuilder, Psake, or shell scripts), the command line runner is your friend.  TeamCity also has two .NET build runners specifically targeting code inspection: Duplicates finder and FxCop.

I have been using NAnt for several years to build both web and desktop applications.  NAnt has been fairly quiet for the last couple years.  The latest official release (0.86 Beta 1) is dated December 2007, but there has been sporadic 0.86 Beta 2 activity in the nightly releases.

I have just started working with Rake for build scripts. (A big thanks to the Fluent NHibernate and FubuMVC contributors for their pioneering efforts on this front.)  Rake, like Ruby itself, seems to be infinitely extensible.

What Should a Build Script Do?

My build scripts need to at least handle the following targets/tasks:

  1. Compile the application
  2. Run the tests
  3. Provide a code coverage report
  4. Zip up the compiled application for artifact retention
  5. Update the configuration files
  6. Publish the website to a folder or IIS virtual directory
    TeamCity natively handles 1 and 2, but we will be duplicating that functionality with the build scripts.  TeamCity also allows you to download your artifacts as a zip file, but does not store it as one.  TeamCity does not provide code coverage for .NET projects.  However, it does for Java projects.)
    Our NAnt and Rake scripts will handle the all the tasks.  I will cover the first four tasks in this post.  I will cover the other three in another post. 

Setting Up an NAnt Build Runner

We’ll start with an NAnt Build Runner.  In your TeamCity website:

  • From the Projects page, click the drop arrow next to the existing project and click on Edit Settings
  • On the right hand side of the screen, click on the Copy button to copy the configuration
  • Name the new build configuration “Just Build It! (with NAnt)”
  • Select the Runner link from the navigation bar on the right

Based on the sample solution, enter the following values and save your changes.  Leave all other values at their defaults. (If your solution is structured differently, adjust accordingly.)

Build Runner: NAnt
Path to a build file: nant.build
NAnt Home: lib/nant

Leaving the Targets text box blank will force the runner to use the default target.

The default targets in thee script files create a zipped file of the website and code coverage results from the free NCover Community edition.  I am using a couple custom tasks from kiwidude for integrating NCover with the NAnt script. 

In the General Settings page, add the following to the Artifacts text box to force both files to show up as artifacts:

results/Artifacts.zip
results/CoverageReport.html=>ncover

 
Creating custom tabs is fairly simple as well.  Add the following entry to the <TeamCity data directory>/config/main-config.xml (the default data directory is named .BuildServer to create a tab that shows the NCover results for each build:

<report-tab title="NCover"
basePath="ncover"
startPage="CoverageReport.html" />

(Thanks to Laurent Kempé for the instructions on integrating NCover with TeamCity)

Setting Up a Rake Build Runner

Make another copy of a configuration and name the new build configuration “Just Build It! (with Rake)”. This time select Rake as the build runner on the Build Runner page.  Leave all other values at their default.  (The Rakefile in the root directory will be used.  I modified the rake scripts from the FubuMVC project for this solution.)

You will need Ruby and a couple of Gems to get started.  Check the information in the Ruby_Not_Installed.txt file in the root directory of the sample solution. 

Setting Up an MSBuild Build Runner

I actually do not have an example of an MSBuild script (because I do not have enough experience with MSBuild).  Setting up the build runner is similar to the NAnt and Rake runners.  If you are interested in this build runner type, check out Ryan Anderson’s posts. He has done an excellent job detailing the process.


 Upcoming posts:

  • Deployment: Using TeamCity and build scripts to update different deployment environments.
  • Setting up notifications: We will look at the different options for notifications, such as email and the tray notifier.

JetBrains TeamCity
Sample Project on GoogleCode
NAnt / NAntContrib
NCoverExplorer Downloads
NAnt
NAntContrib
NCoverExplorer Download Page (with NAnt/MSBuild documentation links)
Ruby Downloads