问题
I'm having trouble using an environment variable in my Gemfile.
I'm trying to load a gem from a private Github repository with an API key:
auth = ENV['SECRET_GIT']
gem 'foobar', git: "https://#{auth}:x-oauth-basic@github.com/Foo/Bar.git"
But if I puts
my ENV['SECRET_GIT']
variable, there's nothing in it.
I though you could do it this way because of these (especially the first one):
- https://devcenter.heroku.com/articles/bundler-configuration#gem-source-username-and-password
- https://stackoverflow.com/a/7338154/5353193
- Deploying to Heroku with environment variables in Gemfile
Bundler version 1.14.6
ruby 2.4.0p0
Thank you for you help
EDIT
I'm trying to do this in my local environment, I guess there would be no problem doing this on heroku.
回答1:
Well yes you can set it from console
heroku config:set SECRET_GIT=your-api-key
Or, you can set the environment variables from heroku dashbord
heroku > your-app > settings > Config variables
And add a new entry
SECRET_GIT = your-api-key
And now you can use it directly in Gemfile
gem 'foobar', git: "https://#{ENV['SECRET_GIT']}:x-oauth-basic@github.com/Foo/Bar.git"
回答2:
From your comments I understand that you expect the heroku config to be available in local development. This is not the case as you recognized.
You need to require that heroku_env.rb
you mentioned in your Gemfile. The Gemfile is just a plain Ruby file executed in a specific context. So, you should be able to just add require 'config/heroku_env'
to the top - or whatever your path is. Remember to omit the .rb
at the end.
Alternatively, give heroku local
a try: https://devcenter.heroku.com/articles/heroku-local
回答3:
I came out with a solution thanks to the replies I got (especially https://stackoverflow.com/a/42718962/5353193).
I was expecting that spring or something would magically load my environment file (as nothing was specified for local environment here).
I put this code at the beginning of my Gemfile:
env = 'config/env.rb'
load(env) if File.exist?(env)
来源:https://stackoverflow.com/questions/42713765/environment-variable-in-gemfile