LoginSignup
2
3

More than 1 year has passed since last update.

CircleCIでbundle installされない

Posted at

はじめに

記事内容

私は現在、バックエンドエンジニアを目指し、転職活動中です。
今回はCircleCI導入時、bundle installが実行されないエラーが発生したので、その解決方法について執筆します。

開発環境

  • Ruby 2.7.2
  • Rails 6.1.4
  • Docker 20.10.6
  • MySQL 5.7.32
  • bundler 2.2.23
  • CircleCI 2.1

CircleCIによる自動化項目

  • RuboCop
  • RSpec

.circleci/config.yml

config.yml

version: 2.1
executors:
  default_container:
    docker:
      - image: circleci/ruby:2.7.2-node
        environment:
          RAILS_ENV: test
          BUNDLE_JOBS: 4
          BUNDLE_RETRY: 3
          BUNDLE_PATH: vendor/bundle
          BUNDLER_VERSION: 2.2.23

      - image: circleci/mysql:5.7.32
        command: mysqld --default-authentication-plugin=mysql_native_password
        environment:
          MYSQL_ALLOW_EMPTY_PASSWORD: root
    working_directory: ~/checkcalorie
commands:
  install_bundler:
    description: Bundler install
    steps:
      - run: gem install bundler -v 2.2.23

jobs:
  fetch_source_code:
    executor:
      name: default_container
    steps:
      - checkout
      - save_cache:
          key: v1 -checkcalorie-{{.Environment.CIRCLE_SHA1}}
          paths:
            - ~/checkcalorie
  bundle_dependencies:
    executor:
      name: default_container
    steps:
      - restore_cache:
          key: v1 -checkcalorie-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1 -dependencies-{{ checksum "Gemfile.lock" }}
      - install_bundler
      - run:
          name: Bundle Install Dependencies
          command: |
            bundle install
      - save_cache:
          key: v1 -dependencies-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
  rspec:
    executor:
      name: default_container
    steps:
      - restore_cache:
          key: v1 -checkcalorie-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1 -dependencies-{{ checksum "Gemfile.lock" }}
      - run:
          name: Watting stand up database
          command: |
            dockerize -wait \
            tcp://127.0.0.1:3306 -timeout 120s
      - run: mv ./config/database.yml.ci ./config/database.yml
      - install_bundler
      - run:
          name: Testing DB migration and seed
          command: |
            bundle exec rails db:create db:migrate db:seed db:drop
      - run:
          name: yarn Install
          command: yarn install
      - run:
          name: Run rspec
          command: |
            mkdir /tmp/test-results
            mkdir -p ~/rspec
            bundle exec rails db:create db:migrate
            TEST_FILES="$(circleci tests glob \"spec/**/*_spec.rb\" | circleci tests split --split-by=timings)"
            bundle exec rspec --require rails_helper \
                      --color \
                      --format progress \
                      --format RspecJunitFormatter \
                      --out ~/rspec/rspec.xml
      - store_test_results:
          path: ~/rspec
      - store_artifacts:
          path: ~/tmp/test-results
          destination: test-results
  rubocop:
    executor:
      name: default_container
    steps:
      - restore_cache:
          key: v1 -checkcalorie-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1 -dependencies-{{ checksum "Gemfile.lock" }}
      - install_bundler
      - run:
          name: Execute rubocop
          command: |
            bundle exec rubocop
workflows:
  build:
    jobs:
      - fetch_source_code
      - bundle_dependencies:
          requires:
            - fetch_source_code
      - rubocop:
          requires:
            - bundle_dependencies
      - rspec:
          requires:
            - bundle_dependencies

エラー内容

bundle installされない

  • 以下添付画像のエラーがrubocop、rspecで発生しており、実行されない。

  • CircleCIエラー箇所はそれぞれ1箇所ずつのみ
    スクリーンショット 2021-08-13 14.18.24.png

  • エラー詳細
    スクリーンショット 2021-08-13 14.20.29.png

  • コード

#!/bin/bash -eo pipefail
bundle exec rails db:create db:migrate db:seed db:drop

bundler: command not found: rails
Install missing gem executables with `bundle install`

Exited with code exit status 127
CircleCI received exit code 127

タイポか?

確認するも見当たらない。

cacheしっかりできてるのだろうか?

できてないっぽい。

スクリーンショット 2021-08-13 14.31.35.png

エラー原因

このエラーについてググったところ、下記記事を発見しました。

  • ちなみにこの記事の通り、cacheも保存されていませんでした。

スクリーンショット 2021-08-13 14.43.40.png

localのRubyBundlerのverとCircleCIのverに差異がある

この場合、Gemfile.lockのものが上書きされるとのこと。

解決方法

$ bundle lock --add-platform x86_64-linux

したら解決しました。凡ミスですね。

  • そもそもCircleCIとは、下記トップページ画像にあるように、、、

スクリーンショット 2021-08-13 15.22.30.png

Linux向け仮想マシン、、、

エラー時の自分のGemfile.lockは、

Gemfile.lock

PLATFORMS
  x86_64-darwin-20

となっていました。上記コマンドを実行すると、

Gemfile.lock

PLATFORMS
  x86_64-darwin-20
  x86_64-linux

となります。この状態でエラーは解消されました。

まとめ

ただ使えるだけじゃダメですね。
今後は、使う技術の内容を少しずつ理解していきます。

2
3
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
2
3