Cronwel
Cronwel

Reputation: 113

Environment variables in Rails, where to put for development testing before deploying

Where do I put environment variables in Rails and can I make a file for checking the environment before running?

In a MERN stack app(all javascript), I created a 3 files in a config folder:

keys.js dev.js prod.js

keys.js has the following code:

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./prod');
} else {
  module.exports = require('./dev');
}

dev.js contains all of the variables for google, mongo, stripe, etc. 'prod.js' has the following code:

module.exports = {
  googleClientID: process.env.GOOGLE_CLIENT_ID,
  googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
  mongoURI: process.env.MONGO_URI,
  cookieKey: process.env.COOKIE_KEY,
  stripePublishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
  stripeSecretKey: process.env.STRIPE_SECRET_KEY,
  sendGridKey: process.env.SENDGRID_KEY,
  redirectDomain: process.env.REDIRECT_DOMAIN
};

This config folder is in the root of the app structure.

I've done this to deploy to Heroku and it works perfectly. My development env allows me to test the APIs as it would in production. It's been ignored within my repo so the dev.js is not available on Github.

My question: where to I put the such a file in a Rails application? Can I create such a file to store my env variables?

I'd like to use the same logic if possible and I can figure the code out to do so but I'm tepid about where to put the file(s).

Upvotes: 0

Views: 732

Answers (1)

Finn
Finn

Reputation: 1580

I like using the Figaro gem to manage my ENV variables in Rails.

Setup

  1. Add the gem to your Gemfile:
    gem 'figaro'
  2. Install the gem by running $ bundle install
  3. Set up Figaro: $ bundle exec figaro install (this creates a file called application.yml under the config/ directory)
  4. Add your ENV variables to the new config/application.yml file

Use

Add your ENV variables to the config/application.yml file. For example,

# config/application.yml

google_client_id: '7381a978f7dd7f9a1117'
google_client_secret: 'abdc3b896a0ffb85d373'
mongo_uri: 'https://www.example.com'
...

You can then use the ENV variables in your Rails code, wherever you need them: ENV['google_client_id']

Different ENV variables and values for different environments...

You can specify different values for your variables for different environments, say development and test. Here is an example from Figaro's README:

# config/application.yml

pusher_app_id: "2954"
pusher_key: "7381a978f7dd7f9a1117"
pusher_secret: "abdc3b896a0ffb85d373"

test:
  pusher_app_id: "5112"
  pusher_key: "ad69caf9a44dcac1fb28"
  pusher_secret: "83ca7aa160fedaf3b350"

Pushing ENV variables to Heroku

It sounds like you're using Heroku. Figaro makes it easy to deploy all your Rails environment variables to Heroku at once: figaro heroku:set -e production

More

There is a lot more you can do with Figaro, just check out their Github Readme.

Upvotes: 3

Related Questions