I am running a Gatsby site, now that I have branch deploys working and being deployed to the right subdomain, I would like to set an ENVIRONMENT VAR for my STAGING Branch Deploy.
I have been reading all the threads I have found , but had not success yet.
Development and Production were working fine using .env.XXX files. until now, but that’s Gatsby normal build process.
I have been trying to create a .end.STAGING file without sucess, now I am moving the variables to netlify.toml but still struggling to get it working.
thanks for answering, I do not think this is the answer. AFAIK GATSBY_ variables are used to exposed them to the client.
I only need them to used them at Build time (so, Server side, so to say).
I have been able to explore more, now my build command in netlify.toml is:
command = “yarn env && gatsby build”
And in the log file I can see variables are being set:
I did a test repo to make sure, since it’s been awhile since I’ve done this. I created a staging branch and changed the Netlify app to deploy the branch staging.
I did see the process.env.BUILD_ENV from the node env correctly from context.staging.
Also process.env.CONTEXT shows as branch-deploy, so you could also check off of that in your build code to check for a non production branch deploy. process.env.CONTEXT for the production build did have production, so the statement in my last reply does not apply as far as I can see.
I assume this is because Gatsby is forcing NODE_ENV to be Production when “gatsby build” is run, and then reading from the .env.production file no matter what.
It looks like you’re experiencing something similar to what we debugged in this thread below. Can you please take a look and follow the proposed steps?
Additionally, I want to dive into this point and hopefully clarify some differences between server-side variable and build time variables:
Server-side variables are variables accessible to functions and defined in the UI. Build time variables are defined in your TOML and are not accessible to functions. Knowing this, could you save your variables in the Netlify UI rather than using .env files?
thanks for the answer, let me answer your two suggestions.
About the first thread, yes I already read it before posting. According to Gatsby documentation variables GATSBY_xxx are used to be accessed by the browser as exposed here:
This is not what I want, my vars are only needed in build time to change the endpoints being accessed.
To your second point. Just to clarify, I do not need these variables to be accessed by any functions, just Gatsby Build process. I have tested both approaches having .env files and setting them up in the toml file.
No success.
As I want the variables to have different values for production and staging (branch deployment) using the UI is not a valid option, my reasoning:
UI does not allow me to have different values per different deployment type, they are all shared.
Then, I would need to use different VAR names, like. PROD_VAR, STAGING_VAR
Having different VAR names is forcing me to detect in build time in what type of deployment the build is being run to use one or the other
and then I am back to my original problem…
Gatsby is not reading the variables setup neither in .env.staging, neither in TOML file.
I am sure this is some mis-configuration on my side, or not clearly understanding something in Gatsby, but I am blocked with this right now.
I will make some more tests today and try to bring logs and examples with TOML file defined variables.
I think it was clue on how you could possibly try to access the variables. If setting it in .env file is not working, you could set it in the build command like how Hillary has suggested. You could choose to exclude the GATSBY_ prefix.
thanks for the answer, the truth is that I am completely lost now, so I am going to post my current status just in case someone finds this helpful but I am going to focus on some other priorities now.
In summary, variables are defined in TOML file, injected through the build command and I am using them in two different files to test how gatsby accesses them.
Parts of my Build Log file:
7:43:38 PM: Found a Gatsby cache. We’re about to go FAST.
7:43:38 PM: BUILD_ENV = staging
7:43:38 PM: Calculated = staging
7:45:05 PM: success Writing page-data.json files to public directory - 0.004s - 10/10 2241.06/s
7:45:10 PM: CSS_CLASS=undefined
7:45:10 PM: WEB_URL=undefined
7:45:10 PM: CSS_CLASS=undefined
7:45:10 PM: WEB_URL=undefined
7:45:10 PM: CSS_CLASS=undefined
7:45:10 PM: WEB_URL=undefined
7:45:10 PM: success Building static HTML for pages - 4.695s - 10/10 2.13/s
7:45:11 PM: success onPostBuild - 0.066s
Seems that the TOML variables are accesible at some point while building but not when the real react build is going on.
I have tested that also with .env files and the result is a bit better, only .env.production variables are read.
At least the app ends with some version of the variables.
Without .env files, gatsby is not reading even the production declared vars.
Very rare.
I do not expect an answer just leaving this here for future members