For awhile now at work, I've been using a loose collection of PowerShell scripts and manual steps to handle deployment of ASP.NET sites and associated SQL Server databases. I recently switched to using a Rake build script to not only automate deployment, but also to help in a few other tasks such as re-building the development database, and synchronizing the staging database with the production db.

Briefly, Rake is a Ruby version of Make, and is a framework for build scripts written in Ruby. Since a Rakefile (the main build file you work with) uses a plain Ruby syntax (albeit one that uses a few Rake conventions), it's very easy to get up to speed with if you know a little Ruby, and even if you don't, Ruby's syntax has a reputation for being fairly straightforward anyway. Rake also has many built-in library functions for handling builds and uses a dependency-based computational model. For example, I can specify that task A depends on task B and be sure that task B is always called before task A:

task :a => :b do
     puts "World" 
end 
task :b do
    puts "Hello " 
end

Rake is normally used in the Ruby world (such as for working with Ruby on Rails sites), but is generic enough to be usable with other setups such as .NET. Why did I switch to Rake? I tried, I really tried to like and use PowerShell. PowerShell is Microsoft's newer command line environment and scripting language. It's a definite improvement over the normal Windows command prompt, as it offers a greatly enhanced command set and direct interoperability with the .NET framework. PowerShell, as well as a light development environment for PowerShell scripts, are tentatively set to be included in Windows 7.

However, I was put off by PowerShell's syntax, which uses a blend of curly braces, mixed case keywords, dollar signs,  and unconventional operators e.g. the less than or equal to operator in:

for ( $i = 7; $i -le 84; $i+=7 ) { $i }

Also, since PowerShell is relatively new, there seems to be poor support for it. Editing support is not included with Visual Studio and it's not natively supported (with syntax highlighting, etc) in common free text editors such as Notepad++. Also, while you can write individual PowerShell scripts and tie them together yourself, there's not a robust framework such as Rake made specifically for builds (although there is the psake upstart). Ruby and Rake, on the other hand, are much more mature and established.


To deploy, the central steps that my Rakefile does are:

  1. Take in an argument specifying whether to target the staging or production server.
  2. Deploy the database
    1. If deploying to the staging server, synchronize the schema from the production server (using RedGate's excellent schema comparison and synchronization tool).
    2. If deploying to the staging server, synchronize the data from the production server (using another RedGate tool). This and the step before insures that the staging server has a db that's a mirror of production.
    3. Run migrations to get the db up to the latest version.
  3. Deploy the site
    1. Compile the Visual Studio web site solution.
    2. Copy the site files to the staging or production server (using the free Robycopy tool).

I should run the unit tests as a part of the deployment task, but since the project is fairly small, I've been running them independently before deploying. I have a task set up for the unit tests, so I could fairly easily plug this in later.

There's a task that recreates the test database on a developer's local system, and it actually still calls out to an older PowerShell script that I haven't bothered converting yet (but having Rake and PowerShell interact is fairly seamless at the moment).

Here's my Rakefile. The steps outlined above should be recognizable in the script, but please leave a comment if you have a question about anything in particular in it or any suggestions as well.