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

Load environment variables from file #405

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ZaninAndrea
Copy link

This pull request adds a new key env_file to the configuration, the key specifies a file from which the environment variables for running bin should be sourced.
If env_file is set, the environment file is sourced every time the bin is run, so that it works as expected when "env" is in include_ext and the env file is updated.

The new feature was suggested in #47 and removes the need for less elegant solutions like the one suggested in #58.

@silverwind
Copy link
Contributor

silverwind commented Apr 21, 2023

Does it also work when cmd and full_bin run? Please add some docs on where it exactly applies. I guess some people might also prefer a separate vars file between cmd vars and bin / full_bin, so that will need two options.

I would suggest setting the default to "", so this is not enabled by default and does not break existing setups.

Another problem I see that this does not support multipe files, so it would not be useable as-is to me, as I use setups with at least two files, like this.

@mfechner
Copy link

I think using cmd or full_bin will not work if you use air on e.g. Linux and Windows for the same project, as both environment are defining env variables differently. I think using a env_file configuration parameter is the only way to make this feature for all operating systems available.

@ZaninAndrea
Copy link
Author

The implementation doesn't break existing setups, if the env_file parameter is not specified it defaults to "" (due to how the parameters are parsed) and thus no environment file is loaded. I added the env_file = ".env" parameter in the example configuration as a form of documentation, but that file is meant to be modified by the user when doing the first configuration.

The environment file is loaded when running bin/full_bin but not when running cmd. I think that this feature is much more important for bin/full_bin rather than cmd, because the environment variables used in the build command can generally be safely committed in git, while the variables used at runtime may contain secrets; either way we could extend this implementation with another cmd_env_file (or something similar) quite easily.

Multiple environment files are indeed not supported, I could extend this to accept a list of files instead of a single file.

@whoiscarlo
Copy link

Any updates on this?

@whoiscarlo
Copy link

whoiscarlo commented Jun 3, 2023

I think using cmd or full_bin will not work if you use air on e.g. Linux and Windows for the same project, as both environment are defining env variables differently. I think using a env_file configuration parameter is the only way to make this feature for all operating systems available.

I found that it also doesn't work on Mac Os, not sure what's missing but my environment variables aren't being brought over

Solution is to use bin instead of full_bin. i.e bin=";APP_ENV= local APP_USER=me ./tmp/main"

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

Successfully merging this pull request may close these issues.

None yet

4 participants