Single build for multiple environment
See original GitHub issueSummary
Hello.
For our gatsby project, we’d like to build it once and use it for multiple environments( say testing
and staging
). Only the environment variables are different for each environment.
Say our .env
file looks like this,
SITE_URL=https://testorstage.willbedifferentforeach.env
SOME_API_KEY=abc1212xyz
MY_API_ENDPOINT=https://api.mysite.com
SOME_OTHER_KEY=qwertyabcd
Possible approaches
- Loading the env vars from a
static/env.js
script:- We create a
static/env.js
and declare the env vars in it( which will load them inglobalThis
of the browser). - Then we use the
wrapRootElement
ofbrowser api
to wrap the app inReact.Context
and load the script from there. - Works well for env vars that are required in the react components.
- Does not work for
SITE_URL
var which is used by robot-txt plugin.
- We create a
- Re-running webpack on the
public
folder:- We replace the environment variables by some unique placeholders, like,
SITE_URL=https://some.placeholder.url SOME_API_KEY=SOME_API_KEY_PLACEHOLDER
- Re-running webpack with
DefinePlungin
andsome HTML/ text replacement plugin
, replacing the placeholder string with the real keys(or text). - I have not tried this, as I am not sure how the wepack config would look like.
webpack-stats.json
does look like an interesting file to get thewebpack entry
. Any help on this front would be appreciated.
- We replace the environment variables by some unique placeholders, like,
- Running CLI tools on the
public
folder:- Similar to the above approach.
- We replace the environment variables by some unique placeholders, like,
SITE_URL=https://some.placeholder.url SOME_API_KEY=SOME_API_KEY_PLACEHOLDER
- Using some cli tools( say
sed & grep
or others), replace placeholder strings with actual env vars. - Currently testing, if this works.
Questions
- Is there a better way to do this?
- Will replacing strings in generated js/ html files cause any issue?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Build once deploy to multiple environments - Tim Deschryver
In our quest to build a twelve-factor app where we build once and deploy to multiple environment we encountered some problems, ...
Read more >How To Build An Angular App Once And Deploy It To Multiple ...
Building only one bundle is quite easy but it leads to one big challenge we need to consider: How can we pass environment-specific...
Read more >A single build for multiple environments #3855 - vitejs/vite
I'm using a single build approach and configuring a script directly in HTML manually in each environment that makes some global variables available....
Read more >next.js multiple environments build | by Dzung Nguyen - Medium
During development, we met a problem with multiple env builds. ... Every single changed, Next rebuilds your web app into “.next” directory, but...
Read more >pipeline: how to build for multiple environments?
hello, I understand that "deployment" allows the env specific variable values. ... two variables(one for each environment) like $TEST_PORT and $DEV_PORT.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can we please reopen this and get some response from the Gatsby team? I’m having the same issue and I feel like someone in the Gatsby team must have encountered this and have a good solution for it.
I’m facing this problem as well and as Miyou I think it should be reopened. This is, of course, a common challenge of any web application, but some framework provides a solution out of the box (e.g. https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration).
It would definitely be helpful if an official solution is provided instead of relying on some custom solution