在 Rails 和 Heroku 中使用 credentials.yml [英] Using credentials.yml with Rails and Heroku
问题描述
我在 Rails 5.2 上有一个应用程序,它以前托管在 DigitalOcean 上,但我需要在 heroku 上托管它.我一直在读到 heroku 无法读取 Credentials.yml 因为它在 gitignore 上,当然我不希望它公开.
I have an app on Rails 5.2 and it was previously hosted on DigitalOcean, but I need to host it on heroku. I've been reading that heroku can't read Credentials.yml of because it's on gitignore and of course I don't want it public.
所以我的关键变量是这样的(和 redis 的例子):
So my key variables are like this (and example with redis):
host: Rails.application.credentials.redis_host,
password: Rails.application.credentials.redis_password
Heroku 无法读取此内容.所以我的问题是将其更改为 heroku ENV 变量的最佳方法是什么?我是否需要将所有当前的键(大约有 340 个)编辑为 ENV['SOMEKEY']?
Heroku can't read this. So my question is what is the best approach to change that in to heroku ENV variables? Do I need to edit all my current keys (there about 340) to ENV['SOMEKEY']?
感谢您的帮助!
推荐答案
创建凭据.yml 和主密钥:
Create credentials.yml and master key:
rails credentials:edit
编辑凭据:
EDITOR=vim rails credentials:edit
使用 VIM:
- 用于插入
- 按 i//进行必要的编辑
- 退出时按 Esc
- :wq//用于退出和保存
- :q!//不保存就退出
CREDENTIALS.YML 外观示例:
EXAMPLE OF HOW CREDENTIALS.YML can look:
development:
github:
client: acascascsacascascasc
secret: vdsvsvg34g34g
production:
github:
client: 34g3rvv
secret: erberb43
查找凭据:
rails c
Rails.application.credentials.dig(:aws, :access_key_id)
或者如果使用了环境变量
or if an env variable is used
Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id]
credentials.yml 文件不应在 gitignore 中.
解密凭据的主密钥应该在 gitignore 中.
要在生产中设置主密钥:
heroku config:set RAILS_MASTER_KEY=123456789
或
heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
这就是您需要了解的有关 Ruby on Rails 凭据的全部信息.祝你好运:)
That's all you need to know about credentials in Ruby on Rails. Good luck :)
这篇关于在 Rails 和 Heroku 中使用 credentials.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!