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) |
Here is an updated gist covering this technique and another technique(using workflows) to generate coverage reports locally. It also generates html report.
Happy coding!