Skip to content

Commit

Permalink
Pass extra env to the Docker CLI
Browse files Browse the repository at this point in the history
Fix #1749

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Aug 28, 2019
1 parent bbe58e9 commit df8cf2c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/skaffold/build/local/docker.go
Expand Up @@ -73,8 +73,9 @@ func (b *Builder) dockerCLIBuild(ctx context.Context, out io.Writer, workspace s
}

cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Env = append(util.OSEnviron(), b.retrieveExtraEnv()...)
if b.cfg.UseBuildkit {
cmd.Env = append(util.OSEnviron(), "DOCKER_BUILDKIT=1")
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=1")
}
cmd.Stdout = out
cmd.Stderr = out
Expand Down
89 changes: 89 additions & 0 deletions pkg/skaffold/build/local/docker_test.go
@@ -0,0 +1,89 @@
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package local

import (
"context"
"io/ioutil"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestDockerCLIBuild(t *testing.T) {
tests := []struct {
description string
localBuild latest.LocalBuild
extraEnv []string
expectedEnv []string
}{
{
description: "docker build",
localBuild: latest.LocalBuild{},
expectedEnv: []string{"KEY=VALUE"},
},
{
description: "extra env",
localBuild: latest.LocalBuild{},
extraEnv: []string{"OTHER=VALUE"},
expectedEnv: []string{"KEY=VALUE", "OTHER=VALUE"},
},
{
description: "buildkit",
localBuild: latest.LocalBuild{UseBuildkit: true},
expectedEnv: []string{"KEY=VALUE", "DOCKER_BUILDKIT=1"},
},
{
description: "buildkit and extra env",
localBuild: latest.LocalBuild{UseBuildkit: true},
extraEnv: []string{"OTHER=VALUE"},
expectedEnv: []string{"KEY=VALUE", "OTHER=VALUE", "DOCKER_BUILDKIT=1"},
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.NewTempDir().Chdir()
dockerfilePath, _ := filepath.Abs("Dockerfile")
t.Override(&util.DefaultExecCommand, t.NewFakeCmd().WithRunEnv("docker build . --file "+dockerfilePath+" -t tag --force-rm", test.expectedEnv))
t.Override(&util.OSEnviron, func() []string { return []string{"KEY=VALUE"} })
t.Override(&docker.NewAPIClient, func(*runcontext.RunContext) (docker.LocalDaemon, error) {
return docker.NewLocalDaemon(&testutil.FakeAPIClient{}, test.extraEnv, false, nil), nil
})

builder, err := NewBuilder(stubRunContext(test.localBuild))
t.CheckNoError(err)

artifact := &latest.Artifact{
Workspace: ".",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: "Dockerfile",
},
},
}

_, err = builder.buildDocker(context.Background(), ioutil.Discard, artifact, "tag")
t.CheckNoError(err)
})
}
}

0 comments on commit df8cf2c

Please sign in to comment.