LoginSignup
7
7

More than 5 years have passed since last update.

Encrypted Credentialsに設定した変数を環境毎に出し分けたい

Last updated at Posted at 2018-12-25

Encrypted Credentials

Rails5.2より config/secrets.yml の代わりに入った
config/credentials.yml.enc です。
(他の方の丁寧な記事がたくさんありますので、詳しい説明は割愛します。)

Rails6.0からは公式サポートされそうです。
参考: Add support for multi environment credentials.
https://github.com/rails/rails/pull/33521

環境毎(production, development等)に異なる値をファイル内で
簡単に出し分けできたらと考えていました。

方法

以下のような書き方をしてみました。

config/credentials.yml.enc
development:
  hoge: hoge123456

production:
  hoge: hogehoge123

これを以下のように呼んでみます。

Rails.application.credentials[Rails.env.to_sym][:hoge]

結果

実行環境
Rails 5.2.2
Ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin15]

$ bundle exec rails c
Loading development environment (Rails 5.2.2)
[1] pry(main)> Rails.application.credentials[Rails.env.to_sym][:hoge]
=> "hoge123456"


$ bundle exec rails c -e production
Loading production environment (Rails 5.2.2)
[1] pry(main)> Rails.application.credentials[Rails.env.to_sym][:hoge]
=> "hogehoge123"

うまく出し分けできてそうです!😃

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