LoginSignup
11
10

More than 3 years have passed since last update.

Rails 5.2をProduction環境に切り替える時にハマった点

Last updated at Posted at 2019-10-06

credentials.ymlではまった。

開発環境である程度できたので本番環境に切り替えようとしたところ、credentials.ymlではまった。
こちら情報を元に色々設定。

credentials.yml.enc
aws:
   access_key_id: AAAA
   secret_access_key: BBBB

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 1111111111aaaaaa...

master.keyも開発環境から持ってきて上のようにひらけた。

export RAILS_ENV=production
rails console
`validate_secret_key_base': Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit` (ArgumentError)

secret_key_baseがないとエラー。。。(なんで?)
以下で見てみると

$ rails console
irb(main):001:0> Rails.application.credentials.aws[:access_key_id]
=> AAAA

irb(main):002:0> Rails.application.credentials.aws[:secret_access_key]
=> BBBB

irb(main):003:0> Rails.application.credentials.secret_key_base
=> nil

secret_key_baseが認識されない
awsのキーをコメントアウト

credentials.yml.enc
#aws:
   #access_key_id: AAAA
   #secret_access_key: BBBB

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 1111111111aaaaaa...
$ rails console
irb(main):001:0> Rails.application.credentials.secret_key_base
=> 1111111111aaaaaa...

認識できた!!
ymlの書き方の問題??と思い
一から書き直したがだめ。
どの情報をみても、credentials.yml.encが見れればOKそう。
訳もわからず、試しに以下の書き方にしてみた。

credentials.yml.enc
# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 1111111111aaaaaa...

aws:
   access_key_id: AAAA
   secret_access_key: BBBB
$ rails console
irb(main):001:0> Rails.application.credentials.secret_key_base
=> 1111111111aaaaaa...

通ったーーーーーー!!!

こんな情報どこにもなかったのですが、なぜかうまく行った。
開発環境だと、訂正前のymlでいけるのに、本番環境に変更すると訂正前の書き方だとうまくいかず。
訂正したら、両環境でうまくいった。
インデントの問題だった気がするが、本番環境に切り替えできたのでよかった。

11
10
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
11
10