LoginSignup
7
8

More than 5 years have passed since last update.

カバレッジレポートを GitLab Pages にホスティングした

Posted at

はじめに

やりたかったこと

コードカバレッジレポートを簡単に見れるようにしたい。

カバレッジのバッジから
スクリーンショット 2018-07-28 0.20.44.png

こんなレポートページへ

スクリーンショット 2018-07-28 0.23.42.png

やったこと

  • SimpleCov を使って Rspec のカバレッジを計測 & レポートを作成
  • GitLab CI を使って上記のカバレッジレポートを GitLab Pages に、ホスティング
    • バッジからのリンクを張る

SimpleCov を導入する

まずはカバレッジレポートを生成するために SimpleCov を導入する。

  1. SimpleCov を Gemfile に追記

    Gemfile
    gem 'simplecov', require: false, group: :test
    
  2. spec_helper.rb で SimpleCov をロード&起動

    spec_helper.br
    require 'simplecov'
    SimpleCov.start 'rails'
    
    # Previous content of test helper now starts here
    

これでテストを実行するとカバレッジレポート (coverage/index.html) が生成されるようになる。

GitLab CI を構成する

生成したカバレッジレポートを GitLab Pages にホスティングするためのパイプラインを作る。
具体的には以下のような .gitlab-ci.yml になる。

.gitlab-ci.yml
...

rspec:
  stage: test
  script:
    - bundle install
    - rspec
  artifacts:
    paths:
      - coverage/

pages:
  stage: deploy
  dependencies:
    - rspec
  script:
    - mv coverage/ public/
  artifacts:
    paths:
      - public
    expire_in: 30 days
  only:
    - master

...
  • rspec ジョブ
    • artifacts: 生成したレポートをストア
  • pages ジョブ
    • dependencies: rspec ジョブで生成した artifacts をダウンロード
    • GitLab Pages は public ディレクトリ内を探しに行くので、ディレクトリ名は必ず public
    • only: master ブランチの更新時のみレポートをデプロイする

カバレッジレポートバッジにリンクを貼る

ここまでで、 master ブランチへのマージのたびに GitLab Pages にカバレッジレポートがデプロイされる状態。
あとはすぐ見に行けるように、 README.md のカバレッジバッジからのリンクを張る。

README.md
[![coverage report](https://gitlab.com/APP_NAME/GROUP_NAME/badges/master/coverage.svg?job=rspec)](https://APP_NAME.gitlab.io/GROUP_NAME/)

...
  • Settings > CI /CD > General piplines からバッジの Markdown を取得
    • デフォルトではリンク先が commits になっているので、これを GitLab Pages のページに変更する
  • job=rspec: テストを実行するジョブ名を rspec 以外にしていたら置き換える
    • 上の例と同じように .gitlab-ci.yml を作成しているならこのままでよい

参考

7
8
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
7
8