3
1

More than 3 years have passed since last update.

[rails]credentials.yml.encにdatabase設定を保存している場合のheroku デプロイ

Posted at

ローカルの開発環境

データベースのパスワードなどを平文でdatabase.ymlに保存したくないので、
credentials.yml.encにdatabaseのパスワードなどを保存している。

【備忘録】credentials.yml.encにdatabase設定を保存する

heroku

この環境のままherokuにデプロイし、db:migrateするとherokuのdbに接続できず、エラーになる。

$ heroku rake db:migrate
....
rake aborted!
NoMethodError: Cannot load database configuration:
undefined method `[]' for nil:NilClass

このままheroku openすると以下のようなエラーになってApp Crashedになる。

heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=violao.herokuapp.com request_id=bf9aba32-8c1e-4b44-948b-3d302934f41f fwd="XXX.XXX.XXX.XXX" dyno= connect= service= status=503 bytes= protocol=https

解決方法

master.key をherokuの環境変数としてセットする。

$ heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
Setting RAILS_MASTER_KEY and restarting ⬢ app_name... done, v13
RAILS_MASTER_KEY: xxxxxx601433892f64d2c33e69cxxxxxx

↓参考
secrets.ymlや環境変数をRails 5.2のEncrypted Credentialsに移行する

その後 heroku db:migrateでdbのマイグレーションを行うと、エラーが解消し、heroku openでアプリが起動する。

$ heroku rake db:migrate
3
1
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
3
1