Thanks to visit codestin.com
Credit goes to github.com

Skip to content

environment context variables do not override build variables #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
naipath opened this issue Sep 26, 2018 · 46 comments
Closed

environment context variables do not override build variables #59

naipath opened this issue Sep 26, 2018 · 46 comments
Labels
type: bug code to address defects in shipped code

Comments

@naipath
Copy link
Contributor

naipath commented Sep 26, 2018

If you define environment variables in the netlify.toml only the build.environment variables are being used.

Eg:

[build.environment]
  GREETING="Hello World! I am a variable set in a .env file"

# When building on the production environment this variable will NOT be used.
[context.production.environment]
  GREETING="production greeting"
@swyxio
Copy link
Contributor

swyxio commented Oct 11, 2018

hi Naipath, i've checked with our support and unfortunately this is a bug in our platform. :(

The fix is to set the environment variable inside the app.netlify.com UI itself.

This is totally on us. Sorry. The requisite bug report has been filed.

@nesymno
Copy link

nesymno commented Oct 12, 2018

@sw-yx but if I need them? I need them to separate prod and stage environment. May be you know a way to do this?

@swyxio
Copy link
Contributor

swyxio commented Oct 17, 2018

yes @weijinnx, you set up the env variables inside the app.netlify.com UI itself. netlify also provides you extra variables you can use inside your code: https://www.netlify.com/docs/continuous-deployment/#build-environment-variables

@swyxio swyxio closed this as completed Oct 17, 2018
@shmargum
Copy link
Contributor

Hi, I see that this issue was closed, but was it resolved?
I am having similar issues when trying to use environment variables for deploy-preview in my lambda functions.

@swyxio
Copy link
Contributor

swyxio commented Dec 21, 2018

i see this. now that im rereading this issue im not sure why our support thought this is a netlify functions issue. it looks like there is a related issue with deployed netlify functions reading environment variables, but possibly separate from this one.

if you deploy this does it work? so can we confirm that its just a netlify-lambda issue?

@8eecf0d2
Copy link
Contributor

Afaict this is working as intended, the context when using netlify-lambda is not production, it could potentially be development or something, but the context needs to be set somewhere..

I might be forgetting but does netlify-lambda provide a cli argument to set the context and or is any context use by default?

@shmargum
Copy link
Contributor

yup, the branch fixes it for me πŸ‘

@shmargum
Copy link
Contributor

my use case is for [context.deploy-preview.environment] and [context.branch-deploy.environment]

@swyxio swyxio reopened this Dec 21, 2018
joelalejandro added a commit to counterfactual/monorepo that referenced this issue Dec 26, 2018
joelalejandro added a commit to counterfactual/monorepo that referenced this issue Dec 27, 2018
…#377)

* playground-server: passing webpack config to build

* playground-server: fixed local routing issues

* playground-server: fixed environment name

* playground-server: forcing Netlify to ack productive env

* playground-server: adding DEBUG=* to see where the router breaks

* playground-server: added some console.log()'s to debug

