Railsmagazine60x60 Continuous Integration Tools in Rails

by Saurabh Bhatia

Issue: Winter Jam

published in December 2009

Saurabh bhatia

Saurabh Bhatia is Co-Founder and CEO of Safew Technology Labs. He has been working with Ruby and Rails since 2005. He loves to write software in Rails apart from managing his company.

He also has a knack for system administration and is an expert level Linux system administrator. In his free time, he likes to listen to music and read fiction. He can be reached at saurabh (at) noisybrain (dot) net.

Introduction

In past few years the software engineering principles have undergone a phenomenal change, what could be called a complete paradigm shift. Software development Life cycle used to rule the software companies, with strenuous phases of requirement "freezing", design development and testing phases. However, Moore's law has applied to each and every part of technology. The rate at which requirements change in real-time in today's times is phenomenal and spares no scope for the traditional software development life cycle to be used as the development process.

One of twelve core principles of Extreme Programming, continuous integration is a practice that imparts discipline to the development process and streamlines the code to always keep it production ready.

A few Practices that make continuous integration highly effective for the projects are:

Single Source Code Repository

Today most of the projects make it a point to follow the source code Repositories as an integral part of the project, but many of tend to add only the development core of the application. A good practice is to add Tests, schema and each every asset related to the Project, everything that is needed to generate a build and supposed to go into production. SCM like git and svn allow to keep multiple branches for the same repository, hence there can be a development branch where you do the development and check for the build errors and merge it with the main branch which will contain the final tested working production ready code.

Automate Build and Build Often

Adding scripts that build whenever the repository is updated and getting the assessment of build errors is a process that signifies a tectonic shift in the philosophy of software development over the ages. So,when we have a single source code repository we have all the components that would be required to emulate a production environment and generate build. One of the agile practices is to avoid quick-fixes and make final production ready drafts from the beginning. Building as often as every commit makes it easy to inflict discipline for such kind of processes.

Test Automation

Test automation is the most logical implication of the above two concepts. What it means is, whenever we send the code into the repository,the tests should run by themselves and you should get results in the a format which you keep checking regularly, for instance Twitter, Yahoo Messenger or email.

There are several tools for these purposes like svn, git, bazaar, perforce as source control systems, cerberus, cruisecontrol.rb, hudson, Selenium continuous integration runner, and test automation tools like selenium. There are several combinations that can be made out of these tools, but the best solution is the one with which you are the most comfortable.

Let's watch some of these tools in action. Starting with the most basic implementation of continuous integration,yet one of the most powerful and effective tools available around for building applications in ruby on rails is Cerberus. Lets get an introduction to the various scenarios in which Cerberus can be used.

Cerberus

Cerberus is a continuous builder which is compatible with all ruby projects that use Rake, so it can be used, not only for Rails but also for Merb and Sinatra. The home of Cerberus project defines it as

“Cerberus is a lightweight and easy-to-use Continuous Builder software for Ruby.”

Cerberus runs periodically to check the latest changes in the repository and checks for tests. If the tests fail and the build is broken, it notifies the users through various channels, like twitter, emails, Campfire, IRC and RSS.

Installation

Cerberus is distributed as a gem package so it is easy to install,upgrade and maintain. This also makes

Cerberus usable on any OS platform. The prerequisite for installation is Ruby 1.8.2 or greater version and Rubygems. In order to install, just run the following command:

saurabh@laptop:~$sudo gem install cerberus --include-dependencies

Successfully installed cerberus-0.7

Successfully installed actionpack-2.3.4

2 gems installed

Add the project to the Cerberus in two ways:

1. Mention the path to the directory directly, mention the application name and email of the people who intend to receive the status of the build.

saurabh@laptop:~/allnews/safewlabs_an$ cerberus add /home/saurabh/allnews/safewlabs_an APPLICATION_NAME=safewlabs_an RECIPIENTS=saurabh.a.bhatia@gmail.com

Application 'safewlabs_an' has been added to Cerberus successfully

2. Mention the svn url and rest remains the same.

saurabh@laptop:~$ cerberus add http://safewlabs.unfuddle.com/svn/safewlabs_traffictwit/ APPLICATION_NAME=traffictwit RECIPIENTS=saurabh.a.bhatia@gmail.com

Application 'traffictwit' has been added to Cerberus successfully

Now goto the Cerberus directory in your home and edit the configuration file with your favourite editor.

saurabh@laptop:~$ cd .cerberus/

saurabh@laptop:~/.cerberus$ nano config.yml

The ~/.cerberus/config.yml file looks like this:

publisher:

# active: mail jabber rss campfire

mail:

address: smtp.gmail.com

port: 587

domain: gmail.com

authentication: plain

user_name: your.gmail

password: yourpassword

# on_event: all

# jabber:

# jid: somemailbox@gmail.com/cerberus

# password: somepassword

# twitter:

# login: twitter_username

# password: twitter_password

# irc:

# nick: cerb

# server: irc.freenode.net

# channel: cerberus

# campfire:

# url: http://someemail:password@cerberustool.campfirenow.com/room/51660

# rss:

# file: /usr/www/rss.xml

#builder:

# rake:

# task: migrate test

#changeset_url: POINT_TO_YOUR_TRAC/changeset/

#hook:

# rcov:

# on_event: successful, setup #by default - run hook for any state

# action: 'export CERBERUS_HOME=/home/anatol && sudo chown www-data -R /home//anatol/cerberus && rcov' #Add here any hook you want

You can uncomment the options you want to and get the messages to the notification of your choice.

Now Goto config folder inside cerberus directory and list the files inside it. You will be able to see the configuration files of applications you had added earlier.

saurabh@laptop:~/.cerberus$ cd config/

saurabh@laptop:~/.cerberus/config$ ls

safewlabs_an.yml traffictwit.yml

Make sure the options in this file are also right:

saurabh@laptop:~/.cerberus/config$ cat traffictwit.yml

publisher:

mail:

recipients: saurabh.a.bhatia@gmail.com

scm:

url: http://safewlabs.unfuddle.com/svn/safewlabs_traffictwit

type: svn

Once you have made sure all this, we are ready to prepare the first build of the project.

cerberus build traffictwit

The result of this is shown in the following screens:

/system/graphics/55/large/build.jpg?1261600701

/system/graphics/56/large/detail-cerberusmail.jpg?1261600736

This command goes to the svn repository and builds the latest version available. In case the tests fail,you get a mail mentioning the details of the build.

Now, in order to stick to the agile methodology, we will go ahead and automate Cerberus build. In order to do this, we setup cron jobs on our system.

saurabh@laptop:~$ crontab -e

Setup the crontab file in order to check for the changes to the repository every 10 minutes.

# m h dom mon dow command

10 * * * * cerberus build traffictwit

If you are hosting your repository on your own server, you can build using subversion hook and run after commit.

Other SCMs supported by Cerberus are git, bazaar, darcs, bjam and perforce.

Conclusion

Continuous Integration is an essential practice in today's scenario, because of the pace at which the projects are carried out. Cerberus is a minimal continuous builder tool, which works pretty well, but does not have a dashboard or a gui for the users. There are several other tools we will explore in the coming articles, which have a UI and have several interesting features.