Skip to content

Commit

Permalink
chore: expand environment for cliFlags in ToCLIBuildArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry committed Dec 14, 2024
1 parent 745cc01 commit ec0b09c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
8 changes: 1 addition & 7 deletions pkg/skaffold/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/gcb/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ec0b09c

Please sign in to comment.