* playground-server: fixed env-var bug (see netlify/netlify-lambda#59)

* playground-server: replace NODE_ENV with PLAYGROUND_SERVER_ENV
@shmargum
Copy link
Contributor

@8eecf0d2 the context gets set automatically by netlify;
for production is [context.production]
for pull requests/deploy previews: [context.deploy-preview]
for all branch deploys: [context.branch-deploy]
and for a specific branch: [context.name-of-my-custom-branch]

@8eecf0d2
Copy link
Contributor

8eecf0d2 commented Dec 27, 2018

@shmargum right, I was talking about netlify-lambda which afaict doesn't set the context.

@shmargum
Copy link
Contributor

the process.env.CONTEXT is being set correctly when the netlify-lambda build command runs on the netlify servers, this is why the pull request works
I actually now realize that my pull request doesn't take into account the process.env.BRANCH in order to override things specific to a branch (branch-deploy affects all branches but we can also have branch specific overrides such as for the staging branch)

@swyxio
Copy link
Contributor

swyxio commented Jan 10, 2019

we have merged #94 which should address this - please reopen/comment if any further issues

@swyxio swyxio closed this as completed Jan 10, 2019
@pgarciacamou
Copy link

I'm using netlify-lambda@^1.4.3 and still have this issue with lambda functions in deploy-preview.

[context.production.environment]
  CONTACT_FORM_EMAIL = "[email protected]"

[context.deploy-preview.environment]
  CONTACT_FORM_EMAIL = "[email protected]"

When using deploy-preview-1--my-app.netlify.com, emails are still sent to [email protected].

It is worth mentioning that we also have the build environment variables set in the UI, should I remove those?

@swyxio
Copy link
Contributor

swyxio commented Mar 27, 2019

i dont understand what this means, "the build environment variables set in the UI".

its worth opening up netlify-lambda in node_modules, and tracing through to see what is going on with the context object so you can debug. i'm worried about that hyphen in deploy-preview. i'm not sure how we handle that.

@pgarciacamou
Copy link

@sw-yx I'm working on creating a cleaned up public repo to show the issue, stay tuned.

i dont understand what this means, "the build environment variables set in the UI".

Screen Shot 2019-03-27 at 1 37 40 PM

i'm worried about that hyphen in deploy-preview

That's how it is done in the TOML docs in netlify: https://www.netlify.com/docs/netlify-toml-reference/#getting-started

@pgarciacamou
Copy link

pgarciacamou commented Mar 27, 2019

Ok, here you go. Let me know if this is enough info to explain what I mean.

I created a lambda function that only returns Hello <process.env.WHO_AM_I>!. In production it should be "World", in staging (deploy previews) should be "Developer".

Production: https://netlify-context-in-previews.netlify.com/.netlify/functions/hello should return "Hello World!"

Deploy Preview 1: https://deploy-preview-1--netlify-context-in-previews.netlify.com/.netlify/functions/hello should return "Hello Developer!"

Both return "Hello Build!", as WHO_AM_I was set to "Build" when creating the app (see image below). My understanding is that it should be overwritten, correct? Am I doing something wrong?


Repo: https://github.com/pgarciacamou/netlify-context-in-previews

// functions/hello.js
exports.handler = async event => {
  const { WHO_AM_I } = process.env;

  return {
    statusCode: 200,
    body: `Hello ${WHO_AM_I}!`
  };
}
# netlify.toml
[build]
  command = "npm run build"
  functions = ".netlify/functions/"

[context.production.environment]
  WHO_AM_I = "World"

[context.deploy-preview.environment]
  WHO_AM_I = "Developer"

Netlify App: (notice the ENV variable)

Screen Shot 2019-03-27 at 4 06 32 PM

@pgarciacamou
Copy link

I think this issue should be reopened. Unless there is something not stated in the docs that must be done to change the context when building with netlify-lambda.

@swyxio
Copy link
Contributor

swyxio commented Mar 28, 2019

ok thank you for the detailed repro so i can understand your issue.

so first of all, netlify-lambda is just a tool for local emulation of functions and building of them for deployment into the main Netlify service. anything main service related as you have shown above, is not at all part of netlify-lambda's responsibility. so this is not actually the right place to resolve this issue.

however, we still care that you are seeing this behavior that is unexpected. Basically, what is being set in the UI is overriding the more specific context dependent env variables set from netlify.toml. I agree that this is unintuitive. I'm referring this to our support team, who have probably seen this before. please stay tuned...

@swyxio
Copy link
Contributor

swyxio commented Mar 29, 2019

@pgarciacamou we have a possible bug that might be worth trying a small fix for - can you try clicking the Trigger deploy button to see if it might update the new env variables from your netlify.toml?

@pgarciacamou
Copy link

pgarciacamou commented Mar 29, 2019

@sw-yx

Anything main service related as you have shown above, is not at all part of netlify-lambda's responsibility.

You are totally correct, this issue has nothing to do with netlify-lambda, my bad πŸ˜…. Would you mind pointing me to the best place to add this issue?

we have a possible bug that might be worth trying a small fix for - can you try clicking the Trigger deploy button to see if it might update the new env variables from your netlify.toml?

I tried it but the issue is still there. If you want me to, I can add you to the app so you don't have to wait for me, although it might be easier & faster (and more flexible) for you to fork the repo and deploy to netlify.

@swyxio
Copy link
Contributor

swyxio commented Apr 2, 2019

thanks for your patience :) as with everything, the best place is Netlify Support: https://www.netlify.com/support/ - they have the right process and knowledge to help you. in this case, we suspect that there may be a known bug that is in our backlog to solve. in the mean time, Support can work with you to suggest workarounds, and/or notify you when the bug is done. best to take it there.

