Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow environment variable overrides #4570

Closed
tremby opened this issue Mar 16, 2018 · 15 comments
Closed

Allow environment variable overrides #4570

tremby opened this issue Mar 16, 2018 · 15 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. status: needs more info Needs triaging and reproducible examples or more information to be resolved type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@tremby
Copy link
Contributor

tremby commented Mar 16, 2018

I'm aware that Gatsby will pass in any GATSBY_* environment variables. But this doesn't help in my current use case.

I have three environments: development, staging, and production.

Staging should be a production-like build, and therefore NODE_ENV needs to be production.

But that means I can't load in the environment variables I need (which are different from the production ones) from .env.staging, since only .env.$NODE_ENV is loaded.

I also can't override for example API_URL because it doesn't start with GATSBY_.

Perhaps it would make sense to allow, in addition to GATSBY_*, any env vars found in the .env file, and if any are found, let the env vars take precedence? Alternatively, allow somewhere to configure a whilelist of such env var names?

By the way, my current workaround is to consume these vars in my code as process.env.GATSBY_API_KEY || process.env.API_KEY but this seems ugly and verbose to me.

@m-allanson
Copy link
Contributor

Hey @tremby, Can you tell me if I've understood your use case correctly? You've got these three environments:

development environment

NODE_ENV=development

.env.development file:

API_KEY = devApiKey

staging environment

NODE_ENV=production

.env.production file:

API_KEY=prodApiKey

production environment

NODE_ENV=production

.env.production file:

API_KEY=prodApiKey

But in the staging environment you want API_KEY to be something like API_KEY=stageApiKey?

@tremby
Copy link
Contributor Author

tremby commented Mar 23, 2018

That's right.

@camsjams
Copy link
Contributor

camsjams commented Mar 27, 2018

I have the same need for this use, I've used Gatsby in production for about a year now and have different things like API keys, account Ids for metrics, and a few other items that are environment specific.

For me its more complex than just configs per environment, as I also need to set NODE_ENV=production to have minified builds:

DEV
  accountId: dev_account1
  NODE_ENV=production
QA
  accountId: qa_account1
  NODE_ENV=production
STAGING
  accountId: stg_account1
  NODE_ENV=production
PRODUCTION
  accountId: prod_account1
  NODE_ENV=production

@m-allanson
Copy link
Contributor

There's some new docs on handling additional environments: https://www.gatsbyjs.org/docs/environment-variables/#additional-environments-staging-test-etc

Added via #4858

@alvis
Copy link
Contributor

alvis commented Apr 27, 2018

The ability to overwrite env vars is needed I think. Recently I'm facing a similar problem upon deploying an A/B test. I was setting up multiple sites with different titles and slogans, deploying via netlify. While I can set up multiple .envs, I found it more easy to manage via just changing the environment variables supplied to netlify.

@camsjams
Copy link
Contributor

@m-allanson Thanks for this update - this appears to be exactly what I need! (I somehow missed the notification from 15 days ago so also thanks to @alvis for bumping)

@ThiagoMiranda
Copy link

ThiagoMiranda commented Jun 7, 2018

@m-allanson Hi.
Following the docs about handling additional staging environments I can only add this "per file"? For example:
I have a .env.staging file with a diferent base_api_url then the production one
So I´ve added the snippet code in my gatsby-config.js file but it only works on the gatsby-config.js file itself not in the actual file that I use the base_api_url inside my src folder. How can I "overwrite" it to respect my .env.staging variables in all files not just the ones that´ve added the dotenv code?

Thanks

@mbergeronupgrade
Copy link

mbergeronupgrade commented Aug 2, 2018

Same setup of stage, dev, production environments with different env files for all 3. However only the .env.production is used when outputting minified builds, so the other envs.

Fix for now is having the build machine set the environment variables pre-build instead of counting on NODE_ENV for dotenv to load the proper variables file. This is also what is recommended to do by The Twelve Factor App.

or have a build script in package.json that is like:

"build:dev": "NODE_ENV=production GATSBY_SOME_VAR=somevalue gatsby build"

gets a bit ugly if there are tons of variables though

@kakadiadarpan
Copy link
Contributor

@ThiagoMiranda can you provide a sample reproduction repo showing your use case? That would make it much easier to help you out.

@mbergeronupgrade did you went through the link shared by @m-allanson?

@kakadiadarpan kakadiadarpan added type: question or discussion Issue discussing or asking a question about Gatsby status: needs more info Needs triaging and reproducible examples or more information to be resolved labels Sep 21, 2018
@kakadiadarpan
Copy link
Contributor

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub issues, we have to clean some of the old issues as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Gatsby version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@kakadiadarpan kakadiadarpan added the stale? Issue that may be closed soon due to the original author not responding any more. label Oct 23, 2018
@kakadiadarpan
Copy link
Contributor

This issue is being closed because there hasn't been any activity for at least 30 days. Feel free to open a new one if you still experience this problem 👍

@jgonera
Copy link

jgonera commented May 9, 2019

Confirming that what @ThiagoMiranda and @mbergeronupgrade mentioned is still an issue with Gatsby 2.4.3. I don't have a repro repo but it seems that files in src/ never pick up variables loaded in gatsby-config.js from .env.${activeEnv}.

For example, let's say I have .env.development with the following content:

MY_VAR=hello-dev

and .env.production with:

MY_VAR=hello-prod

Let's say I build with GATSBY_ACTIVE_ENV=development npm run build and I have src/components/Something.tsx which uses process.env.MY_VAR to render something.

I would expect grepping public/ for hello-prod to yield no results and grepping for hello-dev to show the rendered component, but it's not the case (it's the other way round).

@Ronserruya
Copy link

@jgonera I encountered that issue on 2.1.31, updated to 2.13.52 and it works fine, you might want to try that.

@supert56
Copy link

I have also just encountered this issue and seem to get an error when trying to upgrade gatsby to 2.13.52 - sharp.node requires version 6001.0.0 or later, but libglib-2.0.dylib provides version 5801.0.0

@sumitsg10
Copy link

For example, let's say I have .env.development with the following content:

MY_VAR=hello-dev
and .env.production with:

MY_VAR=hello-prod
Let's say I build with GATSBY_ACTIVE_ENV=development npm run build and I have src/components/Something.tsx which uses process.env.MY_VAR to render something.

I would expect grepping public/ for hello-prod to yield no results and grepping for hello-dev to show the rendered component, but it's not the case (it's the other way round).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. status: needs more info Needs triaging and reproducible examples or more information to be resolved type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests