Friday, January 13, 2012

Hudson/Jenkins With RVM and PhantomJS

Setting up Hudson/Jenkins to work RVM (Ruby Version Manager) and PhantomJS (for headless JavaScript testing) can be painful. This post will show you how to easily set them up on your own server.

At Bizo we have several projects that have dependencies on different versions of Ruby, mostly due to some projects relying on older gems which are incompatible with Ruby 1.9. Installing RVM on a dev machine is almost always a cinch but getting it to play nicely with your ci build server isn't quite so straightforward.

We run our Hudson server off of an Amazon EC2 instance. Our EC2 instances are started up with custom software, but  it really boils down to executing a bash start up script. Assuming the Hudson user's $HOME is set to /var/lib/hudson, you can copy/paste  the code below to install RVM for you. Otherwise just replace /var/lib/hudson below to the $HOME dir of your Hudson (or Jenkins) user.

bash -s stable < <(curl -s
echo "[ -s \"/var/lib/hudson/.rvm/scripts/rvm\" ] && source \"/var/lib/hudson/.rvm/scripts/rvm\" # loads RVM" > .bashrc
# ensure RVM is loaded
source ~/.bashrc

echo "Installing Ruby 1.9.2"
rvm install 1.9.2 
rvm use 1.9.2

echo "Installing gems for Ruby: 1.9.2"
gem install bundler --no-rdoc --no-ri
# add additional ruby versions here
su - hudson -c "$COMMANDS"

Then in your Hudson build go to your project configuration and under "execute shell" you can invoke rvm and run your project like normal. Note* our version of Hudson doesn't automatically load .bashrc, so you might need to source it first to ensure RVM loads, ex:

source ~/.bashrc
# Pick our ruby version
rvm use 1.9.2

# Run your project... ex bundle install && rake test:units for a Rails project 

PhantomJS is our execution environment of choice for running JavaScript unit tests and setting it up to run on Hudson is actually quite easy.

Here is the necessary bash snippet to make it available for use in Hudson. 

INSTALL_PATH= # wherever you want
wget ${INSTALL_PATH}/phantomjs.tar.gz
# OR For 64bit machines wget

mkdir ${INSTALL_PATH}/phantomjs
tar -zxvf ${INSTALL_PATH}/phantomjs.tar.gz -C ${INSTALL_PATH}

ln -s ${INSTALL_PATH}/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
After running the script above you can invoke PhantomJS as "phantomjs" in the "execute shell" box inside your project configuration. You'll probably want your tests to fail with a non-0 exit status so the Hudson build will fail, if you use the Jasmine testing framework you can use our phantom-jasmine test runner on Github:

No comments: