Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

aaronlehmann
Copy link
Collaborator

This allows environment variables from the Dockerfile and devcontainer.json to be exported to a file that can be sourced later.

@aaronlehmann aaronlehmann requested a review from kylecarbs October 3, 2023 22:44
@@ -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])
Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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...

Copy link
Collaborator Author

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.
@aaronlehmann aaronlehmann changed the title [WIP] Add EXPORT_ENV_FILE parameter Add EXPORT_ENV_FILE parameter Oct 4, 2023
@@ -668,6 +675,20 @@ func Run(ctx context.Context, options Options) error {
}
_ = file.Close()

var exportEnvFile *os.File
if options.ExportEnvFile != "" && !skippedRebuild {
Copy link
Member

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!

Copy link
Collaborator Author

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"`
Copy link
Member

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.

Copy link
Member

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?

Copy link
Collaborator Author

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test! 😍

@aaronlehmann
Copy link
Collaborator Author

Comments addressed; this should be ready to go :)

Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@aaronlehmann aaronlehmann merged commit 628029f into main Oct 5, 2023
@aaronlehmann aaronlehmann deleted the export-env branch October 5, 2023 15:30
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.

2 participants