Setting Up Travis Locally with Docker to Test Continuous Integration

TravisCI is a great service for running your continuous integration scripts, but when something isn’t working it can be a pain to resolve. One way of testing your script in order to fix it is to install Travis locally using Docker.

The following instructions should help you get set up with a local Travis install; and will show you how to get it up and running.

Getting Started

To begin with install Docker if you haven’t already done so. Then choose a Docker image based on the language you’ve chosen in your .travis.yml file. For example, this uses the most recent (at the time of writing) image for PHP:-

$ docker run --name travis-debug -dit travisci/ci-php:packer-1494867192 /sbin/init

In the example above the container has been named travis-debug, but what you call it is up to you (just remember to replace references to travis-debug below with your chosen name).

Once the image is installed, start your Docker container.

$ docker exec -it travis-debug bash -l

You now want to switch to the travis user.

$ su - travis

Installing Travis

Clone the travis-build repository in your builds directory and switch to it.

$ cd ~/builds
$ git clone https://github.com/travis-ci/travis-build.git
$ cd travis-build

Install Travis.

$ gem install travis
$ travis
$ bundle install
$ bundler add travis
$ bundler binstubs travis

Setting Up Your Test

Generate a new SSH key for GitHub (changing your-github-email@example.com for your own email address).

$ ssh-keygen -t rsa -b 4096 -C "your-github-email@example.com"

You need to copy the contents of the key you’ve just created to your GitHub SSH keys. Name the key something that is easy to identify like Travis Debug Key. Assuming you saved your key to the default path you can view it using:-

$ less ~/.ssh/id_rsa.pub

Once you’ve set up your key in GitHub return to your Docker terminal. The next steps are to clone the repository you want to run tests for and to compile the Travis script for it.

Change back to your builds directory.

$ cd ~/builds

Then clone your repository and change into the directory created by the git clone.

$ git clone git@github.com:AUTHOR/PROJECT.git
$ cd PROJECT

Next it’s time to compile your Travis build script. We do this using travis compile and write it to a file that we can execute; in this case we are writing to a file called ci.sh.

$ travis compile > ci.sh

Your going to need to open up this newly created file and make some changes before you can run the script. The most important thing you will need to change is the Git branch you want to checkout for your tests.

$ vim ci.sh

I’m using Vim here to edit the file as it’s my editor of choice from the command-line but use something else if you are not comfortable using it. In Vim we can quickly find the line we need to edit by searching using /branch. This should lead you to something like:-

branch\=\'\NEW_BRANCH'\

Change this to your branch name.

If you’re using the matrix and/or env keywords you’ll need to update these too as the compiler will have ignored these.

Finally

You should now be in a position to actually run the script.

$ bash ci.sh

Hopefully you now have a functioning local copy of TravisCI to test with.

Related Content

Published on