@renschler
Copy link

@pgarciacamou did you get help from support? If yes would you mind adding your solution here: https://community.netlify.com/t/netlify-toml-context-env-variables-do-not-apply-to-functions/410

I tried what they suggested but still can't get my staging functions to have staging values for the Env Variables.

@pgarciacamou
Copy link

pgarciacamou commented Apr 12, 2019

@renschler I did get help but did not work. The response to the email I sent was:

Yup, that's a known bug. Env vars set in the netlify.toml file are not accessible to netlify functions. I've added you to the issue that is tracking that and we'll let you know when we get a fix out for it. For now, you will need to set your env var in the UI and handle the context changes via the build command.

Like so: eval MY_CONTEXT=${MY_UI_VARIABLE} && npm run build

That example is based on this: https://stackoverflow.com/questions/42107655/travis-can-you-construct-env-var-using-other-env-vars

Hope that helps.

That was Dennis from Netlify. NOTE: he mentioned he added me to the issue, but I believe this is an internal issue because he did not attach a link.

That said, I did not try using eval, I tried using env-cmd as explained in https://facebook.github.io/create-react-app/docs/deployment#customizing-environment-variables-for-arbitrary-build-environments (you can look at the PR of my attempt here pgarciacamou/netlify-context-in-previews#2). NOTE: it looked promising, but it did not work so I just closed it and moved on.


I tried what they suggested but still can't get my staging functions to have staging values for the Env Variables.

I might quickly try eval tomorrow morning (PST). If you try it before I do, please let me know of your findings.

@renschler
Copy link

I tried eval and it also didn't work. I updated the post in netlify community.

Maybe the shorterm solution is just to push different versions of the functions to my staging branch. Run them through a string replace or something like that before pushing.

@swyxio swyxio reopened this Apr 15, 2019
@EmilEriksen
Copy link

In order to access environment variables that are present at build time (such as those you might specify in netlify.toml or the special build env variables such as DEPLOY_URL) in a function you can do the following if you're building your functions with netlify-lambda.

  1. Create a custom webpack config file. Let's call it webpack.functions.js with the following content and simply add the env vars that you need. You could even export the entire build env with new webpack.EnvironmentPlugin(process.env) but you probably shouldn't unless you fully understand what that does.
// webpack.functions.js
const webpack = require('webpack')
module.exports = {
    plugins: [
        new webpack.EnvironmentPlugin(['VARIABLE1', 'VARIABLE2', ...])
    ]
}
  1. Change your build command to include the config file:
netlify-lambda build --config ./webpack.functions.js functions/
  1. Use process.env like you normally would in your functions.

What is happening? The issue is that the aforementioned env variables don't get exported to your function env - they are only present at build time. What we're basically doing is that we're replacing instances of process.env.VARIABLE in the function code at build time where the env variables we want are present. You can read about the environment plugin here.

@renschler
Copy link

@EmilEriksen thanks! Can you expand how you would use that approach to have different variable values for Staging vs Master branches?

@EmilEriksen
Copy link

@renschler You'd simply use contexts in netlify.toml like you normally would. You just need to add the names of the variables to new webpack.EnvironmentPlugin(['VARIABLE1', 'VARIABLE2', ...]). As I mentioned you could also simply change the line to new webpack.EnvironmentPlugin(process.env) and then it should work for all variables without you having to manually specify any names. There could potentially be conflicts with this method though if you're using some functions specific env variables that are also defined at build time but it's probably not an issue.

@stale
Copy link

stale bot commented Jun 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 24, 2019
@swyxio
Copy link
Contributor

swyxio commented Jun 25, 2019

never stale

@stale stale bot removed the stale label Jun 25, 2019
@designbuedchen
Copy link

hi there any news or workarounds about this issue? It would be awesome to use different env variables in functions based on dev/branch/master πŸ™πŸΌ

@swyxio
Copy link
Contributor

swyxio commented Jul 15, 2019

