Continuous Integration for Rails 6 with Semaphore, Travis and Coveralls Part 2
In my part 1 of the Continuous Integration series, I have covered Semaphore Classic and Semaphore 2.0, you can read it here. The second part will include Travis CI and Coveralls
Travis CI is also a continuous integration tool like Semaphore which is used mostly with Github and Bitbucket. Let’s get right into the setup.
It is quite easy to set up Travis CI in your open source project. First, you need to visit travis-ci.com then sign in using your Github account and give Travis CI permission to access your project;
It is alright to select All repositories because you don’t need to worry about integrating your future repos with Travis, it will show up automatically on the Travis system.
Right now you should be able to see your list of repos on Travis web app. I will select my Rails project with RSpec tests which I have covered in part 1 of this series.
Well, we haven’t set it up yet so let’s do that. To activate Travis on every commit or PR on this repo, you need to create a configuration file and call it
.travis.yml in your root of the project folder. Travis CI support many languages you can check them on Travis Docs
As you can see, this config file is very similar to that we use in Semaphore 2.0. We need to tell Travis which Ruby version we are using, database type, and package manager. You can change your preferred version of the database if you wish by typing
postgresql: "9.4" instead. (Default is 9.6 for PostgreSQL)
If you are using SQLite in your project, unlike Semaphore 2.0, Travis supports SQLite3. Here is additional information you need to set up Travis with SQLite3 in case you like to stick with Rails default database selection.
As soon as you push your repo with
.travis.ymlfile, Travis will start to build automatically.
Coveralls shows your test coverage in detail which is quite useful for Test-Driven Development. Test coverage is defined as a method that determines if our test cases are actually covering the code and how much code is exercised when we run those test cases. With test coverage, we can determine where there are gaps in requirements and how many tests are executed. Coveralls shows your coverage in percent and can show which line of codes are not being tested. Yes! It checks every line of code in the application.
Let’s start by adding coveralls to Gemfile in our project;
bundle on the terminal, the next step is to add Coveralls to your testing suite. We need to add the necessary configuration to
Coveralls.wear! must occur before any of your application code is required, so should be at the very top of your
The final step of the Coveralls setup is to create a rake task so that we can push results to the Coveralls system. We are going to create
lib/tasks/ci.rake task file;
Now you can run
rails ci:tests on your terminal and RSpec will run and report to Coveralls.
It is time to connect your Github account with Coveralls, you can easily do it by logging in Coveralls with your Github account on their website.
Turn Coveralls on for your Rails project from the list, Run your custom task then click details.
If you click List on Source Files on Development, you will see that Coveralls provide detailed reports about your project files. You are able to check which part of your un-tested code is causing low coverage percentage.
We wouldn’t prefer running Coveralls each time we commit or merge PR’s, would we? Of course, we prefer automation for this, we are going to make a slight adjustment for our Semaphore and Travis CI setups.
Integrate Coveralls with Semaphore
It is very simple to integrate Coveralls with Semaphore, you only need to define an environment variable before running your task and that will be Coveralls Token for the repo. You can get your token from the details section of the coveralls.
Integrate Coveralls with Travis CI
Travis integration of Coveralls is also very simple, you just need to create
.coveralls.yml file in the root of your project and add the configuration below;
And edit your
.travis.yml file accordingly for our rake task;
This concludes the setup for Travis integration with Coveralls. Whenever you commit, Coveralls will automatically evaluate the test coverage.
We have covered Semaphore and Travis CI tools and I believe it is fair to say they are very similar on basic setup and quite suitable for open-source personal projects. Continuous Integration will help developers save time and coordinate with fellow colleagues without breaking the codebase and having Coveralls connected will support Test-Driven Development with its detailed code coverage reports.
See you on other topics and articles!