Skip to content

Instantly share code, notes, and snippets.

@evanwhalen
Last active February 18, 2018 18:54
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanwhalen/f74879e0549b67eb17bb to your computer and use it in GitHub Desktop.
Save evanwhalen/f74879e0549b67eb17bb to your computer and use it in GitHub Desktop.
circleci-parallel-codeclimate
#!/usr/bin/env ruby
require "codeclimate-test-reporter"
branch = ENV['CIRCLE_BRANCH']
node_index = ENV['CIRCLE_NODE_INDEX'].to_i
node_total = ENV['CIRCLE_NODE_TOTAL'].to_i
artifacts_dir = ENV['CIRCLE_ARTIFACTS']
coverage_dir = File.join("..", "..", "..", ENV['CIRCLE_ARTIFACTS'], "coverage")
filename = '.resultset.json'
SimpleCov.coverage_dir(coverage_dir)
# Only run on node0
exit if !node_index.zero?
if node_total > 0
# Copy coverage results from all nodes to circle artifacts directory
1.upto(node_total-1) do |i|
node = "node#{i}"
node_artifacts_dir = `ssh #{node} 'printf $CIRCLE_ARTIFACTS'`
from = File.join(node_artifacts_dir, 'coverage', filename)
to = File.join(coverage_dir, "#{i}#{filename}")
command = "scp #{node}:#{from} #{to}"
puts "running command: #{command}"
`#{command}`
end
end
# Merge coverage results from other nodes
# .resultset.json is a hidden file and thus ignored by the glob
files = Dir.glob(File.join(coverage_dir, "*#{filename}"))
files.each_with_index do |file, i|
resultset = JSON.load(File.read(file))
resultset.each do |command_name, 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'
# Format merged result with html
html_formatter = SimpleCov::Formatter::HTMLFormatter.new
html_formatter.format(merged_result)
# Only submit coverage to codeclimate on master branch
exit if branch != 'master'
# Post merged coverage result to codeclimate
codeclimate_formatter = CodeClimate::TestReporter::Formatter.new
codeclimate_formatter.format(merged_result)
@kruppel
Copy link

kruppel commented Feb 29, 2016

exit if branch != 'master

Any reason to not move that to the top?

@earnold
Copy link

earnold commented May 3, 2016

Thanks for this! It was very helpful!

@ksykulev
Copy link

👍 nice.

@dangrahn
Copy link

dangrahn commented Mar 23, 2017

Thanks for this!

I had to make a slight change to the script to get it to work with 0.4.8 version of codeclimate-test-reporter. When formatting and posting final results to Code Climate I had to do as follows:

# Post merged coverage result to codeclimate
formatter = CodeClimate::TestReporter::Formatter.new
formatted_results = formatter.format(merged_result.to_hash)
CodeClimate::TestReporter::PostResults.new(formatted_results).post

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