-
Notifications
You must be signed in to change notification settings - Fork 114
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
Comments
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. |
@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? |
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 |
Hi, I see that this issue was closed, but was it resolved? |
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? |
Afaict this is working as intended, the context when using netlify-lambda is not I might be forgetting but does netlify-lambda provide a cli argument to set the context and or is any context use by default? |
yup, the branch fixes it for me π |
my use case is for |
β¦#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
@8eecf0d2 the |
@shmargum right, I was talking about netlify-lambda which afaict doesn't set the context. |
the |
we have merged #94 which should address this - please reopen/comment if any further issues |
I'm using [context.production.environment]
CONTACT_FORM_EMAIL = "[email protected]"
[context.deploy-preview.environment]
CONTACT_FORM_EMAIL = "[email protected]" When using It is worth mentioning that we also have the build environment variables set in the UI, should I remove those? |
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 |
@sw-yx I'm working on creating a cleaned up public repo to show the issue, stay tuned.
That's how it is done in the TOML docs in netlify: https://www.netlify.com/docs/netlify-toml-reference/#getting-started |
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 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 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) |
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 |
ok thank you for the detailed repro so i can understand your issue. so first of all, 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... |
@pgarciacamou we have a possible bug that might be worth trying a small fix for - can you try clicking the |
You are totally correct, this issue has nothing to do with
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. |
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. |
@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. |
@renschler I did get help but did not work. The response to the email I sent was:
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
I might quickly try |
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. |
In order to access environment variables that are present at build time (such as those you might specify in
// webpack.functions.js
const webpack = require('webpack')
module.exports = {
plugins: [
new webpack.EnvironmentPlugin(['VARIABLE1', 'VARIABLE2', ...])
]
}
netlify-lambda build --config ./webpack.functions.js 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 |
@EmilEriksen thanks! Can you expand how you would use that approach to have different variable values for Staging vs Master branches? |
@renschler You'd simply use contexts in |
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. |
never stale |
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 ππΌ |
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? |
nothing public yet and no timeline, its more r&d work so i dont want to overpromise. |
β¦ (#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
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. |
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. |
I don't think this should have been closed? @sw-yx |
i have left the company and dont maintain this anymore sorry |
Sorry about that - reopened now |
Anyone got a solution for this? I thought nextjs api would solve my problem, but no, it also doesnt pull the env vars |
I built https://www.npmjs.com/package/netlify-plugin-inline-functions-env-typescript not sure it can help |
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 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? |
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. |
This plugin worked perfectly for me. Thank you for building it an linking it @Elyx0 . |
Hi folks, this is somewhat orthogonal to the bug in |
If you define environment variables in the netlify.toml only the
build.environment
variables are being used.Eg:
The text was updated successfully, but these errors were encountered: