Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cant seem to target the correct build in Gatsby Cloud

I recently set up a Gatsby Cloud account for my Gatsby app. I currently have a github workflow that deploys the prod build to AWS, I want to move away from AWS and use G.C.

I have a prod backend and a staging backend, both WordPress.

My goal is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • Whenever I put up a PR, I can see that preview-build targeting/pointing to my staging backend
  • When I merge a PR into master I want the prod build to point to my prod backend

Once I can accomplish the above, I can then update my dns and point to the GC server. Allowing me to leave AWS, but giving me a prod instance and an ephemeral staging instance.

Problem I am facing:

  • Whether it is a preview build from a PR or a "prod" build (merge to master), G.C. seems to only use my development env values.
  • I dont see a way to kick off a specific build script based on a G.C. env var, I am currently using GATSBY_IS_PREVIEW but it seems to be true in both build scenarios

Here is my gatsby-config:

let activeEnv = process.env.GATSBY_IS_PREVIEW ? "development" : process.env.NODE_ENV || "development";

require("dotenv").config({
  path: `.env.${activeEnv}`,
});
console.log(`** Build Env: '${activeEnv}'`);
console.log("** BACKEND URL:", process.env.GATSBY_GRAPHQL_URL);
console.log("** GATSBY_STRIPE_KEY:", process.env.GATSBY_STRIPE_KEY.substring(0, 10));
console.log("** GATSBY_STRIPE_SECRET_KEY:", process.env.GATSBY_STRIPE_SECRET_KEY.substring(0, 10));

Any help is greatly appreciated.

>Solution :

You can use the BRANCH env var, like this:

let activeEnv = process.env.BRANCH === 'master' ? "production" : "development";
require("dotenv").config({
  path: `.env.${activeEnv}`,
});

Gatsby Cloud Env Vars

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading