(Last updated on )
GitHub Actions で RuboCop と RSpec を実行し、開発した gem の品質をチェック
GitHub Actions で RuboCop と RSpec を実行し、開発した gem の品質をチェック
GitHub Actions で RuboCop と RSpec を実行し、開発した Gem の品質をチェックした。
GitHub Actions
Rubyでのビルドとテスト - GitHub Docs
を参考に、GitHub Actions を設定した。
ruby-ci.yml を作成し、RuboCop でコードの品質をチェックし、 RSpec でテストを実行するようにした。

.github/workflows/ruby-ci.yml の設定
name: Ruby CI
on:
  push:
    branches:
      - main
  pull_request:
    paths:
      - '**.rb'
  workflow_dispatch:
jobs:
  ruby-ci:
    runs-on: ubuntu-latest
    name: Ruby ${{ matrix.ruby-version }}
    strategy:
      matrix:
        ruby-version:
          - '3.0'
          - '3.1'
          - '3.2'
          - '3.3'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby-version }}
          bundler-cache: true
      - name: Run rubocop
        run: bundle exec rubocop
      - name: Run rspec
        run: bundle exec rspec
費用
GitHub Actions の課金について - GitHub Docs に記載されている通り、パブリックリポジトリでは標準のホステッドランナーを無料で利用できる。 費用が気になっていたので、安心しました。
GitHub Actions の使用は、パブリック リポジトリの標準の GitHub ホステッド ランナーとセルフホステッド ランナーの場合は無料です。
RuboCop
エラー
bundle exec rubocop を実行すると、下記エラーが発生した。
Unable to find gem rubocop-discourse; is the gem installed? Gem::MissingSpecError
エラー対応
RuboCop pulls in .rubocop.yml files from vendor directories · Issue #9832 · rubocop/rubocop
を参考に 'vendor/bundle/**/*' を Exclude に追加した。
.rubocop.yml
  AllCops:
    NewCops: enable
    SuggestExtensions: false
    Exclude:
+     - 'vendor/bundle/**/*'
.rubocop.yml の設定
require:
  - rubocop-performance
  - rubocop-rails
  - rubocop-rspec
AllCops:
  NewCops: enable
  SuggestExtensions: false
  Exclude:
    - 'spec/dummy/**/*'
    - 'vendor/bundle/**/*'
Rails:
  Enabled: true
Metrics/MethodLength:
  Max: 15
Metrics/AbcSize:
  Max: 15
Metrics/ClassLength:
  Max: 100
Style/Documentation:
  Enabled: false
RSpec
.rspec の設定
--color
--require spec_helper
