Github ActionでCIのテストレポートを取得


プルリクエストをした後、Github Actionは自動的にテストを実行し、
一般的には合格か不合格の結果だけが表示されます。
テストレポートをどのように取得するのでしょうか？
以下にいくつかの方法を紹介します。

## Codecov

Codecovに登録すると、自動的にアクセス可能なリポジトリが読み込まれます。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3191031/6f109eca-5018-4f79-ecff-c1dc0b247131.png)
`setup repo`，をクリックし、手順に従って設定します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3191031/cd41ef99-f02e-0a51-625d-d134ba66f685.png)

Railsで以下の設定が必要です。

### gemを追加
```
# Gemfile

group :test do
  ...
  gem 'simplecov'
  gem 'simplecov-cobertura'
end
```

### テストレポートのフォーマットを設定
```ruby
# test_helper.rb
require 'simplecov'
require 'simplecov-cobertura'

SimpleCov.start do
  coverage_dir 'tmp/coverage'
end

SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
```

### actionを設定
```yml
- name: Upload coverage reports to Codecov
  uses: codecov/codecov-action@v3
  with:
    token: ${{ secrets.CODECOV_TOKEN }}
    files: ./tmp/coverage/coverage.xml
    fail_ci_if_error: true
```

### 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3191031/8757bce8-464c-3719-bfb0-00587f176922.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3191031/b0aa2408-bedd-f0e0-84ba-4ead8ee7b888.png)


## simplecov-report-action

最もシンプルなテストカバレッジレポート

### actionを設定
actionを設定するだけでOKです
```yml
- name: Simplecov Report
  uses: aki77/simplecov-report-action@v1
  with:
    token: ${{ secrets.GITHUB_TOKEN }}
    failedThreshold: 10
    resultPath: tmp/coverage/.last_run.json
```

### 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3191031/d7c7222d-83b2-23c7-bb1c-749848bb8b0d.png)

## cobertura-action

より詳細なテストカバレッジレポート


### gemを追加
```
# Gemfile

group :test do
  ...
  gem 'simplecov'
  gem 'simplecov-cobertura'
end
```

### テストレポートのフォーマットを設定
```ruby
# test_helper.rb
require 'simplecov'
require 'simplecov-cobertura'

SimpleCov.start do
  coverage_dir 'tmp/coverage'
end

SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
```

### actionを設定
```yml
- uses: 5monkeys/cobertura-action@master
  with:
    path: tmp/coverage/coverage.xml
    repo_token: ${{ secrets.GITHUB_TOKEN }}
    minimum_coverage: 30
    show_missing: true
    show_class_names: true
    link_missing_lines: true
    report_name: code coverage results
```

### 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3191031/091e8d81-2916-d8b1-e4c2-3e20a8f2f89e.png)

# 完成例
```yml
name: Rails Ci

on:
  pull_request:
    branches:
      - develop
    types:
      - opened
      - synchronize
      - ready_for_review

concurrency: 
    group: ${{ github.workflow }}-${{ github.ref }}
    cancel-in-progress: true

env:
  GH_TOKEN: "${{secrets.GH_TOKEN}}"

jobs:
  test:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
      with:
        token: ${{ secrets.GH_TOKEN }}
    - name: Set up Ruby
    # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
    # change this to (see https://github.com/ruby/setup-ruby#versioning):
      uses: ruby/setup-ruby@v1
      with:
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically
    - name: Run tests
      run: bundle exec rake test
    - name: Upload coverage reports to Codecov
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        files: ./tmp/coverage/coverage.xml
        fail_ci_if_error: true
    - name: Simplecov Report
      uses: aki77/simplecov-report-action@v1
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        failedThreshold: 10
        resultPath: tmp/coverage/.last_run.json
    - uses: 5monkeys/cobertura-action@master
      with:
        path: tmp/coverage/coverage.xml
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        minimum_coverage: 80
        show_missing: true
        show_class_names: true
        link_missing_lines: true
        report_name: code coverage results
```