From 396fd4cefb4f219c07644b2427225bdfcb39da1e Mon Sep 17 00:00:00 2001 From: Feroze Mohideen Date: Thu, 29 Feb 2024 11:24:01 -0500 Subject: [PATCH] add env from container to build (#4345) --- cli/cmd/docker/builder.go | 5 +++-- cli/cmd/v2/apply.go | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cli/cmd/docker/builder.go b/cli/cmd/docker/builder.go index 08ce49606e..a60040dbd2 100644 --- a/cli/cmd/docker/builder.go +++ b/cli/cmd/docker/builder.go @@ -62,7 +62,6 @@ func (a *Agent) BuildLocal(ctx context.Context, opts *BuildOpts) (err error) { if len(dockerIgnoreBytes) != 0 { excludes, err = dockerignore.ReadAll(bytes.NewBuffer(dockerIgnoreBytes)) - if err != nil { return fmt.Errorf("error reading .dockerignore: %w", err) } @@ -92,7 +91,6 @@ func (a *Agent) BuildLocal(ctx context.Context, opts *BuildOpts) (err error) { // add the dockerfile to the build context tar, dockerfilePath, err = AddDockerfileToBuildContext(dockerfileCtx, tar) - if err != nil { return fmt.Errorf("error adding Dockerfile to build context: %w", err) } @@ -222,6 +220,9 @@ func buildLocalWithBuildkit(ctx context.Context, opts BuildOpts) error { "--cache-from", fmt.Sprintf("type=registry,ref=%s:%s", opts.ImageRepo, opts.CurrentTag), } for key, val := range opts.Env { + if key == "PORTER_BUILDKIT_ARGS" { + continue + } commandArgs = append(commandArgs, "--build-arg", fmt.Sprintf("%s=%s", key, val)) } diff --git a/cli/cmd/v2/apply.go b/cli/cmd/v2/apply.go index 7f6b10ca7a..69e3d92529 100644 --- a/cli/cmd/v2/apply.go +++ b/cli/cmd/v2/apply.go @@ -9,6 +9,7 @@ import ( "os/signal" "path/filepath" "strconv" + "strings" "syscall" "time" @@ -180,13 +181,27 @@ func Apply(ctx context.Context, inp ApplyInput) error { color.New(color.FgGreen).Printf("Building new image with tag %s...\n", commitSHA) // nolint:errcheck,gosec + buildEnvVariables := make(map[string]string) + for k, v := range buildSettings.BuildEnvVariables { + buildEnvVariables[k] = v + } + + // use all env variables from running container in build + env := os.Environ() + for _, v := range env { + pair := strings.SplitN(v, "=", 2) + if len(pair) == 2 { + buildEnvVariables[pair[0]] = pair[1] + } + } + buildInput, err := buildInputFromBuildSettings(buildInputFromBuildSettingsInput{ projectID: cliConf.Project, appName: appName, commitSHA: commitSHA, image: buildSettings.Image, build: buildSettings.Build, - buildEnv: buildSettings.BuildEnvVariables, + buildEnv: buildEnvVariables, pullImageBeforeBuild: inp.PullImageBeforeBuild, }) if err != nil {