Skip to content
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

Have netlify build use .env files like netlify dev to override local ENV variables #1514

Closed
moop-moop opened this issue Jun 11, 2020 · 12 comments
Labels

Comments

@moop-moop
Copy link

- Do you want to request a feature or report a bug?

feature

- What is the current behavior?

  • netlify build only honors ENV variables in the Netlify linked site, netlify.toml file as far as project level based environment variables.
  • netlify dev can use a .env file to override the linked Netlify site environment variables.

- If the current behavior is a bug, please provide the steps to reproduce.

N/A

- What is the expected behavior?

The feature request is that netlify build behave in the same way as netlify dev where a .env file can be used to override ENV variables.

- Reason

Consistency with netlify dev, and convenience for working with Netlify Plugin development and Netlify builds (like performance tuning and errors).
Working more on the DevOps side, rather that actual site programming/development.

I use netlify build almost daily.

@ehmicky ehmicky transferred this issue from netlify/cli Jun 11, 2020
@erezrokah
Copy link
Contributor

Hi @moop-moop, very interesting - thank you for opening the issue.
is this intended for testing purposes?
Meaning, once you get netlify build to work you'll update the variables from .env in the site settings?

If not, can you describe the use case?

@moop-moop
Copy link
Author

Meaning, once you get netlify build to work you'll update the variables from .env in the site settings?

Yes, for some cases.

If not, can you describe the use case?

Really, it's pretty much the same reasons as when developers are using them for netlify dev except for testing/developing/troubleshooting builds and plugins. Temporary environment variables.

Possible real world example with Contentful:

We might need to create a Contentful environment branch to test a big content model change. The Contentful environment branch could have a different API key while testing/developing.
If something is broken in the build, I am also testing this build locally. I would want to use those temporary API keys. We could use alternate mechanisms, but we would need put the keys in the Netlify Site and use a Plugin for env with context.

@ehmicky
Copy link
Contributor

ehmicky commented Jun 15, 2020

Instead of .env:

EXAMPLE_ONE=one
EXAMPLE_TWO=two

You can do this in netlify.toml:

[build.environment]
EXAMPLE_ONE = "one"
EXAMPLE_TWO = "two"

Would this solve your problem? I.e. using environment variables while testing/debugging a Build plugin.

@dimavolo
Copy link

@ehmicky good suggestion! I have [dev.environment] and [build.environment] sections in netlify.toml, but they seem to be ignored.

Am I missing a critical step?

Thanks!

@ehmicky
Copy link
Contributor

ehmicky commented Jun 16, 2020

The one used by netlify build would be [build.environment], not [dev.environment].

Those should be available in your build command and plugins. Is there a repository I could check to reproduce?

@dimavolo
Copy link

Ah yes, I'm trying to use both, but neither section is picked up when I try to access process.env.foo from a function while running under netlify dev

@ehmicky
Copy link
Contributor

ehmicky commented Jun 16, 2020

Just to be sure: by "function", do you mean a function inside a Build plugin, a function inside a build command, or a Netlify Function (function-as-a-service)?

@moop-moop
Copy link
Author

Instead of .env:

EXAMPLE_ONE=one
EXAMPLE_TWO=two

You can do this in netlify.toml:

[build.environment]
EXAMPLE_ONE = "one"
EXAMPLE_TWO = "two"

Would this solve your problem? I.e. using environment variables while testing/debugging a Build plugin.

Not exactly. My goal would be to have environment variables that are not committed to a repo. Local only, in-other-words.
Your suggestion works if you don't care about exposing those particular variables forever in a repo.

Another option is to use the Contextual ENV Netlify Build Plugin, but that then requires "local" ENV variables to be stored in Netlify.

@ehmicky
Copy link
Contributor

ehmicky commented Jun 26, 2020

Yes, you're right, if the environment variables are secret, those should not be committed to git.

However, since those environment variables would be only temporary for debugging/testing in your case, wouldn't it work to modify the netlify.toml during testing without committing it to git?

Note: If the build is tested in production (instead of locally), setting environment variables in the Netlify app would need to be used instead.

Another note: netlify build also fetches and uses the environment variables you set in the Netlify app, providing you have run netlify init and netlify login. So setting them there is also an alternative way to get them in local builds.

@ehmicky
Copy link
Contributor

ehmicky commented Jul 1, 2020

The following plugin was proposed to add .env support: netlify/plugins#105

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had activity in 1 year. It will be closed in 14 days if no further activity occurs. Thanks!

@github-actions github-actions bot added the stale label Oct 11, 2022
@github-actions
Copy link
Contributor

This issue was closed because it had no activity for over 1 year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants