Build and deploy .NET projects with Rake and Albacore

Hello. I wanted to share with you a small rake script that I use to build and deploy my .NET projects, be they web sites or simple DLLs that need consistent versioning and trackability in the wild.

If you don’t know rake, it is a task oriented ruby gem that allows to do a lot of things… you can get more info from this link, including installation details.

Of course before installing rake you will need to install Ruby on your machine, just head to the installation download page and get the version you prefer. I am using 1.9.3 but 2.0.0 should work perfectly.

Once Ruby and Rake are installed, you need albacore… Just open your DOS prompt and type gem install albacore and that will do. If you don’t know what albacore is and if you have problem installing just head to rubysource for a quick overview.

The script

Let’s start to write our script and analyse it as we go through. Open your favourite text editor and start typing.

Save the file as rakefile.rb (one of the default names for rakefiles, this frees you from specifying the filename when running rake).
The first line here loads the albacore gem, while the second line will load the config file (which I like to keep separate since it will be different for each project).
I also like to have the $svn_revision initialised to a dummy value so we know if something went wrong later on, when we get this number from the repository.
The task keyword creates a task, named :default (meaning it will be run by default if you don’t specify anything on the command line), specifying that it depends on other three tasks, and gives it a description (meaning it will be shown when you request the list of tasks from the command line).

The rakeconfig.rb file is simply a container for all the config for your script and looks like this:

The properties are needed further along in the script. They are defined like this so that they can be overridden by a command line option (useful if you use this script both on your machine and your Continuous Integration Server).

The command line

Since we have been talking about the command line a couple of times now, let’s see what option we have available. First, though, let’s add a task to our rakefile so we can see the effects of our efforts. Add this to the rakefile and save:

Open a command prompt and type rake -T. This option (uppercase T) will give you a list of tasks with their description. If a task does not have a desc line above it, it will not show in this list; in this way you can have sort-of “private” tasks in your rakefile. In your output you should see the two tasks default and config:

Now try typing: rake config PROJECT_NAME="Another Project". This should output the configs as coming from the config file, except for PROJECT_NAME which should now be “Another Project”. You need to use the quotes if you want to put spaces, and you will need to escape backslashes using \\ of course, in case you are typing paths.

Ok let’s go ahead and try just simply rake. This will try to build the default task and since the deault task depends on the other tasks it will go ahead and try to run those:

Of course it will abort immediately since it does not know what the other tasks are! So let’s get started on those.

The AssemblyInfo Task

Until now, in case you were wondering, we haven’t used anything that comes from Albacore: all of the above was pure rake stuff. Now we are going to use the albacore gem to create a task that updates the AssemblyInfo.cs file in our project so that the revision version matches the SVN revision. The task looks like this:

Here, the first assemblyinfo is the albacore task we are invoking, while :assemblyinfo is the task name; it can be anything you like and it must match the name used above in the :default task dependecies: => [:config, :assemblyinfo, :build].
So, first of all I copy my AssemblyInfo.template as the AssemblyInfo.cs. I do this because I don’t want to commit my real file everytime I change the version number, but I like to somehow have the file in SVN, so I keep the template file versioned and the .cs file unversioned.

The assemblyinfo task has a lot more options that you can read in the Albacore Wiki. We are using the input and output file options so we don’t have to create a new file from scratch but simply edit the existing one.

To get the SVN version, the get_version() method runs the svn info command (the svn command line client should be in your PATH of course) and gets the output into an xml file (see how we use the config options specified in the rakeconfig file?). The xml file will look something like this:

So we use REXML (which is built into ruby) to get the entry element and read the revision attribute… nothing too fancy here. We end up with a number than in this case will be 1.0.0.48 and we stick it in the Version and FileVersion attributes of your Assembly Info File. If you open it, at the bottom you should see:

That’s it, it could not be any easier could it? I have omitted the description here since I would like people to use the default (or the other “public”) task, following a series of predefined steps rather than cherry-picking the task to run.

Now your command line rake command should work without problems and your solution will build. If you go into the bin\Debug folder and right click on your project DLL, selecting Properties and then the Details tab should show the FileVersion as being what we wanted. Cool, isn’t it?

Unit testing

As a bonus, albacore also has nunit and mstest tasks to run your tests. You simply need to create a task like this:

The properties just set up everything for deploying. The :UseWPP_CopyWebApplication => true will do your Web.Config transforms, this will require the :PipelineDependsOnBuild => false, or your build will fail.

That is all for now. You may notice there is some inconsistent use of the config properties (some have the extension, some don’t, we need to put trailing slashes etc.), but I am sure it is something you can easily make more consistent yourself.