LoginSignup
2
3

More than 3 years have passed since last update.

RubyなリポジトリのGitHub ActionsでCoverallsを使う

Posted at

はじめに

Travis CIからGitHub Actionsに移行する際に、GitHub ActionsでSimpleCovの結果をCoverallsに送信する方法を調べました。simplecov-lcovを使用して、code coverageをlcov形式で出力し、それをCoverallsに送るという感じでした。

SimpleCovとsimplecov-lcovのインストール

Gemfileに以下の二行を追加して、bundle install しました。

gem 'simplecov', '~> 0.21'
gem 'simplecov-lcov', '~> 0.8'

spec/spec_helper.rb の先頭に以下を追加します。Coverallsにcoverageを送る場合に重要になるのは、SimpleCov::Formatter::LcovFormatter に関係する部分です。

require 'simplecov'
require 'simplecov-lcov'

SimpleCov::Formatter::LcovFormatter.config do |config|
  # Coverallsはデフォルトではcoverage/lcov.infoの結果を送信する
  config.report_with_single_file = true
  config.single_report_path = 'coverage/lcov.info'
end

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
  SimpleCov::Formatter::HTMLFormatter, 
  # formatterにSimpleCov::Formatter::LcovFormatterを加える
  SimpleCov::Formatter::LcovFormatter
])

SimpleCov.start

Coveralls GitHub Actions

https://github.com/marketplace/actions/coveralls-github-action に、CoverallsのGitHub Actionsがあります。
coveralls_github_actions-fs8.png

Use latent version ボタンを押すと表示されるモーダルの二行をコピーして、Workflowのymlに追記します。

coveralls_github_actions_code-fs8.png

Workflowの例

code coverageをCoverallsに送信するために .github/workflows/coverage.yml というworkflowファイルを用意しました。重要なのは、前節でコピーした最後の - name: Coveralls GitHub Action からの部分です。github-token は必須のオプションになりますが ${{ secrets.GITHUB_TOKEN }} としておけばOKです。

name: coverage

on: [push, pull_request]

jobs:
  coverage:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - name: Set up Ruby 2.7
        uses: actions/setup-ruby@v1
        with:
          ruby-version: '2.7'
      - name: Build and test with Rake
        run: |
          gem install bundler
          bundle install
          bundle exec rake
      - name: Coveralls GitHub Action
        uses: coverallsapp/github-action@v1.1.2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}

必要かどうかちょっと謎ですが、一行だけの .coveralls.yml を用意しました。

service_name: github-ci

あとはこれらをcommitしてpushすれば、workflowが動きだして、Coverallsに結果が送られます。あらかじめ、Coveralls側でリポジトリの連携をONにしておいてください。

おわりに

CoverallsのGitHub Actionのexampleが、Node.jsのものだったので、Rubyは対応してないのかな〜と思っていたのですが、ちゃんと調べたら簡単でした。Travis CIの頃は、coveralls gemを使っていました。coveralls gemでは、SimpleCovのバージョンにしばりがあるので、今回の様なsimplecov-lcovを使った方法の方が自由で良いですね。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3