Skip to content

Instantly share code, notes, and snippets.

@leemeichin
Last active December 18, 2019 08:29
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leemeichin/1aeb217d989f3981cc3f06d88938bd33 to your computer and use it in GitHub Desktop.
Save leemeichin/1aeb217d989f3981cc3f06d88938bd33 to your computer and use it in GitHub Desktop.
CircleCI 2.0 - Merge parallel coverage results for CodeClimate

CircleCI 2.0 Parallel Builds and CodeClimate

Add the Ruby script to your repo, and add this snippet into your new circle.yml:

- deploy:
    name: Merge and copy coverage data
    command: bundle exec path/to/circle.rb

It will only work if the tests pass.

#!/usr/bin/env ruby
# originally based on https://gist.github.com/evanwhalen/f74879e0549b67eb17bb
require 'codeclimate-test-reporter'
require 'open-uri'
# ENV['PROJECT_NAME'] => 'github/github'
api_url = "https://circleci.com/api/v1.1/project/github/#{ENV['CIRCLE_PROJECT_USERNAME']}/#{ENV['CIRCLE_PROJECT_REPONAME']}/#{ENV['CIRCLE_BUILD_NUM']}/artifacts?circle-token=#{ENV['CIRCLE_TOKEN']}"
artifacts = open(api_url)
coverage_dir = '/tmp/coverage'
SimpleCov.coverage_dir(coverage_dir)
JSON.load(artifacts)
.map { |artifact| JSON.load(open("#{artifact['url']}?circle-token=#{ENV['CIRCLE_TOKEN']}")) }
.each_with_index do |resultset, i|
resultset.each do |_, data|
result = SimpleCov::Result.from_hash(['command', i].join => data)
SimpleCov::ResultMerger.store_result(result)
end
end
merged_result = SimpleCov::ResultMerger.merged_result
merged_result.command_name = 'RSpec'
CodeClimate::TestReporter.run(merged_result.to_hash)
@ziaulrehman40
Copy link

I am trying to use this technique to merge and store builds locally instead of code-climate. Any help will be appreciated.

@ziaulrehman40
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment