Rails 4.1将秘密推向heroku

时间:2014-02-19 23:20:24

标签: ruby-on-rails heroku devise

Rails 4.1.0.beta1和Devise。

我正在尝试从版本控制中删除所有密钥,并且我已升级到Rails 4.1以便为这个新秘密提供一个镜头

尝试将Devise的config.secret_key推送到heroku但是在资产之后失败了:预编译

Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       Devise.secret_key was not set. Please add the following to your Devise initializer:
       config.secret_key = 'EXAMPLE_KEY_HERE'
       Please ensure you restarted your application after installing Devise or setting the key.

以下是我的更改,我将在评论中留下的旧代码。 (它有效)

devise.rb

  # config.secret_key = 'THIS_IS_A_FAKE_KEY' #<---this_is_commented_out
  config.secret_key = Rails.application.secrets.devise_secret_key

secrets.yml

production:
  devise_secret_key: 'THIS_IS_A_FAKE_KEY'

然后运行heroku labs:enable user-env-compile -a myapp(不确定是否有必要)

然后当我推到我的分叉heroku envionment git push forked master时,我得到了上述错误。

我也注意到在这个Devise回购中对此进行了一些讨论,所以我想我会在源代码库中更新我的宝石,没有区别。以下是该讨论的一部分(here)。

2 个答案:

答案 0 :(得分:22)

您可能已经将秘密.yml添加到您的.gitignore中。这是有道理的,因为你把秘密密钥放在其中 - 但是由于Heroku部署使用git,它永远不会看到你的秘密.yml。

一种解决方案是使用heroku_secrets gem - 请参阅https://stackoverflow.com/a/22458102/2831572

另一个解决方案是在使用对环境变量的引用替换所有敏感键后,将secrets.yml添加到git(即从.gitignore中删除它)。 所以:

production:
  devise_secret_key: <%= ENV['DEVISE_KEY'] %>

然后运行heroku config:set DEVISE_KEY='7658699e0f765e8whatever'

答案 1 :(得分:9)

你应该使用figaro gem(https://github.com/laserlemon/figaro),用它来处理秘密真的很容易,而且它对Heroku rake figaro:heroku很有效。