在Heroku上使用ENV文件进行制作 [英] Using ENV files in production on Heroku

查看:93
本文介绍了在Heroku上使用ENV文件进行制作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 production.rb 环境文件中包含以下配置行,如本文

  config.cache_store =:mem_cache_store, ENV [MEMCACHEDCLOUD_SERVERS]。split(','),{:username => ENV [MEMCACHEDCLOUD_USERNAME],:password => ENV [MEMCACHEDCLOUD_PASSWORD]} 

但是当我尝试部署时,出现错误:
$ b


运行:rake资产:预编译rake中止!

未定义方法 split'for nil :NilClass

/tmp/build_abdc.../config/environments/production.rb:107:in
block in'


这是因为配置变量在编译期间不可用。有一个Heroku实验室插件,你可以用它来弥补这一点,但Heroku发出警告:使用此实验室功能与Heroku最佳实践相反。



那么在生产配置中使用ENV变量时,最佳做法是什么?他们是否应该全部被包装在救援处理程序中,以便Heroku在编译过程中忽略它们? 我们最终只是检查ENV分配之前的var。看起来这就是你在Heroku的config / initializers中使用ENV变量时需要的模式:

 #注意:ENV变量不是'因此必须检查它们是否存在:
如果ENV [MEMCACHEDCLOUD_SERVERS]
config.cache_store =:mem_cache_store,ENV [MEMCACHEDCLOUD_SERVERS]。split(','),{ :username => ENV [MEMCACHEDCLOUD_USERNAME],:password => ENV [MEMCACHEDCLOUD_PASSWORD]}
结束

另请参阅:
https://devcenter.heroku.com/articles / rails-asset-pipeline#failure-in-the-assets-precompile-task


I've got the following configuration line in my production.rb environment file, as instructed in this article:

  config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }

But when I try to deploy, I'll get an error:

Running: rake assets:precompile rake aborted!
undefined method split' for nil:NilClass
/tmp/build_abdc.../config/environments/production.rb:107:in
block in '

This is because config vars are not available during compilation. There's a Heroku labs add-on that you can supposedly use to remedy this, but it comes with a warning from Heroku that "Using this labs feature is considered counter to Heroku best practices."

So then what is the best practice when it comes to using ENV vars in production config? Should they just all be wrapped in rescue handlers so that Heroku ignores them during compilation?

解决方案

We ended up just checking for the ENV var before assigment. Looks like that's the pattern you need whenever you use ENV vars in config/initializers on Heroku:

  # NOTE: ENV vars aren't available during slug comiplation, so must check if they exist:
  if ENV["MEMCACHEDCLOUD_SERVERS"]
    config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }
  end

See also: https://devcenter.heroku.com/articles/rails-asset-pipeline#failures-in-the-assets-precompile-task

这篇关于在Heroku上使用ENV文件进行制作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