LoginSignup
0
0

GitHubアクションを使用してRSpecテストの自動化

Posted at

はじめに

RSpecテストを作成したので、デプロイするたびに自動でテストするようにしたい!
と頑張って書いてみたものの、エラーが発生…
解決したのでまとめておきます

エラーの原因の解決

元々の記述内容↓

rails.yml
name: Rails CI/CD

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ruby-version: ['3.1.2']
    steps:
      - uses: actions/checkout@v4
      - name: Set up Ruby ${{ matrix.ruby-version }}
        uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
        with:
          ruby-version: ${{ matrix.ruby-version }}
      - name: Install dependencies
        run: bundle install --jobs 4 --retry 3
      - name: Run tests
        run: bundle exec rspec spec/ --format documentation

  build:
    runs-on: ubuntu-latest
    needs: [test]
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' && success()
    steps:
    - uses: actions/setup-node@v3
    - name: Deploy
      env:
        PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
        USER_NAME: ${{ secrets.USER_NAME }}
        HOST_NAME: ${{ secrets.HOST_NAME }}
      run: |
        echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
        ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOST_NAME} 'cd go_out_planning &&
        git pull origin main &&
        ~/.rbenv/shims/bundle install &&
        ~/.rbenv/shims/bundle exec rails assets:precompile RAILS_ENV=production &&
        ~/.rbenv/shims/bundle exec rails db:migrate RAILS_ENV=production &&
        if [[ -e tmp/pids/puma.pid ]];then sudo kill $(cat tmp/pids/puma.pid); echo kill puma process;fi &&
        ~/.rbenv/shims/rails s -e production'

このエラーが何度も出ていました↓

Failure/Error: <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

ActionView::Template::Error:
  Webpacker can't find application.js in /home/runner/work/go_out_planning/go_out_planning/public/packs-test/manifest.json. Possible causes:
  1. You want to set webpacker.yml value of compile to true for your environment
     unless you are using the `webpack -w` or the webpack-dev-server.
  2. webpack has not yet re-run to reflect updates.
  3. You have misconfigured Webpacker's config/webpacker.yml file.
  4. Your webpack configuration is not creating a manifest.
     Your manifest contains:

どうやらwebpackerがcompileされていないためにエラーが起きているようです
テストフローの中で compileをする記述をしましたがうまくいかなかったので割愛します
最終の修正結果がこちら↓

rails.yml
name: Rails CI/CD

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ruby-version: ['3.1.2']
    steps:
      - uses: actions/checkout@v4
      - name: Set up Ruby ${{ matrix.ruby-version }}
        uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
        with:
          ruby-version: ${{ matrix.ruby-version }}
      - name: Install dependencies
        run: bundle install --jobs 4 --retry 3
      - name: Set up database schema
        run: bundle exec rails db:create db:schema:load
      - name: Run yarn
        run: yarn
      - name: Run tests
        run: bundle exec rspec spec/ --format documentation

  build:
    runs-on: ubuntu-latest
    needs: [test]
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' && success()
    steps:
    - uses: actions/setup-node@v3
    - name: Deploy
      env:
        PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
        USER_NAME: ${{ secrets.USER_NAME }}
        HOST_NAME: ${{ secrets.HOST_NAME }}
      run: |
        echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
        ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOST_NAME} 'cd go_out_planning &&
        git pull origin main &&
        ~/.rbenv/shims/bundle install &&
        ~/.rbenv/shims/bundle exec rails assets:precompile RAILS_ENV=production &&
        ~/.rbenv/shims/bundle exec rails db:migrate RAILS_ENV=production &&
        if [[ -e tmp/pids/puma.pid ]];then sudo kill $(cat tmp/pids/puma.pid); echo kill puma process;fi &&
        ~/.rbenv/shims/rails s -e production'

下記コードを追加しています

rails.yml
      - name: Set up database schema
        run: bundle exec rails db:create db:schema:load
      - name: Run yarn
        run: yarn

GitHubで他のアプリケーションの記述方法を真似したりしたので詳しいことはわかりませんが、run: yarnを実行することによって、package.jsonのファイルが本番環境に読み込まれてwebpacker compileされたのではないかと思います
Set up databaseは作用していたかどうかは不明です

さいごに

RSpecテストをデプロイ時、マージされる前にすることに成功しました
ネットで調べると色々な方法がありますが、どうやら有料のGitHubアクションは数行多くなるだけで万単位でお金がかかるとのことで節約のためにいろんな記述で工夫しているとのことです

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