Hudson test drive (Part 1: Rails) | November 11, 2008-->
November 11, 2008I've been using Hudson for quite a while now and it just rocks. Gone are my CruiseControl (for Java) and CruiseControl.rb (for Ruby/Rails) installations. I even ditched most of my crontab entries and have Hudson manage the repeated tasks for me.
Hudson monitors executions of repeated jobs, such as building a software project or jobs run by cron. Among those things, current Hudson focuses on the following two jobs: 1. Building/testing software projects continuously just like CruiseControl or DamageControl. [...] 2. Monitoring executions of externally-run jobs, such as cron jobs and procmail jobs, even those that are run on a remote machine. [...]
Compared to CruiseControl configuring jobs in Hudson is really easy and straightforward. You can easily give it a test ride without even installing Hudson in your servlet container. Let's give it a try.
stefan@macbook-pro:~$ curl -OL http://hudson.gotdns.com/latest/hudson.war
% Total % Received % Xferd
100 19.6M 100 19.6M 0 0
stefan@macbook-pro:~$ java -jar hudson.war
[Winstone 2008/10/24 12:33:11] - Beginning extraction from war file
hudson home directory: /Users/stefan/.hudson
This will start an instance of hudson which can be accessed at http://localhost:8080/
Hudson will create a .hudson directory to store files and plugins. You can set the HUDSON_HOME environment variable to use a different directory.
stefan@macbook-pro:~$ export HUDSON_HOME=/opt/local/hudson
Due to its multi purpose nature it's easy to build different kinds of software projects. If you can build your projetc using a command line script you might be able to build it using Hudson.
Hudson and Rails
Part one covers building Rails projects with Hudson. I'm going to cover building Java and Flex projects in future posts.
In order to build a Rails project as soon as a commit is made, we need a couple of plugins. I'm going to use the git scm for this example. The default Hudson installation supports SVN out of the box but with the Hudson git plugin using git as your scm is a piece of cake.
Go to Manage hudson (http://localhost:8080/manage) > Manage plugins (http://localhost:8080/pluginManager/) and click on the Available tab. Choose the following plugins:
- Git Plugin
- Rake plugin
- Ruby Plugin
and click on the Install Button (bottom right).
Hudson needs to be restarted in order to activate the plugins. Stop the currently running proceess (CTRL+C) and start Hudson again (java -jar hudson.war).
Configure your Rails project
Hudson can run your Rake test task without further modification. But it would be really nice if the test output could be aggregated in Hudson.
We are going to install the ci_reporter gem (http://caldersphere.rubyforge.org/ci_reporter/) for this purpose.
CI::Reporter is an add-on to Test::Unit and RSpec that allows you to generate XML reports of your test and/or spec runs. The resulting files can be read by a continuous integration system that understands Ant‘s JUnit report XML format, thus allowing your CI system to track test/spec successes and failures.
stefan@macbook-pro:~$ sudo gem install ci_reporter Successfully installed ci_reporter-1.5.1 1 gem installed Installing ri documentation for ci_reporter-1.5.1... Installing RDoc documentation for ci_reporter-1.5.1...Add the following lines to your Rakefile (or clone the git repository with the Rails example project:
git clone http://github.com/juretta/hudson-example-project-rails/tree/master).
require 'rubygems' # Install: # sudo gem install ci_reporter gem 'ci_reporter' require 'ci/reporter/rake/test_unit' # use this if you're using Test::Unit
Run the following rake task and the ci_reporter gem should create xml files in the results directory of your project.
stefan@macbook-pro:~$ rake ci:setup:testunit test:units \
test:functionals CI_REPORTS=results
Add a new job
Go to the Hudson homepage and click on New Job (http://localhost:8080/newJob). Choose a Job name, select Build a free-style software project and click Ok.
You should now be on the project configuration page (http://localhost:8080/job/YOUR-PROJECT-NAME/configure).
- In the Source Code Management section choose git. Enter your git repository URL (The Rails project used in this example is hosted on github: git://github.com/juretta/hudson-example-project-rails.git).
- Click on Add build step and choose Invoke Rake. Enter db:migrate ci:setup:testunit test:units test:functionals CI_REPORTS=results RAILS_ENV=test into the Task field.
- In the Post-build Actions section check Publish JUnit test result report and enter
results/*.xmlinto the Test report XMLs field. - Click Save
You can click Build now to trigger the first build of your Rails project.
We now want to trigger the build automatically as soon as we do a git commit.
Edit the post-receive file in the hooks directory of your git repository (.git/hooks/post-receive).
URL='http://localhost:8080/job/YOUR-PROJECT-NAME/build' echo "Run Hudson build" wget $URL > /dev/null 2>&1
Make the file executable.
stefan@macbook-pro:~$ chmod u+x .git/hooks/post-receive
The hook script now triggers a build after each commit.
The Ruby metrics plugin
The Ruby metrics plugin shows Rails stats (LOC) and rcov reports.
Unfortunately with the latest version of Hudson the Ruby metrics Plugin does not seem to work.
You should definitely give it a try. If you get an HTTP 500 Status code with a org.apache.commons.jelly.JellyTagException
remove (or disable) the following line:
<st:include it="${it.owner}" page="sidepanel.jelly" /> in
$HUDSON_HOME/plugins/rubyMetrics/WEB-INF/classes/hudson/plugins/rubyMetrics/railsStats/RailsStatsBuildAction/index.jelly
Have fun with Hudson!
