5

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.

Community
  • 1
  • 1
Francois
  • 1,367
  • 1
  • 15
  • 23
  • I think the problem is having `auth` as a variable? In the links you provide, none of them uses it that way, they writes in the same line like `gem 'foobar', git: "https://#{ ENV['SECRET_GIT'] }:x-oauth-basic@github.com/Foo/Bar.git"`. – Wit Mar 10 '17 at 08:48
  • I've tried this way too but it's not working either. – Francois Mar 10 '17 at 08:50
  • Ok. Just my curiosity, can we have variables in Gemfile? I can't find reference about that. Can you provide some links? – Wit Mar 10 '17 at 08:53
  • 1
    If you do `foo = 'bar'` then `puts foo #=> 'bar'` in your `gemfile` it works just fine ;) – Francois Mar 10 '17 at 08:55
  • Did you do an `export SECRET_GIT="xxxxx"` before hand or else how are you specifying that env variable in the shell? If you've already done that, and if you're using rails with spring, try stopping the spring server first. – Navin Peiris Mar 10 '17 at 13:39
  • Actually, I did not. As you're pointing it out, I don't specify any environment variable apart from my `heroku_env.rb` file (as explained here: https://devcenter.heroku.com/articles/bundler-configuration#gem-source-username-and-password). I guess I was expecting too much magic from spring / rails. I would like to load my `heroku_env.rb` file whenever I run `bundle` (to share env variables with my team without hassle). – Francois Mar 10 '17 at 15:39

3 Answers3

2

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"
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
  • Thanks for your answer, I edited my post to be more precise: I'm trying to do this in my local environment. I have a `heroku_env.rb` file that's supposed to mock heroku's environment variables (and it's loaded in my `application.rb`). Maybe that's not the right way to do it... – Francois Mar 10 '17 at 08:49
  • ahh I haven't used env variables this way – Deepak Mahakale Mar 10 '17 at 08:54
  • If you only want to use `ENV['SECRET_GIT']` you can use it like `SECRET_GIT=your-api bundle install` – Deepak Mahakale Mar 10 '17 at 08:55
  • The problem with that is that I'm using a `heroku_env.rb` file that is shared with my teammates (I guess they won't be happy to run this command line instead of just `bundle` everytime) :) – Francois Mar 10 '17 at 08:57
  • yeah, I know the pain ;) – Deepak Mahakale Mar 10 '17 at 08:57
1

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

AmShaegar
  • 1,491
  • 9
  • 18
  • Thank you for pointing that out. I guess I'm looking for something like you mentioned (`require 'config/heroku_env'`) but it's not working just like that. Heroku local seems a bit "overkill" for one variable. – Francois Mar 10 '17 at 15:42
  • Is there a way of doing this with an application.yml file using Figaro? – Carpela Apr 06 '17 at 11:40
1

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)
Community
  • 1
  • 1
Francois
  • 1,367
  • 1
  • 15
  • 23