-
Notifications
You must be signed in to change notification settings - Fork 49
Add EXPORT_ENV_FILE parameter #55
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
Conversation
@@ -724,10 +744,16 @@ func Run(ctx context.Context, options Options) error { | |||
for _, env := range configFile.Config.Env { | |||
pair := strings.SplitN(env, "=", 2) | |||
os.Setenv(pair[0], pair[1]) | |||
exportEnv(pair[0], pair[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Environment variables declared with ENV
in the Dockerfile don't seem to actually show up in configFile.Config.Env
. This is very strange and I'm not sure why this isn't working as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, it works in the test but isn't working in my actual setup 🤔. Trying to dig into this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, found the problem. Env vars from the Dockerfile get skipped when SKIP_REBUILD=true
and we are starting an existing container. I will update this to skip writing the env export file in this scenario - we only want to write it when we do the actual build.
This allows environment variables from the Dockerfile and devcontainer.json to be exported to a file that can be sourced later.
55b300e
to
18d1bc4
Compare
@@ -668,6 +675,20 @@ func Run(ctx context.Context, options Options) error { | |||
} | |||
_ = file.Close() | |||
|
|||
var exportEnvFile *os.File | |||
if options.ExportEnvFile != "" && !skippedRebuild { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A mention as to why we don't want to do this on rebuild would be helpful. Essentially your GitHub comments!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment explaining this.
// ExportEnvFile is an optional file path where envbuilder | ||
// will dump environment variables from devcontainer.json | ||
// and the built container image. | ||
ExportEnvFile string `env:"EXPORT_ENV_FILE"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment describing the use case would also be helpful, mainly because of the export
directive, which is technically sh
syntax, not .env
file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should make this a proper .env
file being exported instead. Would that make it less useful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to create a .env
file instead (key/value pairs separated by =
, no quoting). The sh syntax is more useful for me, but I can use something like this to process the .env
file: https://stackoverflow.com/a/66118031
@@ -353,6 +353,39 @@ func TestExitBuildOnFailure(t *testing.T) { | |||
require.ErrorContains(t, err, "parsing dockerfile") | |||
} | |||
|
|||
func TestExportEnvFile(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great test! 😍
6f7f81e
to
dba0dc2
Compare
Comments addressed; this should be ready to go :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This allows environment variables from the Dockerfile and devcontainer.json to be exported to a file that can be sourced later.