🚲 production移行あれこれ

productionに移行するとき詰まったことあれこれ。

Missing `secret_key_base` for 'production' environment
Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit` (ArgumentError)

credintails.yml.enc を読み込みたいけど master.key が見つからなくて泣いてるエラー。

言われた通りに bin/rails credentials:edit すると

$ rails credentials:edit No $EDITOR to open file in. Assign one like this: EDITOR="mate --wait" bin/rails credentials:edit For editors that fork and exit immediately, it's important to pass a wait flag, otherwise the credentials will be saved immediately with no chance to edit.

また怒られる。エディターの指定が必要だそうなので指定して実行。

$ EDITOR="mate --wait" bin/rails credentials:edit Adding config/master.key to store the encryption key: 47f3b348f254c78388c35a6ec9930526 Save this in a password manager your team can access. If you lose the key, no one, including you, can access anything encrypted with it. create config/master.key Couldn't decrypt config/credentials.yml.enc. Perhaps you passed the wrong key?

動いた。これでmaster.keyが生成される。

ActiveSupport::MessageEncryptor::InvalidMessage
in `rescue in _decrypt': ActiveSupport::MessageEncryptor::InvalidMessage (ActiveSupport::MessageEncryptor::InvalidMessage)

簡潔に言うと、「 credintails.yml.enc の復号化には master.key が必要だけど、さっき再生成したせいで関係ない master.key になっちゃった」ことに起因する。

解決方法は credintails.yml.enc を今ある master.key で再生成する。

$ EDITOR="mate --wait" bin/rails credentials:edit File encrypted and saved.

さっきと同じ。

public配下のファイルが表示されない

config/environments/production.rbの以下を編集

# ↓これを # config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? # ↓こうする config.public_file_server.enabled = true

テーブルが見つからない

データベースはdevelopment用とproduction用がある。

$ rails db:migrate RAILS_ENV=production

これで本番用のが出来上がる。

いつもの引数なしだとdev用が生成される。