Pete Hamilton

Adding Travis CI to a Ruby Project

So over the last few days I created a gem to help me generate weekly digest emails for my university computing society. See the Project

I often wish I could use Travis CI for my projects but usually the repositories are private and so I’m forced to find an alternative (see my previous post on ci).

This time however, my project is fully public and so I wanted to make use of Travis and also get those nice build messages you often see on github like this:

I had a bit of a trial and error first experience with Travis CI so I wanted to note down here how to do it for future projects.

1. Add ‘.travis.yml’ in the root of your project

This will be used by travis-ci when building your project and defines what to test against.

For my digest email project (a command line ruby gem) it looks like this:

language: ruby
rvm:
  - 1.9.3
  - 1.8.7

Mine simply says to test against Ruby 1.9.3 and 1.8.7 but there are a lot more configuration options available depending on what kind of application you’re testing. See here for more info.

2. Depend on the ‘rake’ gem

Travis CI uses rake to run your tests so you need to require it in your Gemfile. If you want you can only put it in the test group.

group :test do
  gem 'rake'
end

3. Make sure your Rakefile has a default task

Travis CI will attempt to run rake to execute your tests. You need to ensure you have a default task which will run all the tests you wish to execute.

My Rake file for the digest-email gem looks like this:

require "bundler/gem_tasks"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new

task :default => :spec

Push this all to your project and you’re ready to link Travis to your repository.

4. Ask Travis to test your repository

  1. Visit https://travis-ci.org/ and sign in with github
  2. Visit https://travis-ci.org/profile/YOUR_GITHUB_USERNAME
  3. Switch on testing for your repo by finding it and flicking the switch. You may need to sync with your github account if the repository doesn’t appear.Also, only public repositories will be visible.

  4. Visit https://travis-ci.org/YOUR_GITHUB_USERNAME/GITHUB_REPO_NAME You’ll see the current build status. You’ll notice that travis has yet to run any tests. This will update whenever you push your code to github.

5. The icing on the cake - build status images

This is really simple, in your github readme, simply add the following snippet onto your title, like this (if using markdown):

# DigestEmail
[![Build Status](https://travis-ci.org/PeterHamilton/digest-email.png)](https://travis-ci.org/PeterHamilton/digest-email)

For other README formats or to show build statuses for specific branches see here