2
0

More than 3 years have passed since last update.

【MiniMagick使用時】CircleCIのテスト環境で躓いたので備忘録

Posted at

twitterで毎日積み上げ報告しています。よかったらフォローお願いします。
https://twitter.com/engineer_ikuzou

状況

ローカル環境では上手くいっていたRSpecテストが、CircleCI上ではエラーになる事案が発生しました。画像整形のために使用したminimagickが原因で生じたエラーです。エラーログは以下の通りです。

開発環境

  • Docker
  • Rails6
  • CircleCI(自動テスト)
  • RSpec

使用技術

  • carrierwave (画像アップロードのためのgem)
  • minimagick (画像整形のためのgem)

エラーログ(CircleCIのtestジョブ)

Failure/Error: let(:food_record) { create(:food_record) }

ActiveRecord::RecordInvalid:
  バリデーションに失敗しました: 画像を入力してください, 画像MiniMagickがファイルを処理できませんでした。画像を確認してください。エラーメッセージ: You must have ImageMagick or GraphicsMagick installed
./spec/models/food_record_spec.rb:4:in `block (2 levels) in <top (required)>'
./spec/models/food_record_spec.rb:31:in `block (3 levels) in <top (required)>'

対処① ⇨ 失敗

エラーログに言われた通り、ImageMagickをインストールしないとMiniMagickが動作しない様です。ローカル環境では、いつの間にかImageMagickをインストールしてたんですね。
そこで、.circleci/config.yml のbuildジョブで、パッケージのインストールを行う様にしました。

circleci/config.yml
        (省略)

jobs:
  build: # our first job, named "build"
    docker:
      - image: cimg/ruby:2.7-node # use a tailored CircleCI docker image.
    steps:
      - checkout # pull down our git code.
      - ruby/install-deps # use the ruby orb to install dependencies
      # use the node orb to install our packages
      # specifying that we use `yarn` and to cache dependencies with `yarn.lock`
      # learn more: https://circleci.com/docs/2.0/caching/
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      - run: sudo apt-get update
      - run: sudo apt-get -y install imagemagick   #<buildジョブで追加しました!!>

         (省略)

# We use workflows to orchestrate the jobs that we declared above.
workflows:
  version: 2
  build_and_test:
    jobs:
      - build
      - test:
          requires:
            - build

エラーは出ず、ログを確認してもインストールは成功している様に見えるが、先ほどのエラーと変わらず。
何かImageMagickをセットアップするために、必要なものがbuilsジョブの時点では揃っていないのかもしれないということで、次の対応を行いました。

対処② ⇨ 成功

testジョブ内で、MagickImageをインストール

circleci/config.yml
      (省略)

  test:  # our next job, called "test"
    # we run "parallel job containers" to enable speeding up our tests;
    # this splits our tests across multiple containers.
    parallelism: 3
    # here we set 3 docker images.
    docker:
      - image: cimg/ruby:2.7-node # this is our primary docker image, where step commands run.
      - image: circleci/postgres
        environment: # add POSTGRES environment variables.
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test
        name: db
      - image: selenium/standalone-chrome
        name: chrome
    # environment variables specific to Ruby/Rails, applied to the primary container.
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: db
      RAILS_ENV: test
      SELENIUM_REMOTE_URL: http://chrome:4444/wd/hub
    # A series of steps to run, some are similar to those in "build".
    steps:
      - checkout
      - ruby/install-deps
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      - run: sudo apt-get update
      - run: sudo apt-get -y install imagemagick  #<testジョブで追加しました!!>

    (省略)

最後に

こちらの対処法で上手く行きました!
エラーの原因の詳細まで究明することはできなかったのですが、同じエラーにハマった方の一助となれば幸いです。
また、もしこちらの原因がわかるという方がいらっしゃいましたら、お手数ですがコメントの方で教えていただけると大変嬉しいです。

参考資料

実際にエラーに奮闘する姿は以下のプルリクエストをご覧ください。
https://github.com/naokikubo2/cookinglife_app/pull/15

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