Continuous-Integration(Ruby-Style)

Chris
bigzoo
Published in
3 min readJul 30, 2017

Don’t we all feel some fulfillment when we see the green bar on our rockstar projects. I think all projects are rockstar btw, especially the 101 to-do lists we have on out gh profiles.

A Project I recently worked on.

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 project (a command line ruby gem) it looks like this:

language: ruby
rvm:
- 2.2.3

Mine simply says to test against Ruby 2.2.3 but there are a lot more configuration options like setting two versions to test against, e.g

rvm:
- 1.9.3
- 2.2.3

More are 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 like me or always have it in your app.

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.

Part of my Rakefile for the project looks like this:

require "bundler/gem_tasks"
require "rspec/core/rake_task
RSpec::Core::RakeTask.newtask :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. If you want to use a private repo you have to instead use https://travis-ci.com/.
  5. Visit https://travis-ci.org/YOUR_GITHUB_USERNAME/GITHUB_REPO_NAME You’ll see the current build status.
  6. You’ll notice that travis has yet to run any tests. This only updates whenever you push any commit to your repo.
  7. To start a build, perform one of the following:
  • Commit and push something to your repository
  • Go to your repository’s settings page, click on “Webhooks & Services” on the left menu, choose “Travis CI” in the “Services”, and use the “Test service” button.

NOTE: You cannot trigger your first build using Test Hook button. It has to be triggered by a push to your repository.

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):

# Billy
[![Build Status](https://travis-ci.org/bigzoo/billy.svg?branch=master)](https://travis-ci.org/bigzoo/billy)

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

Follow me on Twitter

--

--

Chris
bigzoo
Editor for

Lifelong learner. Start-up CTO @snaphubr , Engineer at @andela