ZADorkMan
ZADorkMan

Reputation: 431

Env variables configuration for multiple Heroku remotes ie. testing, staging and production

I am using 3 different git remotes during my development namely testing, staging and production (on Heroku). I have implemented a .env file to handle environment variables but just want to clarify that the way I have set them up is correct.

I have implemented the following to fetch the NODE_ENV var and uses it accordingly to append the .env var strings on the page depending on the NODE_ENV var I set.

env = process.env.NODE_ENV;
envString = env;
SHEET_ID = process.env['SHEET_ID_' + envString];

In the documentation, they discuss default env vars but I am unsure if Heroku has its own NODE_ENV var or how it works exactly, I have been setting it myself in my local .env (then when pushing I change the server NODE_ENV var to testing, staging or production)

Is my following understanding correct?

  1. The NODE_ENV var is located in my .env - is this also default env var served from the environment ie Heroku or am I correct in setting the var myself in the env file and mirroring it on the server?

  2. When pushing to a remote, depending on which, is the correct way to switch the env var NODE_ENV var in my local .env to NODE_ENV=testing, NODE_ENV=staging or NODE_ENV=production? Then the set vars from for eg Heroku dashboard would be used instead of the local vars.

  3. Is this the best way to use the same code base through each remote without having to update my local files?

  4. Do environments like Heroku serve a NODE_ENV var by default? I console logged process.env but only found my local set vars.

I am trying to design things to be as maintainable and efficient as possible and when I eventually do share my code with others I would like it to be as easy to implement as possible.

Upvotes: 0

Views: 43

Answers (1)

Denis Cornehl
Denis Cornehl

Reputation: 4212

Following the 12 factor app principles all configuration should happen in the environment variables.

For your app this means

  • the .env file is merely a helper to put configuration in to the local environment for the application to work
  • every app on heroku (in your case staging,testing,production) will have their own config set via the heroku config commands. They will end up in the local environment of your application dynos, together with whatever your addons put there (for example database credentials).

Upvotes: 1

Related Questions