After setting up some Heroku addons I got used to using environment variables to specify add-on settings. I then started using them for non-addon settings, and now I’ve started using them outside of Heroku a bit too.
One area where they work well is for a Heroku-deployable open source app with OmniAuth GitHub OAuth integration. Here is an example initializer (
config/initializers/omniauth.rb) from a RailsCast:
Rails.application.config.middleware.use OmniAuth::Builder do provider :github, 'CLIENT_KEY', 'CLIENT_SECRET' end
I would replace CLIENT_KEY and CLIENT_SECRET with my own keys, but I can’t commit them to a public repository, or else another person could use them and I could be held responsible. Rather than leave out my client key and secret using a .gitignore and a sample config, I can use an environment variable:
Rails.application.config.middleware.use OmniAuth::Builder do provider :github, ENV['GITHUB_CLIENT_KEY'], ENV['GITHUB_CLIENT_SECRET'] end
With a note in the README about the environment variables used, this should be easy for developers to set this up on their own private apps.
So far I’ve been typing environment variables into the command line when running apps locally and on my VPS, but that’s about to change. I may specify them in my nginx config, which is backed up to a private git repo. I’m not sure yet. Also I’m going to dump out my Heroku environment variables and back them up too.
Heroku has guided me down this path as well. Sometimes I couple it with an initializer to load a (gitignored) YAML file and merge it into the environment, to make setup a bit less manual.
Thanks; I think I’ll do the same thing. I hadn’t yet figured out how I would do it but I knew I might need it. I think the YAML file is a good solution.