Rails 4.1将秘密推向heroku

Chr*_*res 11 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.
Run Code Online (Sandbox Code Playgroud)

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

devise.rb

  # config.secret_key = 'THIS_IS_A_FAKE_KEY' #<---this_is_commented_out
  config.secret_key = Rails.application.secrets.devise_secret_key
Run Code Online (Sandbox Code Playgroud)

secrets.yml

production:
  devise_secret_key: 'THIS_IS_A_FAKE_KEY'
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 23

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

一种解决方案是使用heroku_secrets gem - 请参阅/sf/answers/1572067171/.

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

production:
  devise_secret_key: <%= ENV['DEVISE_KEY'] %>
Run Code Online (Sandbox Code Playgroud)

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