netlify-plugin-contextual-env icon indicating copy to clipboard operation
netlify-plugin-contextual-env copied to clipboard

Question: envs used in functions runtime vs build

Open dthyresson opened this issue 5 years ago • 5 comments

I have a question about

https://github.com/cball/netlify-plugin-contextual-env/blob/master/src/index.js#L18

I was using your plugin as a guide to do something similar for a env that isn't used as part of the build runtime but rather when a function executes.

process.env[inputs.netlify_env] = "some value"

While I know that the env (it is DATABASE_URL) is set during build seen in the preBuild plugin:

2:22:10 PM: DATABASE_URL ends in tdjhw

I'm not seeing it that a function can find it when executes:

error: Environment variable not found: DATABASE_URL.

Is that expected, or might it be how that function fetches the env (note: it's Prisma when creating the client).

dthyresson avatar Jul 12 '20 18:07 dthyresson

I'm hitting this exact same problem. The function is receiving the the base API_URL variable, when it should be the DEPLOY_PREVIEW_API_URL. Is there any thoughts on how to use this plugin with functions?

ericchernuka avatar Aug 01 '20 00:08 ericchernuka

Thanks for the report @ericchernuka and @dthyresson. I'll have to double check this and report back.

It sounds like process.env isn't getting set in the containers that run functions.

Originally I was writing an .env file directly, but took it out once Netlify supported writing to process.env during build time. Worst case, it's possible that we could take that route, but it would require sourcing it via something like dotenv

cball avatar Aug 01 '20 02:08 cball

@cball I looked into this a bit further and the Netlify Functions do not pass in any context or branch into the functions that can be used to even replicate your code for determining any of that information. Seems like they only expose those values at build time which is unfortunate. Looks like your writing of a .env file at build time might be the only way to accomplish this for functions.

I wonder if having an option for a postBuild .env creation would be worthwhile as a way to have both. It's really too bad since what you have right now is great and if it worked the way we thought for functions, it would allow for on the fly swapping of ENV variables like a real server. Not sure why Netlify doesn't expose some of these parameters to the functions so that users have context around who the caller might be.

ericchernuka avatar Aug 05 '20 23:08 ericchernuka

@ehmicky is there an approach you'd suggest here? I expect like @ericchernuka said we might be able to make it work by writing out the .env at build time, but that would require each function to explicitly require dotenv.

cball avatar Aug 07 '20 14:08 cball

Only the environment variables set in the Netlify App UI can currently be used in Netlify Functions.

However, there is a workaround: the netlify-plugin-inline-functions-env plugin is injecting/templating any environment variable into Functions source files at build time. I have an ongoing PR to hopefully fix some of the shortcomings of that plugin.

ehmicky avatar Aug 07 '20 15:08 ehmicky