LoginSignup
0
0

初めてGitHub Actionsを使ってRailsのRSpecを自動化したときの備忘録

Posted at

GitHub Actionsでpushをトリガーにrailsのrspecを起動させるコードを初めて作成したときの記録です。色々エラーが発生したので、メモしました。

GitHub Actionsコード

name: Run RSpec

on:
  push:

jobs:
  rspec:
    name: RSpec Tests
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0.31
        ports:
          - 3306:3306
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
        options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 10

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.1.3
          bundler-cache: true

      - name: Set up Node.js (for Rails assets)
        uses: actions/setup-node@v2
        with:
          node-version: '14'

      - name: Bundler and gem install
        run: |
          gem install bundler
          bundle install --jobs 4 --retry 3 --path vendor/bundle

      - name: Database setup and migrate
        run: |
          cp config/database.yml.ci config/database.yml
          bundle exec rails db:create RAILS_ENV=test
          bundle exec rails db:migrate RAILS_ENV=test

      - name: Run rspec
        run: bundle exec rspec

上記のコードをgit push origin <ブランチ名>のコマンドでプルリクエストを実行したところ、以下のエラーが発生。

Your bundle only supports platforms ["x86_64-darwin-22", "x86_64-linux-musl"]
but your local platform is x86_64-linux. Add the current platform to the
lockfile with
`bundle lock --add-platform x86_64-linux` and try again.
Error: The process '/opt/hostedtoolcache/Ruby/3.1.3/x64/bin/bundle' failed with exit code 16

エラーはGemfile.lockに指定されているプラットフォームがGitHub Actionsが動作しているプラットフォーム(この場合x86_64-linux)と一致していないことを示しているようです。

対処

Gemfile.lockにx86_64-linuxプラットフォームを追加します。
(ちなみに私のGemfile.lockにはx86_64-linuxプラットフォームはありませんでした)。
私の環境ではDockerコンテナで作業しているので、以下のコマンドを入力していきます。

1.コンテナ内でshを起動します。

docker-compose exec api sh

(apiの部分はご自身の環境によって変わります)。
上記コマンドを入力したらエンターキーを押す。

2.コンテナ内で、Gemfile.lockにx86_64-linuxプラットフォームを追加します。

上記1でエンターキーを押すと、以下の/app #が表示されるので、
#の後に、bundle lock --add-platform x86_64-linuxを入力し、エンターキーを押す。

/app #

3.コンテナを終了します。
exitを入力し、エンターキーを押す。

exit

4.エラーの対処はできたので、変更をステージングし、コミットします。

git add Gemfile.lock
git commit -m "Add x86_64-linux platform to Gemfile.lock"

5.修正をリモートリポジトリにpushします。

git push origin <ブランチ名>

先ほどのエラーは解消されましたが、今度はdatabase sestup and migrateの方で以下のエラーになりました。

cp: cannot stat 'config/database.yml.ci': No such file or directory
Error: Process completed with exit code 1.

エラーメッセージは、config/database.yml.ciのファイルが存在していないと言っています。

対処

1.config/database.yml.ciのファイルを作成し、CI/CD環境で使用するデータベース接続設定を記述します。

test:
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  host: 127.0.0.1
  database: <データベース名>_test

2.記述が完了したら、以下コマンドを実行

git add config//database.yml.ci
git commit -m "Add database.yml.ci for CI/CD environment"
git push origin <ブランチ名>

以上で、私の環境ではpushイベントのたび、rspecが起動するようになりました。

その他試したこと

コンテナ内でshではなくbashで起動したらどうなるか試してみました。

docker-compose exec api bash

すると、以下のようになりました。

OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown

bashシェルがそのコンテナイメージ内に存在しないか、またはPATH内にないことを示しています。ですので、docker-compose exec shを実行しました。

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