Need help troubleshooting deploy context and environment variables

The site’s name is: app-postscriptmedical.netlify.app

I have read the relevant support guides in the forums as well as the docs regarding deploy contexts and the environment variables.

I have set an environment variable via the Netlify UI to be called “ENV_PREFIX” with a value of “PROD_” which I use to prefix other environment variables. Using Netlify’s CLI in my local environment, I can run netlify env:list in my develop and staging branches which yields me the correct prefixes. “PROD_” and “DEV_” respectively.

When I deploy both the develop and staging the “ENV_PREFIX” variable seems to be stuck on the “DEV_” version, no matter what I do. Running netlify dev and testing locally, the branches and their environment variables behave as expected.

The netlify.toml is as follows:

[[headers]]
  for = "/*"
  [headers.values]
    Permissions-Policy = "interest-cohort=()"

[[redirects]]
  from = 'images/*'
  to = '[somewhere]
  status = 200

[[redirects]]
  from = 'api/*'
  to = '.netlify/functions/:splat'
  status = 200

[build]
  command = "npm run build"
  publish = "public"
  functions = "functions"

[dev]
  command = "npm run dev"

[context.production]
  environment = { ENV_PREFIX = "PROD_" }

[context.branch-deploy]
  environment = { ENV_PREFIX = "DEV_" }

[context.staging]
  environment = { ENV_PREFIX = "DEV_" }

I’ve also checked my deploy logs and it does say “Production” when I push into develop and “Branch deploy” when I push into staging.

I would really appreciate any help and assistance that could lead me to figure out what I am doing wrong.

Many thanks.

Hey @Postscript-Medical,

I checked the environment for that deploy and it seems like the correct variable is being used:

image

Could you try logging the variables to the build log by adding env ; prefix to your build command?

Hi @hrishikesh thank you for looking into this and helping me.

I’m going to try and figure out how to do what you are suggesting but I have very limited knowledge of node and build scripts. If you can explain in further detail the steps to achieve this, I would be very grateful.

So, I managed to work something out, I think. I replaced npm run build with npm run env which gave me the list of environment variables in the logs but did not build the site. Then, I replaced it with npm run env && npm run build which caused the environment variables to be listed and the site to be built and deployed.

Now I can see the correct ENV_PREFIX variable being logged and both the develop and staging branches behave as expected when deployed.

I have since reverted the build command back to npm run build, tested a new deploy and verified that both branches behave as expected.

What has been happening up until now I can only guess. Cached something somewhere? I will never know. I’ll keep checking, hopefully this was a hiccup and won’t happen again.

Thank you again for your time @hrishikesh

I take it back.

Something’s still fishy in this setup. I’ve aligned both of my branches commit-wise and I have ended up back to my original situation where the “ENV_PREFIX” does not match the context where it exists. In this case, the staging environment has the “PROD_” prefix when it should not.
I’ve added the npm run env to the build command and I can see the “DEV_” prefix correctly logged but as soon as I try to log in with one of the test accounts, I get an error and I can see in the function logs that “PROD_” is being logged as the environment variable.

What could I possibly be doing wrong?

Hey there, @Postscript-Medical :wave:

Thanks so much for reaching out! Sorry you are still encountering obstacles. Can you share your the portion of your repo that shows how you are configuring your environment prefix? This will help us look into this further.

Hi @hillary,

I’m not sure I follow when you say: “[…] portion of your repo that shows how you are configuring your environment prefix[…]”.

The prefix environment variable is set via the Netlify UI and also in the netlify.toml shared at the top of the thread. It is accessed in the Netlify functions like the following:

const faunadb = require("faunadb");
const q = faunadb.query;
const { getEnvironmentKey } = require("./utils/get-environment-key");
const FAUNA_KEY = getEnvironmentKey("FAUNA_SERVER_KEY", process.env);

exports.handler = (_, context) => {
  const { user } = context.clientContext;

  console.log("KEY", process.env.ENV_PREFIX);

  try {
    const client = new faunadb.Client({
      secret: process.env[FAUNA_KEY],
    });

    return client.query([...])
 } catch (error) {[...]}

And the getEnvironmentKey method is the following:

exports.getEnvironmentKey = (key, { ENV_PREFIX }) => ENV_PREFIX + key;

Hey @Postscript-Medical,

If this is an issue with the environment variables not working correctly in functions, it’s an entirely different issue. Functions don’t have access to the variables set in TOML. You’ve set the value of ENV_PREFIX as PROD_ in Netlify UI. Thus, that will always be returned in functions.

Right, I see. I must have misunderstood things when reading the docs.

If that is the case, is there a guide on how to conditionally pick environment variables inside functions based on the deploy context?

And, thank you both for the assistance.

Would this help:

I shall have a read. Thanks again.

Hopefully this is the missing piece.

Hey @hrishikesh that blog post helped in the sense that it had links that led me to other information that helped me understand what I needed to do and what I had understood wrong - mainly the whole “build time” and “run time” environments plus what is available when. Ultimately I came across Bencao inline-function-env plugin which was what I needed all along.

I was not able to make anything work following what was described on Divya’s post but, hopefully, with time I will.

Once again, thank you for the support and help.

Hi @Postscript-Medical, it’s been a bit since you first reached out to us about this issue but I wanted to share an update in case it helps! We’ve recently released a new beta that introduces scopes and deploy contexts for environment variables. This beta enables you to configure context-specific values directly from the Netlify app UI, meaning that your functions should pick up the correct contextual value as well. Hope this helps and happy to answer any questions or hear your feedback!

Hi there! Thank you for the heads up. I wasn’t aware of this upcoming update.

At a cursory glance it does seem to be what I had originally understood how the context and environment variables working. I will certainly look into this later on and possibly pick it up.

2 Likes

Thanks for letting us know, @Postscript-Medical! Don’t hesitate to follow up on this thread if you have further questions down the road :slight_smile:

That’s great and hope the new feature helps! To echo Hillary, we are always happy to hear feedback or help with any questions you might have. :slight_smile:

1 Like