GitLab CI Variables for Coveralls
Setting up a demo project on GitLab to use Coveralls was not as straightforward as I expected. Here are a couple issues I ran into.
.gitlab-ci.yml
I used GitLab’s Ruby on Rails template project and the CI wasn’t working out of the box. Here’s a working version that I trimmed down to just have an rspec step:
GitLab CI project variables
Coveralls supports multiple CI’s but there are not specific instructions for GitLab. This page (near the bottom) lists the environment variables required for Coveralls to send code coverage stats. The trick is in how you map those variables to their GitLab counterparts.
Here’s what I came up with. In your GitLab project, under Project Settings -> CI/CD Settings -> Variables:
Key | Value |
---|---|
CI_BRANCH | $CI_COMMIT_REF_NAME |
CI_BUILD_NUMBER | $CI_JOB_ID |
CI_BUILD_URL | $CI_JOB_URL |
CI_NAME | gitlab |
COVERALLS_REPO_TOKEN | your token |
Rails config
Make sure you’ve added the Coveralls gem (instructions) and added this to your spec_helper.rb or rails_helper.rb:
Now you should be good to go. Next time you push commits up to GitLab the GitLab CI should trigger a Coveralls stage after running the rspec task.
If you have troubles you can consult my test project.