railsでポートフォリオ作成している初心者の私がcircleCI導入で遭遇したエラー集とその対応をまとめました。
#version
Ruby 3.0.2
Rails 6.0.4
① unable to parse yaml while parsing a block mapping
原因:ymlファイルの記法ミス
circle CIの設定関係なく、タブインデントを使用している等のymlファイル特有の文法に沿わない記述があると発生します。
解決法:
文法チェックのために毎回デプロイするのは面倒なのでCLIをダウンロードしてデプロイ前の下記コマンドで文法チェックを行うことができます。
https://circleci.com/docs/2.0/local-cli にアクセスして、CLI をインストールします。
$ circleci config validate
Config file at .circleci/config.yml is valid.
#この表示が出れば文法ミスはないことが確認できます
②The following gems are missing
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set --local path './vendor/bundle'`, and stop using this flag
The following gems are missing
* mysql2 (0.5.3)
* mysql2 (0.5.3)
* bcrypt (3.1.13)
* bcrypt (3.1.13)
* puma (4.3.8)
* puma (4.3.8)
* bootsnap (1.7.7)
* bootsnap (1.7.7)
* byebug (11.1.3)
* byebug (11.1.3)
* tailwindcss-rails (0.4.2)
* tailwindcss-rails (0.4.2)
* unicorn (6.0.0)
* unicorn (6.0.0)
* nio4r (2.5.8)
* nio4r (2.5.8)
* msgpack (1.4.2)
* msgpack (1.4.2)
* bindex (0.8.1)
* bindex (0.8.1)
* nokogiri (1.12.3)
* kgio (2.11.4)
* kgio (2.11.4)
* raindrops (0.19.2)
* raindrops (0.19.2)
* websocket-driver (0.7.5)
* websocket-driver (0.7.5)
* sassc (2.4.0)
* sassc (2.4.0)
* ffi (1.15.3)
* ffi (1.15.3)
* racc (1.5.2)
* racc (1.5.2)
Install missing gems with `bundle install`
[DEPRECATED] The `--deployment` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set --local deployment 'true'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/.........
Your bundle is locked to tailwindcss-rails (0.4.2) from
https://github.com/dorianmariefr/tailwindcss-rails.git (at minimal@609e9a6), but
that version can no longer be found in that source. That means the author of
tailwindcss-rails (0.4.2) has removed it. You'll need to update your bundle to a
version other than tailwindcss-rails (0.4.2) that hasn't been removed in order
to install.
原因: gem tailwindcss-rails (0.4.2) from
https://github.com/dorianmariefr/tailwindcss-rails.git (at minimal@609e9a6) が既に削除されていて読み込めない
これはエラー文からダイレクトに原因が分かりますがtailwindcss-railsのgemを特定のgit branchからインストールして使用していましたがその元ファイルが削除されていました。
解決法:
#Gemfile
gem 'tailwindcss-rails', git: 'https://github.com/dorianmariefr/tailwindcss-rails.git', branch: 'minimal@609e9a6'
手動でgit以下を削除
gem 'tailwindcss-rails' #これだけ残す
$ bundle install
③NoMethodError: Cannot load database configuration
- run:
name: データベースの作成
command: bundle exec rake db:create
- run:
name: データベースのセットアップ
command: bundle exec rake db:migrate
- run:
name: test
command: bundle exec rspec
- run:
name: Rubocop
command: bundle exec rubocop --fail-level W --display-only-fail-level-offenses
上記のような流れで自動テストを実施した際に
#!/bin/bash -eo pipefail
bundle exec rake db:create
rake aborted!
NoMethodError: Cannot load database configuration:
undefined method `[]' for nil:NilClass
db:create実行時にdatabaseを読み込めないというエラーに遭遇
原因:circleCIのEnvironmental Variablesにmaster.keyを設定していなかった。
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
socket: /tmp/mysql.sock
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: my_app_development
pool: 5
username: username
password: password
host: db
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: my_app_test
pool: 5
username: username
password: password
host: <%= ENV.fetch("APP_DATABASE_HOST") { 'localhost' } %>
production:
<<: *default
database: <%= Rails.application.credentials.db[:database] %>
username: <%= Rails.application.credentials.db[:username] %>
password: <%= Rails.application.credentials.db[:password] %>
socket: <%= Rails.application.credentials.db[:socket] %>
host: <%= Rails.application.credentials.db[:host] %>
master.keyはcredentials.ymlに設定した環境変数を読み込むために必要だとは知っていたがtest環境にはcredentialsを使っていないので見落としていた。
実際はcircleCI実行時にdatabase.yml全体を読み込むのでcircleCIにもrailsのmaster.keyを登録しないと database: <%= Rails.application.credentials.db[:database] %>の中身が空になっているというエラーを生じさせてしまう。
解決法:
従ってcircleCIの「project settings」→「Environment Variables」→「Add Environment Variables」→「host name: RAILS_MASTER_KEY, value: master.keyの中身」と設定する。
④ Net::SSH::AuthenticationFailed: Authentication failed for user ec2-user@EIP
原因: ec2インスタンスログイン用のSSH KEYの設定ミス
解決法:
$ cd ~/.ssh
$ ls
myserver.pem
$ cat myserver.pem #ここで表示された値を全てコピー
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
circleCI 「project settings」→ 「SSH Keys」→ 「Add SSH key」→「Hostname: elasticIP or 独自ドメイン(app.name.com), private key:さっきコピーした値を貼り付け」→ 「fingerprintをコピー」→
- add_ssh_keys:
fingerprints: **:**:**:... #ここにコピーした値を貼り付け
- run:
name: 自動デプロイ
command: bundle exec cap production deploy