Using Ruby and Rake with ASP.NET

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.

Comments

April 13. 2009 11:28

AgeDefying

Very informative article. I also started working with PowerShell and for me important any information.

AgeDefying

April 26. 2009 10:47

Kampanye Damai Pemilu Indonesia 2009

Very informative article..
thanks for this great informations, now i find what i want to know..
thanks..

Kampanye Damai Pemilu Indonesia 2009

April 26. 2009 21:10

laptop ac adapter


I was very interested to read your post and it seemed to me that it is quite interesting!

laptop ac adapter

May 1. 2009 20:33

graphics comments

thanks for this usefull informations..
now i find what i want to know..
thanks..

graphics comments

July 13. 2009 06:10

Refrigerator water filter

It was really a nice tutorial! Thanks for sharing this information to us. Just hoping that this will not be the last post that I could be read written by you.

Refrigerator water filter

August 4. 2009 14:52

Inheritance Tax

Hi. This information proved to be very useful. Can you please provide more aspects of this subject? Thanks.

Inheritance Tax

September 7. 2009 19:11

buy laptop

Can u compare jsp and asp.net over security issues, fastness, cost effective? Which is best to avoid hacking?

buy laptop

September 24. 2009 05:51

online poker

Hi,


How to use microsoft express to built a web page in asp.net?

online poker

September 25. 2009 11:00

Life insurance

How can I allow my website visitors to create text files in ASP.NET?

Life insurance

September 26. 2009 12:01

Tiffany Bracelets

I was very interested to read your post and it seemed to me that it is quite interesting!

Tiffany Bracelets