question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Single build for multiple environment

See original GitHub issue

Summary

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

  1. 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 in globalThis of the browser).
    • Then we use the wrapRootElement of browser api to wrap the app in React.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.
  2. 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 and some 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 the webpack entry. Any help on this front would be appreciated.
  3. 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

  1. Is there a better way to do this?
  2. Will replacing strings in generated js/ html files cause any issue?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Miyoucommented, Nov 9, 2020

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.

2reactions
MattFantocommented, May 26, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found