Skip to content

Commit

Permalink
Add the --target flag as a parameter to the docker builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlorenc committed Aug 22, 2018
1 parent 3f11067 commit 68ff998
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
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
}
60 changes: 49 additions & 11 deletions pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,56 @@ func TestDigest(t *testing.T) {
}

func TestGetBuildArgs(t *testing.T) {
artifact := &v1alpha2.DockerArtifact{
BuildArgs: map[string]*string{
"key1": util.StringPtr("value1"),
"key2": nil,
type args struct {
a *v1alpha2.DockerArtifact
}
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

0 comments on commit 68ff998

Please sign in to comment.