Gemfile中的环境变量

我在Gemfile中使用环境变量时遇到问题。
我正在尝试使用API​​密钥从私有Github存储库加载gem:

auth = ENV['SECRET_GIT'] gem 'foobar', git: "https://#{auth}:x-oauth-basic@github.com/Foo/Bar.git" 

但是如果我puts我的ENV['SECRET_GIT']变量放在其中,那么它就没有了。
我可以这样做因为这些(特别是第一个):
– https://devcenter.heroku.com/articles/bundler-configuration#gem-source-username-and-password
– https://stackoverflow.com/a/7338154/5353193
– 使用Gemfile中的环境变量部署到Heroku

Bundler版本1.14.6
ruby2.4.0p0

谢谢你的帮助

编辑
我想在我的本地环境中这样做,我想在heroku上做这个没有问题。

是的,你可以从控制台设置它

 heroku config:set SECRET_GIT=your-api-key 

或者,您可以从heroku dashbord设置环境变量

 heroku > your-app > settings > Config variables 

并添加一个新条目

 SECRET_GIT = your-api-key 

现在您可以直接在Gemfile使用它

 gem 'foobar', git: "https://#{ENV['SECRET_GIT']}:x-oauth-basic@github.com/Foo/Bar.git" 

根据您的评论,我了解您希望heroku配置可在本地开发中使用。 你认识到的情况并非如此。

您需要在heroku_env.rb中要求提供heroku_env.rb。 Gemfile只是在特定上下文中执行的普通Ruby文件。 所以,您应该只需require 'config/heroku_env'到顶部 – 或者无论您的路径是什么。 记得最后省略.rb


或者,尝试使用heroku local : https : //devcenter.heroku.com/articles/heroku-local

我得到了一个解决方案,感谢我收到的回复(特别是https://stackoverflow.com/a/42718962/5353193 )。
我期待那个spring或某些东西会神奇地加载我的环境文件(因为这里没有为本地环境指定)。

我把这段代码放在我的Gemfile的开头:

 env = 'config/env.rb' load(env) if File.exist?(env)