diff --git a/pkg/skaffold/build/docker/docker.go b/pkg/skaffold/build/docker/docker.go index e0d46c27621..53166e303a3 100644 --- a/pkg/skaffold/build/docker/docker.go +++ b/pkg/skaffold/build/docker/docker.go @@ -106,13 +106,7 @@ func (b *Builder) dockerCLIBuild(ctx context.Context, out io.Writer, name string if err != nil { return "", fmt.Errorf("unable to evaluate build args: %w", err) } - for i, cliFlag := range a.CliFlags { - a.CliFlags[i], err = util.ExpandEnvTemplate(cliFlag, imageInfoEnv) - if err != nil { - return "", fmt.Errorf("unable to evaluate cli flags: %w", err) - } - } - cliArgs, err := docker.ToCLIBuildArgs(a, ba) + cliArgs, err := docker.ToCLIBuildArgs(a, ba, imageInfoEnv) if err != nil { return "", fmt.Errorf("getting docker build args: %w", err) } diff --git a/pkg/skaffold/build/gcb/docker.go b/pkg/skaffold/build/gcb/docker.go index 3f41563299e..b23431cac4b 100644 --- a/pkg/skaffold/build/gcb/docker.go +++ b/pkg/skaffold/build/gcb/docker.go @@ -108,7 +108,7 @@ func (b *Builder) dockerBuildArgs(a *latest.Artifact, tag string, deps []*latest return nil, fmt.Errorf("unable to evaluate build args: %w", err) } - ba, err := docker.ToCLIBuildArgs(d, buildArgs) + ba, err := docker.ToCLIBuildArgs(d, buildArgs, nil) if err != nil { return nil, fmt.Errorf("getting docker build args: %w", err) } diff --git a/pkg/skaffold/docker/image.go b/pkg/skaffold/docker/image.go index 1d68f41996d..02455ea3495 100644 --- a/pkg/skaffold/docker/image.go +++ b/pkg/skaffold/docker/image.go @@ -626,7 +626,7 @@ func (l *localDaemon) DiskUsage(ctx context.Context) (uint64, error) { return uint64(usage.LayersSize), nil } -func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string) ([]string, error) { +func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string, env map[string]string) ([]string, error) { var args []string var keys []string for k := range evaluatedArgs { @@ -653,7 +653,13 @@ func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string) args = append(args, "--cache-from", from) } - args = append(args, a.CliFlags...) + for _, cliFlag := range a.CliFlags { + cliFlag, err := util.ExpandEnvTemplate(cliFlag, env) + if err != nil { + return nil, fmt.Errorf("unable to evaluate cli flags: %w", err) + } + args = append(args, cliFlag) + } if a.Target != "" { args = append(args, "--target", a.Target) diff --git a/pkg/skaffold/docker/image_test.go b/pkg/skaffold/docker/image_test.go index 5c84e9d1131..322e9fb419d 100644 --- a/pkg/skaffold/docker/image_test.go +++ b/pkg/skaffold/docker/image_test.go @@ -318,6 +318,14 @@ func TestGetBuildArgs(t *testing.T) { }, want: []string{"--foo", "--bar"}, }, + { + description: "expand env for CLI flags", + artifact: &latest.DockerArtifact{ + CliFlags: []string{"--cache-to=type=registry,ref={{ .IMAGE_REPO }}/cache-image:cache"}, + }, + env: []string{"IMAGE_REPO=docker.io/library"}, + want: []string{"--cache-to=type=registry,ref=docker.io/library/cache-image:cache"}, + }, { description: "target", artifact: &latest.DockerArtifact{ @@ -430,7 +438,7 @@ func TestGetBuildArgs(t *testing.T) { return } - result, err := ToCLIBuildArgs(test.artifact, args) + result, err := ToCLIBuildArgs(test.artifact, args, nil) t.CheckError(test.shouldErr, err) if !test.shouldErr {