Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the --target flag as a parameter to the docker builder. #894

Merged
merged 1 commit into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/annotated-skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ build:
cacheFrom:
- image1
- image2
# Dockerfile target name to build.
# target: stageName

# bazel requires bazel CLI to be installed and the artifacts sources to
# contain Bazel configuration files.
Expand Down
3 changes: 0 additions & 3 deletions pkg/skaffold/build/local/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri

args := []string{"build", workspace, "--file", dockerfilePath, "-t", initialTag}
args = append(args, docker.GetBuildArgs(a)...)
for _, from := range a.CacheFrom {
args = append(args, "--cache-from", from)
}

cmd := exec.Command("docker", args...)
if b.cfg.UseBuildkit {
Expand Down
9 changes: 9 additions & 0 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func BuildArtifact(ctx context.Context, out io.Writer, cli APIClient, workspace
BuildArgs: a.BuildArgs,
CacheFrom: a.CacheFrom,
AuthConfigs: authConfigs,
Target: a.Target,
})
if err != nil {
return errors.Wrap(err, "docker build")
Expand Down Expand Up @@ -194,5 +195,13 @@ func GetBuildArgs(a *v1alpha2.DockerArtifact) []string {
}
}

for _, from := range a.CacheFrom {
args = append(args, "--cache-from", from)
}

if a.Target != "" {
args = append(args, "--target", a.Target)
}

return args
}
57 changes: 46 additions & 11 deletions pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,53 @@ func TestDigest(t *testing.T) {
}

func TestGetBuildArgs(t *testing.T) {
artifact := &v1alpha2.DockerArtifact{
BuildArgs: map[string]*string{
"key1": util.StringPtr("value1"),
"key2": nil,
tests := []struct {
description string
artifact *v1alpha2.DockerArtifact
want []string
}{
{
description: "build args",
artifact: &v1alpha2.DockerArtifact{
BuildArgs: map[string]*string{
"key1": util.StringPtr("value1"),
"key2": nil,
},
},
want: []string{"--build-arg", "key1=value1", "--build-arg", "key2"},
},
{
description: "cache from",
artifact: &v1alpha2.DockerArtifact{
CacheFrom: []string{"gcr.io/foo/bar", "baz:latest"},
},
want: []string{"--cache-from", "gcr.io/foo/bar", "--cache-from", "baz:latest"},
},
{
description: "target",
artifact: &v1alpha2.DockerArtifact{
Target: "stage1",
},
want: []string{"--target", "stage1"},
},
{
description: "all",
artifact: &v1alpha2.DockerArtifact{
BuildArgs: map[string]*string{
"key1": util.StringPtr("value1"),
},
CacheFrom: []string{"foo"},
Target: "stage1",
},
want: []string{"--build-arg", "key1=value1", "--cache-from", "foo", "--target", "stage1"},
},
}

arg := GetBuildArgs(artifact)
expected := []string{"--build-arg", "key1=value1", "--build-arg", "key2"}

if diff := cmp.Diff(arg, expected); diff != "" {
t.Errorf("%T differ (-got, +want): %s", expected, diff)
return
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
result := GetBuildArgs(tt.artifact)
if diff := cmp.Diff(result, tt.want); diff != "" {
t.Errorf("%T differ (-got, +want): %s", tt.want, diff)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/skaffold/schema/v1alpha2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ type DockerArtifact struct {
DockerfilePath string `yaml:"dockerfilePath,omitempty"`
BuildArgs map[string]*string `yaml:"buildArgs,omitempty"`
CacheFrom []string `yaml:"cacheFrom,omitempty"`
Target string `yaml:"target,omitempty"`
}

type BazelArtifact struct {
Expand Down