Skip to content

Commit

Permalink
bake: fix output handling for push and load
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Mar 10, 2024
1 parent 4327ee7 commit 1a39131
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
52 changes: 41 additions & 11 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,14 +898,36 @@ func (t *Target) AddOverrides(overrides map[string]Override) error {
if err != nil {
return errors.Errorf("invalid value %s for boolean key push", value)
}
if len(t.Outputs) == 0 {
t.Outputs = append(t.Outputs, "type=image,push=true")
} else {
for i, output := range t.Outputs {
if typ := parseOutputType(output); typ == "image" || typ == "registry" {
t.Outputs[i] = t.Outputs[i] + ",push=" + value
var pushUsed bool
for i, output := range t.Outputs {
if typ := parseOutputType(output); typ == "image" || typ == "registry" {
t.Outputs[i] = t.Outputs[i] + ",push=" + value
pushUsed = true
}
}
if !pushUsed {
t.Outputs = append(t.Outputs, "type=image,push="+value)
}
case "load":
load, err := strconv.ParseBool(value)
if err != nil {
return errors.Errorf("invalid value %s for boolean key load", value)
}
if load {
var loadUsed bool
for _, output := range t.Outputs {
if typ := parseOutputType(output); typ == "docker" {
if v := parseOutput(output); v != nil {
if _, ok := v["dest"]; !ok {
loadUsed = true
break
}
}
}
}
if !loadUsed {
t.Outputs = append(t.Outputs, "type=docker")
}
}
default:
return errors.Errorf("unknown key: %s", keys[0])
Expand Down Expand Up @@ -1394,18 +1416,26 @@ func removeAttestDupes(s []string) []string {
return res
}

func parseOutputType(str string) string {
func parseOutput(str string) map[string]string {
csvReader := csv.NewReader(strings.NewReader(str))
fields, err := csvReader.Read()
if err != nil {
return ""
return nil
}
res := map[string]string{}
for _, field := range fields {
parts := strings.SplitN(field, "=", 2)
if len(parts) == 2 {
if parts[0] == "type" {
return parts[1]
}
res[parts[0]] = parts[1]
}
}
return res
}

func parseOutputType(str string) string {
if out := parseOutput(str); out != nil {
if v, ok := out["type"]; ok {
return v
}
}
return ""
Expand Down
4 changes: 2 additions & 2 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba

overrides := in.overrides
if in.exportPush {
overrides = append(overrides, "*.output=type=registry")
overrides = append(overrides, "*.push=true")
}
if in.exportLoad {
overrides = append(overrides, "*.output=type=docker")
overrides = append(overrides, "*.load=true")
}
if cFlags.noCache != nil {
overrides = append(overrides, fmt.Sprintf("*.no-cache=%t", *cFlags.noCache))
Expand Down
1 change: 1 addition & 0 deletions docs/reference/buildx_bake.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ You can override the following fields:
* `context`
* `dockerfile`
* `labels`
* `load`
* `no-cache`
* `no-cache-filter`
* `output`
Expand Down

0 comments on commit 1a39131

Please sign in to comment.