Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
CircleCI 2.0 Parallel builds SimpleCov coverage report merging locally

Simplecov aggregated coverage report from CircleCI 2.0 parallel builds (focused on storing locally/within CI containers as artifacts)

Problem Statement

We have Rails application which is running tests on circleCI 2.0, we have simplecov configured to track the coverage of our test suite. Now the problem is with parallelism enabled, we have partial coverage reports in all different containers according to the tests those containers ran.

We obviously want to have consolidated simplecov coverage report which actually shows us overall coverage report.


There are a few steps involved in this process, we have to fetch all coverage jsons generated by simplecov in different containers of the build, and than merge those.

For fetching the coverage jsons we have two options:

1- Use CircleCI 2.0 feature of workflows. (This is more straight forward and makes more sense when you already are using workflows)

2- Use deploy step in simple CircleCI 2.0 build. Build step also waits for all parallel builds to finish before it executes this step in container number 0. (This method requires to use circleci api to fetch coverage files from builds)

**Credits: ** Help was taken from many resources main resources were this & this. Help from CircleCI support was also taken, although that was not very helpful but helped clear some things.

#!/usr/bin/env ruby
# NOTE: This will only work if you have simplecov >= 0.16.1
require 'open-uri'
require 'active_support/inflector'
require 'simplecov'
class SimpleCovHelper
def self.report_coverage(base_dir: './coverage_results')
SimpleCov.start 'rails' do
skip_check_coverage = ENV.fetch('SKIP_COVERAGE_CHECK', 'false')
add_filter '/spec/'
add_filter '/config/'
add_filter '/vendor/'
Dir['app/*'].each do |dir|
add_group File.basename(dir).humanize, dir
minimum_coverage(100) unless skip_check_coverage
new(base_dir: base_dir).merge_results
attr_reader :base_dir
def initialize(base_dir:)
@base_dir = base_dir
def all_results
def merge_results
results = { |file| SimpleCov::Result.from_hash(JSON.parse( }
SimpleCov::ResultMerger.merge_results(*results).tap do |result|
artifacts = open(api_url)
coverage_dir = 'coverage_results'
.map do |artifact|
path = artifact['path']
next if !(path.end_with?('/.resultset.json') && path.include?('/coverage/'))
.each_with_index do |resultset, i|
resultset.each do |_, data|
result = SimpleCov::Result.from_hash(['command', i].join => data)
merged_result = SimpleCov::ResultMerger.merged_result
merged_result.command_name = 'RSpec'

Using CircleCI 2.0 deploy step and circleci api v1.1 to merge SimpleCov coverage files of parallel builds

CircleCI 2.0 deploy step doc


  1. Using this we don't HAVE to use workflows.
  2. We get to keep rebuild without cache button. (YAY!!)
  3. We don't have to spin up another container, this work is done in container 0 which is already setup with all configurations etc. So it is bit faster.


I was able to configure this properly and get html report generated using parts from both gists i have mentioned in the main doc in this gist. Some issues i faced were that coverage was not realistically accurate, issue was that i was initializing simplecov a bit late, i move it above all other includes etc. This is literally how beginning looks in my spec_helper.rb file.

require 'simplecov'

  dir = File.join(ENV['CIRCLE_ARTIFACTS'], 'coverage')
SimpleCov.start 'rails' do
  add_filter '/spec/'
  add_filter '/vendor/'

Merge code is in simplecov_merger.rb file(attached in this gist). I choose to run this ruby code directly instead of rake task. You can obviously choose however you want to use this code.

CirclieCI api token setting

You also have to generate a circleci API token for an account which has access to this project on circleci, and set that api token in an env_var in project settings. That environment variable will be available for you than with the name you choose. Replace API_TOKEN in the simplecov_merger.rb file with that environment variable. You probably don't want to commit this token to git as anyone having this token can do whatever he want on circleci.

CircleCI doc on environemtn variables and CircleCI doc for managing your API tokens

Note: To use this file as is without rake tasks, you have to give this file execute permission(using chmod) before committing it in your git repo.

Following is how my .circleci/config.yml file calls this code:

    # Store generated coverage for each build(lookout if you have set a different directory, in which case you will also have to change in rb file)
    - store_artifacts:
        path: coverage
    # Make directory to store aggregated/combined results
    - run:
        name: Stash Coverage Results
        command: |
          mkdir coverage_results
    # This is magic step, which actually will combine coverage reports of all parallel containers
    - deploy:
        name: Merge and copy coverage data
        command: |-
          RUN_COVERAGE=true bundle exec spec/simplecov_merger.rb
    # without RUN_COVERAGE=true you wont get html coverage report generated

    # Now store the results so we can see them in artifacts(coverage report will only be in container 0 as described earlier)
    - store_artifacts:
        path: /home/circleci/projDirec/projName/coverage_results

Using CircleCI 2.0 workflows to merge SimpleCov's generated coverage reports in parallel builds

CircleCI 2.0 Workflows Docs


  1. Intro: Workflows is a nice feature it defines a flow in which different steps of flow are performed. For example for Rspec tests there can be one step, on second step we can check some security problems say from brakeman or any other tool and in next step we can combine coverage reports(for which this gist is). And in another step we can deploy our app.
  2. Every step in workflows initializes a new container and runs only when all previous steps have succeeded. You can define different parallelism and different configuration for each step.
  3. You should also know that at the time of writing, there is no option of rerun without cache option when using workflows, circleci blogs say it is a feature request they are considering to add. But there is no timeline provided.


You can follow this issue for this approach, this gist from trev is provided in the above issue and this was what actually lead me to implement it.

I should say that if you are new to workflows, understanding the configurations and working of workflows can take some time. But at the end you will be able to get it working with these resources.

Only issues i encountered were because of cache keys etc so lookout for that and you will be fine.

Also i was not properly saving generated report and was assuming it is not generating report for some reason. Use ls and other commands to debug within your builds if you encounter such issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.