以下の内容はhttps://ama-tech.hatenablog.com/rails-github-actions-ci-lint-testより取得しました。


【Rails/GitHub Actions】CI環境を構築する

GitHub Actionsを使ってRailsアプリのCI環境を構築しました。

本記事では、以下の2つのワークフローの実装と発生したエラーへの対応について解説します。

  • lint.yml:push時にbin/lintを実行
  • test.yml:push時にbundle exec rspecを実行

1. 開発環境

  • Ruby:3.3.0
  • Rails:7.1.3.2
  • rspec-rails:6.1.0
  • tailwindcss-rails:2.3.0
  • PostgreSQL:16.4
  • MacBook Pro (13-inch, 2020)
  • macOS Sequoia 15.2

2. lint.yml

starter-workflows/ci/rubyonrails.ymlを元に作成しました。

# .github/workflows/lint.yml
name: "Lint"
on: push

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    - name: Setup Ruby
      uses: ruby/setup-ruby@v1.207.0
      with:
        bundler-cache: true
    - name: Lint Ruby files
      run: bin/lint

3. test.yml

starter-workflows/ci/rubyonrails.ymlを元に作成しました。

# .github/workflows/test.yml
name: "Test"
on: push

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:11-alpine
        ports:
          - "5432:5432"
        env:
          POSTGRES_DB: rails_test
          POSTGRES_USER: rails
          POSTGRES_PASSWORD: password
    env:
      RAILS_ENV: test
      DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test"
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    - name: Setup Ruby
      uses: ruby/setup-ruby@v1.207.0
      with:
        bundler-cache: true
    - name: Set up database schema
      run: bin/rails db:schema:load
    - name: Run tests
      run: bundle exec rspec

3-1. bin/rails db:schema:load実行時のエラー

test.ymlを実行するとSet up database schemaステップのbin/rails db:schema:loadで以下のエラーが発生しました。

  bin/rails db:schema:load
  shell: /usr/bin/bash -e {0}
  env:
    RAILS_ENV: test
    DATABASE_URL: ***localhost:5432/rails_test
bin/rails aborted!
NoMethodError: undefined method `[]' for nil (NoMethodError)

    Rails.application.credentials.github[:client_id],

CIを実行したアプリではOmniAuthを使ったGitHubログイン機能が実装されており、config/credentials.yml.encの内容は以下のようになっています。

# config/credentials.yml.enc
github:
  client_id: "xxx"
  client_secret: "xxx"

CI環境でconfig/credentials.yml.encが適切に読み込まれておらず、Rails.application.credentials.githubnilになることでエラーが発生していたため、GitHub Actionsのシークレットにconfig/master.keyの内容を設定しワークフローに追加しました。

# .github/workflows/test.yml
    env:
      RAILS_ENV: test
+     RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
      DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test"

この修正によりconfig/credentials.yml.encが正しく復号化され、エラーが解消されました。

3-2. bundle exec rspec実行時のエラー

次にRun testsステップのbundle exec rspecで以下のエラーが発生しました。

Run bundle exec rspec
  bundle exec rspec
  shell: /usr/bin/bash -e {0}
  env:
    RAILS_ENV: test
    RAILS_MASTER_KEY: ***
    DATABASE_URL: ***localhost:5432/rails_test

省略

Failures:

  1) hoge
     Failure/Error: = stylesheet_link_tag 'tailwind', 'inter-font', 'data-turbo-track': 'reload'

     ActionView::Template::Error:
       The asset "tailwind.css" is not present in the asset pipeline.
     # ./app/views/layouts/application.html.slim:9:in `_app_views_layouts_application_html_slim___2497370432981056635_29280'
     # ./spec/hoge/hoge_spec.rb:14:in `block (3 levels) in <main>'
     # ------------------
     # --- Caused by: ---
     # Sprockets::Rails::Helper::AssetNotFound:
     #   The asset "tailwind.css" is not present in the asset pipeline.
     #   ./app/views/layouts/application.html.slim:9:in `_app_views_layouts_application_html_slim___2497370432981056635_29280'

CIを実行したアプリではtailwindcss-railsを使用しており、事前にアセットのプリコンパイルが必要だったためテスト実行前にbin/rails assets:precompileを実行するように修正しました。

+   - name: Precompile assets
+     run: bin/rails assets:precompile
    - name: Set up database schema
      run: bin/rails db:schema:load
    - name: Run tests
      run: bundle exec rspec

最終的なワークフローファイルは以下のようになりました。

# .github/workflows/test.yml
name: "Test"
on: push

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:11-alpine
        ports:
          - "5432:5432"
        env:
          POSTGRES_DB: rails_test
          POSTGRES_USER: rails
          POSTGRES_PASSWORD: password
    env:
      RAILS_ENV: test
      RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
      DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test"
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    - name: Setup Ruby
      uses: ruby/setup-ruby@v1.207.0
      with:
        bundler-cache: true
    - name: Precompile assets
      run: bin/rails assets:precompile
    - name: Set up database schema
      run: bin/rails db:schema:load
    - name: Run tests
      run: bundle exec rspec

【参考】




以上の内容はhttps://ama-tech.hatenablog.com/rails-github-actions-ci-lint-testより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14