1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【CircleCI】【RSpec】【mysql】CircleCIでRSpecを自動化するときに詰まったこと

1
Last updated at Posted at 2020-07-18

Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

<%= ENV['DB_HOST'] %> に渡してあげる感じ

.circleci/config.yml
- image: circleci/ruby:2.7.1-node-browsers
    environment:
      RAILS_ENV: test
      DB_HOST: 127.0.0.1
- image: circleci/mysql:8.0.21
    command: |
      mysqld --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: app_test
      MYSQL_ROOT_PASSWORD: 任意
config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: 任意
  host: <%= ENV['DB_HOST'] %> 

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

NoMethodError: Cannot load database configuration: undefined method `[]' for nil:NilClass

ローカルはこんな感じでRails.application.credentialsを渡して開発していた。
.circleci/config.ymlではRAILS_ENV=testを渡しているので開発環境関係ないやろと思ってたんだけど、案の定Error吐かれた。
pushする時は消すか、もうちょっとうまく環境変数でやれると思う。

config/database.yml
development:
  <<: *default
  database: app_development
  username: <%= Rails.application.credentials.mysql[:username] %>
  password: <%= Rails.application.credentials.mysql[:pass] %>

参考

DockerオフィシャルのMySQL(MariaDB)コンテナの挙動をDockerfile周辺から読み解く
【CircleCI】設定とエラー対処法
CircleCIでRails+MySQLプロジェクトの自動テストを行うための最低限の設定
CircleCIでmysql8を使用する設定

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?