i believe this will be a better first class solution once the new buildbot @DavidWells is working on comes alive

@designbuedchen
Copy link

i believe this will be a better first class solution once the new buildbot @DavidWells is working on comes alive

Sounds promising. Anything to read more about the new buildbot?

@swyxio
Copy link
Contributor

swyxio commented Jul 17, 2019

nothing public yet and no timeline, its more r&d work so i dont want to overpromise.

Alonski pushed a commit to counterfactual/dapps-bots that referenced this issue Aug 6, 2019
… (#377)

* playground-server: passing webpack config to build

* playground-server: fixed local routing issues

* playground-server: fixed environment name

* playground-server: forcing Netlify to ack productive env

* playground-server: adding DEBUG=* to see where the router breaks

* playground-server: added some console.log()'s to debug

* playground-server: fixed env-var bug (see netlify/netlify-lambda#59)

* playground-server: replace NODE_ENV with PLAYGROUND_SERVER_ENV
@marcosscriven
Copy link

In order to access environment variables that are present at build time (such as those you might specify in netlify.toml or the special build env variables such as DEPLOY_URL) in a function you can do the following if you're building your functions with netlify-lambda.

  1. Create a custom webpack config file. Let's call it webpack.functions.js with the following content and simply add the env vars that you need. You could even export the entire build env with new webpack.EnvironmentPlugin(process.env) but you probably shouldn't unless you fully understand what that does.
// webpack.functions.js
const webpack = require('webpack')
module.exports = {
    plugins: [
        new webpack.EnvironmentPlugin(['VARIABLE1', 'VARIABLE2', ...])
    ]
}
  1. Change your build command to include the config file:
netlify-lambda build --config ./webpack.functions.js functions/
  1. Use process.env like you normally would in your functions.

What is happening? The issue is that the aforementioned env variables don't get exported to your function env - they are only present at build time. What we're basically doing is that we're replacing instances of process.env.VARIABLE in the function code at build time where the env variables we want are present. You can read about the environment plugin here.

This would be so handy to add to the official Netlify documentation. It’s far from clear otherwise how to safely use secret variables in lambdas.

@stale
Copy link

stale bot commented Oct 18, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 18, 2019
@stale stale bot closed this as completed Oct 25, 2019
@renschler
Copy link

renschler commented Feb 26, 2020

I don't think this should have been closed? @sw-yx

@swyxio
Copy link
Contributor

swyxio commented Mar 25, 2020

i have left the company and dont maintain this anymore sorry

@nolessafool
Copy link
Contributor

Sorry about that - reopened now

@nolessafool nolessafool reopened this Aug 7, 2020
@erezrokah erezrokah added type: bug code to address defects in shipped code and removed stale labels Aug 13, 2020
@gtwebsite
Copy link

Anyone got a solution for this? I thought nextjs api would solve my problem, but no, it also doesnt pull the env vars

@Elyx0
Copy link

Elyx0 commented Feb 4, 2022

@jbool24
Copy link

jbool24 commented Mar 15, 2022

Not sure how this is still open 3yrs later because its seems simple enough. Since Netlify is using build containers can't the tool just export the ENVs during container instantiation?

What is the point of having the CLI and netlify.toml allow for context overrides and the netlify build can specify a flag --context xyz if those contexts do not use those ENV Vars specified in the container env?

Is the work around to mess with build env within the chosen bundler?? I'm using in Vite and I need it to string replace URLs in bundled code based on environments (this is similar to webpack define plugin). Do I need to ignore Netlify configs and bake them into the bundler?

@guemmern
Copy link

I'm also having issues with environment variables per branch deploy. Maybe you should remove this feature from the docs if it's not working correctly.

@jpellizzari
Copy link

I built https://www.npmjs.com/package/netlify-plugin-inline-functions-env-typescript not sure it can help

This plugin worked perfectly for me. Thank you for building it an linking it @Elyx0 .

@melanierichards
Copy link

Hi folks, this is somewhat orthogonal to the bug in netlify.toml config, but we now support contextual environment variables (including branch-based values) in the Netlify UI, CLI, and API. If you'd prefer to set your env vars with one of those methods, you can move your site over to the new experience today! Hope that helps.

@danez danez closed this as completed Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.