From 18d1bc42ba50502c647b1ef39bb317768c578fca Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Tue, 3 Oct 2023 15:43:43 -0700 Subject: [PATCH 1/3] Add EXPORT_ENV_FILE parameter This allows environment variables from the Dockerfile and devcontainer.json to be exported to a file that can be sourced later. --- envbuilder.go | 28 ++++++++++++++++++++++++++++ integration/integration_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/envbuilder.go b/envbuilder.go index 279dae5a..13ea53d5 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -194,6 +194,11 @@ type Options struct { // This is useful for self-signed certificates. SSLCertBase64 string `env:"SSL_CERT_BASE64"` + // 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"` + // Logger is the logger to use for all operations. Logger func(level codersdk.LogLevel, format string, args ...interface{}) @@ -524,6 +529,7 @@ func Run(ctx context.Context, options Options) error { }) } + skippedRebuild := false build := func() (v1.Image, error) { _, err := options.Filesystem.Stat(MagicFile) if err == nil && options.SkipRebuild { @@ -537,6 +543,7 @@ func Run(ctx context.Context, options Options) error { return nil, fmt.Errorf("image from remote: %w", err) } endStage("🏗️ Found image from remote!") + skippedRebuild = true return image, nil } @@ -668,6 +675,20 @@ func Run(ctx context.Context, options Options) error { } _ = file.Close() + var exportEnvFile *os.File + if options.ExportEnvFile != "" && !skippedRebuild { + exportEnvFile, err = os.Create(options.ExportEnvFile) + if err != nil { + return fmt.Errorf("failed to open EXPORT_ENV_FILE %q: %w", options.ExportEnvFile, err) + } + } + exportEnv := func(key, value string) { + if exportEnvFile == nil { + return + } + fmt.Fprintf(exportEnvFile, "export %s=%q\n", key, value) + } + configFile, err := image.ConfigFile() if err != nil { return fmt.Errorf("get image config: %w", err) @@ -695,6 +716,7 @@ func Run(ctx context.Context, options Options) error { if container.RemoteEnv != nil { for key, value := range container.RemoteEnv { os.Setenv(key, value) + exportEnv(key, value) } } } @@ -724,10 +746,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]) } for _, env := range buildParams.Env { pair := strings.SplitN(env, "=", 2) os.Setenv(pair[0], pair[1]) + exportEnv(pair[0], pair[1]) + } + + if exportEnvFile != nil { + exportEnvFile.Close() } username := configFile.Config.User diff --git a/integration/integration_test.go b/integration/integration_test.go index f5f6c5f0..ccce7dca 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -353,6 +353,39 @@ func TestExitBuildOnFailure(t *testing.T) { require.ErrorContains(t, err, "parsing dockerfile") } +func TestExportEnvFile(t *testing.T) { + t.Parallel() + + // Ensures that a Git repository with a devcontainer.json is cloned and built. + url := createGitServer(t, gitServerOptions{ + files: map[string]string{ + ".devcontainer/devcontainer.json": `{ + "name": "Test", + "build": { + "dockerfile": "Dockerfile" + }, + "build": { + "dockerfile": "Dockerfile" + }, + "remoteEnv": { + "FROM_DEVCONTAINER_JSON": "bar" + } + }`, + ".devcontainer/Dockerfile": "FROM alpine:latest\nENV FROM_DOCKERFILE=foo", + }, + }) + ctr, err := runEnvbuilder(t, options{env: []string{ + "GIT_URL=" + url, + "EXPORT_ENV_FILE=/env", + }}) + require.NoError(t, err) + + output := execContainer(t, ctr, "cat /env") + require.Contains(t, strings.TrimSpace(output), + `export FROM_DOCKERFILE="foo" +export FROM_DEVCONTAINER_JSON="bar"`) +} + func TestPrivateRegistry(t *testing.T) { t.Parallel() t.Run("NoAuth", func(t *testing.T) { From f49ccd6e4adbf07f6c588e1d9642a3f018696f38 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Wed, 4 Oct 2023 08:58:58 -0700 Subject: [PATCH 2/3] Export to .env file --- envbuilder.go | 8 ++++---- integration/integration_test.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/envbuilder.go b/envbuilder.go index 13ea53d5..4c8fc803 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -194,9 +194,9 @@ type Options struct { // This is useful for self-signed certificates. SSLCertBase64 string `env:"SSL_CERT_BASE64"` - // ExportEnvFile is an optional file path where envbuilder - // will dump environment variables from devcontainer.json - // and the built container image. + // ExportEnvFile is an optional file path to a .env file where + // envbuilder will dump environment variables from devcontainer.json and + // the built container image. ExportEnvFile string `env:"EXPORT_ENV_FILE"` // Logger is the logger to use for all operations. @@ -686,7 +686,7 @@ func Run(ctx context.Context, options Options) error { if exportEnvFile == nil { return } - fmt.Fprintf(exportEnvFile, "export %s=%q\n", key, value) + fmt.Fprintf(exportEnvFile, "%s=%s\n", key, value) } configFile, err := image.ConfigFile() diff --git a/integration/integration_test.go b/integration/integration_test.go index ccce7dca..5b007359 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -382,8 +382,8 @@ func TestExportEnvFile(t *testing.T) { output := execContainer(t, ctr, "cat /env") require.Contains(t, strings.TrimSpace(output), - `export FROM_DOCKERFILE="foo" -export FROM_DEVCONTAINER_JSON="bar"`) + `FROM_DOCKERFILE=foo +FROM_DEVCONTAINER_JSON=bar`) } func TestPrivateRegistry(t *testing.T) { From dba0dc2dee78e0b5d59b0b6339c819a3cb16275b Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Wed, 4 Oct 2023 08:59:27 -0700 Subject: [PATCH 3/3] Add comment explaining SKIP_REBUILD behavior for env var export --- envbuilder.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/envbuilder.go b/envbuilder.go index 4c8fc803..c041ffb4 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -676,6 +676,11 @@ func Run(ctx context.Context, options Options) error { _ = file.Close() var exportEnvFile *os.File + // Do not export env if we skipped a rebuild, because ENV directives + // from the Dockerfile would not have been processed and we'd miss these + // in the export. We should have generated a complete set of environment + // on the intial build, so exporting environment variables a second time + // isn't useful anyway. if options.ExportEnvFile != "" && !skippedRebuild { exportEnvFile, err = os.Create(options.ExportEnvFile) if err != nil {