Continuous Integration: Travis CI

Travis is a cloud-based continuous integration server that works really well. It’s quite well-known in the open source community because it’s free for open source projects and it integrates perfectly with GitHub. In this short post, I’ll go over how to set up Travis for your open source project.

I’ll assume you know what CI is, if you don’t, the short version is, it’s a way to build, run and test code automatically. I wrote an article on one other CI solution: nevercode.io, check it out if you want. In this post, I’ll focus specifically on linking Travis with Github and running your unit tests on it.

Connecting With GitHub

Travis CI is available from the GitHub Marketplace, so connecting with it is extremely simple. In the pricing and setup section at the bottom select the install button:

This will link Travis with your GitHub account, next thing to do is to select the repositories you want to build using Travis, let’s do that:

If you don’t see the repository you want to build press the ‘Sync account’ button. Just a note, this is a free version of Travis that I’m using here, so you’ll only see your public repositories. When you flick the switch on the repositories, you’ll find that repository in your list of repositories:

Now if you select your repository you’ll see it has no builds:

Travis will trigger your builds every time you push into a branch, but it needs to know a bit about your project. Let’s see how to set the Travis up.

Setup Travis

If you read my post on nevercode you’ll remember that you could configure nevercode from the browser by setting a couple of parameters and you were set to go. To configure Travis you need to commit a file to the root of you repository and the file must be named travis.yml. If you simply want to build and run your project this file will be really simple, if you want to run unit tests you will need to add a couple of parameters to it, but it’s really easy to do it. It’s pretty common to use CI to run your unit tests, so I’ll show you how to configure your travis.yml for that.

Before we do anything, it’s worth pointing out that your scheme needs to be shared in order for Travis to be able to build your project. Go to ‘Manage Schemes…’:

And make sure your scheme is shared:

Now you can create your travis.yml file. Create it in the root of your project. This is how I have my travis.yml file configured:

If you only want to build and run your project, you’ll only have to specify the language you’re using for your project (swift in my case). I wanted to test my project on a simulator as well, so I set it up to be built on Xcode 8.3 and to run a script after the build.

Travis can run some terminal commands, like xcodebuild. So you can add a command you would normally use to run the tests from your terminal directly into the file. You can add this command in the ‘script’ section and you can add as many commands as you want.

The only thing to note here is the simulator name and OS version. If Travis doesn’t have the simulator you provided it will kindly inform you about the available simulators, so you can just use one of them.

When you’re finished with the file, save it, and commit it to the root of your repo, Travis will queue your build, you can see it in the ‘Build History’ tab:

Or in the ‘Branches’ tab:

Once your build starts you’ll see the printout in the console, you can click on the build number to see this page:

If your tests passed you should see a nice green checkmark next to your branch name, life is good. Just to show you, it took me a few tries to set it up properly, mainly because xcbuild was not working for me and I mistyped the simulator name a few times:

But once you get it working it’ll work pretty much all the time… Unless Xcode update breaks the build 🙂

Add Build Status Badge

That’s pretty much it. Just one last thing. Travis has a little badge that you can stick into your readme.md file and it will display on your project’s GitHub page. This way you, and the world will know if your build is passing or failing. Click on the little ‘build unknown’ icon next to your project name:

This will present a small popup to you, select your branch and since we’re using Travis with GitHub, select Markdown:

Copy the markdown code and paste it in your readme.md file where you would like to see the badge:

Now you have a badge on your GitHub repository and when you click on it, you’ll be landed on the Travis page for the repo. Sweet 🙂

Conclusion

Travis can be a bit pricey for private repositories, but if you have public repositories it’s a great option. It’s easy to set up and it just works. It also integrates really well with GitHub and is a de-facto CI for open source projects. So you should definitely give it a go.

To be honest with you, I wasn’t planning on covering Travis CI on my blog, but I wanted to play with another tool called codecov, I decided to link it with Travis. So in one of my next articles, I can tell you all about it 🙂 I also said to myself that this post will be less than 1000 words, I should stop talking I guess 🙂

Have a nice day 🙂

Dejan.

More resources

0

Leave a Reply