Tuesday, December 14, 2010

CruiseControlling Ruby 1.9.2 and Rails 3.0.2

CruiseControl for Ruby from ThoughWorks has long been one of the easiest ways to your rails project under continuous integration.

But there's still an issue that it can't run under Ruby 1.9.x. That's not very good if you are targeting 1.9.2 for your project.

Here's a quick recipe for how you can build a 1.9.2 project with CC, using the wonders of rvm..
# download and unpack CC to /usr/local/cruisecontrol-1.4.0 (or where you like)
# for convenience, add .rvmrc in /usr/local/cruisecontrol-1.4.0 to have it run 1.8.7
echo "rvm 1.8.7-p302" > /usr/local/cruisecontrol-1.4.0/.rvmrc
# configure CC:
cd /usr/local/cruisecontrol-1.4.0
./cruise add my_project_name --source-control git --repository git@github.com:myname/myproject.git
# ^^ This will initialize the ~/.cruise/projects/my_project_name folder for a git-based project

# if you have an .rvmrc file in your git repo, pre-emptively trust it to avoid clogging CC:
mkdir ~/.cruise/projects/my_project_name
rvm rvmrc trust ~/.cruise/projects/my_project_name

In ~/.cruise/projects/my_project_name, edit cruise_config.rb to run a shell script instead of the standard build task (I'm calling it ccbuild.sh and it will be in the root of my git repo):
Project.configure do |project|
# [.. other stuff ..]
project.build_command = './ccbuild.sh'
# [.. other stuff ..]
end

Add ccbuild.sh to your repository (don't forget to chmod u+x it). It needs to ensure rvm script is loaded and activate the correct ruby & gemset.

The script initialization is necessary because it seems the way CC spawns the shell script it doesn't pick up the rvm initialization you might already have in .bash_profile. Without rvm script initialization, "rvm" will invoke the binary which can't make the direct environment mods it needs to do.

Here's what I have in ccbuild.sh:
#!/bin/bash

if [ "$(type rvm | head -1)" != "rvm is a function" ]
then
source ~/.rvm/scripts/rvm || exit 1
fi

if [ "$(type rvm | head -1)" != "rvm is a function" ]
then
echo "rvm not properly installed and available"
exit 1
fi

rvm use 1.9.2-p0@mygemsetname --create
bundle check || bundle install || exit 1
rake # setup to run all required tests by default

Once that's checked in and pushed to the repo, you can kick-off CC:

cd /usr/local/cruisecontrol-1.4.0
./cruise start

Now my ccmenu is green, and CruiseControl is running my project under 1.9.2 and rails 3.0.2;-)

Blogarhythm: Waiting for the light ART-SCHOOL

No comments: