Skip to content

Commit

Permalink
add env from container to build (#4345)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feroze Mohideen authored Feb 29, 2024
1 parent a3c298b commit 396fd4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions cli/cmd/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}

Expand Down
17 changes: 16 additions & 1 deletion cli/cmd/v2/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 396fd4c

Please sign in to comment.