LoginSignup
7
2

More than 3 years have passed since last update.

CircleCIでrailsアプリのimageをアップグレードしたらmysqlに接続できなくなった話

Last updated at Posted at 2019-10-18

概要

railsのアプリでrubyのバージョンを上げようと思って.circleci/config.ymlのimageを変更したらmysqlに接続できなくなってちょっとはまったので共有しておきます。

結論

  • mysql-clientパッケージ名がdefault-mysql-clientに変わったので変更する。
  • bundle installのキャッシュの消さないとmysql-clientのパッケージが変わったので接続できない。

やったこと

config.ymlを以下のように変更してpush。

※ config.ymlは関係ない部分を省略してます。

.circleci/config.yml
version: 2.1
jobs:
  build:
    docker:
-   - image: circleci/ruby:2.6.1-node-browsers
+   - image: circleci/ruby:2.6.3-node-browsers
    - image: circleci/mysql:5.7-ram
    steps:
    - run:
        name: Install depencies
        command: |
          sudo apt-get install -y mysql-client
    - restore_cache:
        keys:
        - cache-bundle-{{ checksum "Gemfile.lock" }}
        - cache-bundle-
    - run:
        name: bundle
        command: |
          bundle install --jobs=4 --retry=3 --path vendor/bundle
    - save_cache:
        paths:
          - ./vendor/bundle
        key: cache-bundle-{{ checksum "Gemfile.lock" }}

CirclCIでInstall depenciesのところでこんなエラーでこけました。

Package mysql-client is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'mysql-client' has no installation candidate
Exited with code 100

こちらが参考になりましたが、パッケージ名がmysql-clientからdefault-mysql-clientに変わったので変更します。

.circleci/config.yml
version: 2.1
jobs:
  build:
    docker:
    - image: circleci/ruby:2.6.3-node-browsers
    - image: circleci/mysql:5.7-ram
    steps:
    - run:
        name: Install depencies
        command: |
-         sudo apt-get install -y mysql-client
+         sudo apt-get install -y default-mysql-client

これでインストールは成功しますが、今度はbundle exec rails db:migrate --traceしてるところで下記のようなエラーで止まります。

#!/bin/bash -eo pipefail
bundle exec rails db:migrate --trace
rails aborted!
LoadError: libmariadbclient.so.18: cannot open shared object file: No such file or directory - /home/circleci/test_dir/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.2/lib/mysql2/mysql2.so
/home/circleci/test_dir/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
/home/circleci/test_dir/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
...以下長いバックトレース

mysql-clientが変わったのでbundle installをやり直してmysql2native extensionsコンパイルをやり直さないといけません。bundle installは時間がかかるので大抵の人がキャッシュしてると思います。多分皆さんここではまるんじゃないかと。

今回はキャッシュの名前を変更して対応しました。

.circleci/config.yml
    - restore_cache:
        keys:
-       - cache-bundle-{{ checksum "Gemfile.lock" }}
+       - cache-bundle-v1-{{ checksum "Gemfile.lock" }}
-       - cache-bundle-
+       - cache-bundle-v1-
    - run:
        name: bundle
        command: |
          bundle install --jobs=4 --retry=3 --path vendor/bundle
    - save_cache:
        paths:
          - ./vendor/bundle
-       key: cache-bundle-{{ checksum "Gemfile.lock" }}
+       key: cache-bundle-v1-{{ checksum "Gemfile.lock" }}
7
2
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
7
2