From a238b05a4c89ac9bcd4639494237cda63267f5aa Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 23 Apr 2019 14:18:54 -0700 Subject: [PATCH 1/2] bake: add output key Signed-off-by: Tonis Tiigi --- bake/bake.go | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index a9e79cd2..3cb886a1 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -132,6 +132,8 @@ func (c Config) setOverrides(v []string) error { t.Tags = append(t.Tags, parts[1]) case "cache-from": t.CacheFrom = append(t.CacheFrom, parts[1]) + case "cache-to": + t.CacheTo = append(t.CacheTo, parts[1]) case "target": s := parts[1] t.Target = &s @@ -141,6 +143,8 @@ func (c Config) setOverrides(v []string) error { t.SSH = append(t.SSH, parts[1]) case "platform": t.Platforms = append(t.Platforms, parts[1]) + case "output": + t.Outputs = append(t.Outputs, parts[1]) default: return errors.Errorf("unknown key: %s", keys[1]) } @@ -216,18 +220,19 @@ type Group struct { } type Target struct { - Inherits []string `json:"inherits,omitempty"` - Context *string `json:"context,omitempty"` - Dockerfile *string `json:"dockerfile,omitempty"` - Args map[string]string `json:"args,omitempty"` - Labels map[string]string `json:"labels,omitempty"` - Tags []string `json:"tags,omitempty"` - CacheFrom []string `json:"cache-from,omitempty"` - CacheTo []string `json:"cache-to,omitempty"` - Target *string `json:"target,omitempty"` - Secrets []string `json:"secret,omitempty"` - SSH []string `json:"ssh,omitempty"` - Platforms []string `json:"platform,omitempty"` + Inherits []string `json:"inherits,omitempty" hcl:"inherits,omitempty"` + Context *string `json:"context,omitempty" hcl:"context,omitempty"` + Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,omitempty"` + Args map[string]string `json:"args,omitempty" hcl:"args,omitempty"` + Labels map[string]string `json:"labels,omitempty" hcl:"labels,omitempty"` + Tags []string `json:"tags,omitempty" hcl:"tags,omitempty"` + CacheFrom []string `json:"cache-from,omitempty" hcl:"cache-from,omitempty"` + CacheTo []string `json:"cache-to,omitempty" hcl:"cache-to,omitempty"` + Target *string `json:"target,omitempty" hcl:"target,omitempty"` + Secrets []string `json:"secret,omitempty" hcl:"secret,omitempty"` + SSH []string `json:"ssh,omitempty" hcl:"ssh,omitempty"` + Platforms []string `json:"platforms,omitempty" hcl:"platforms,omitempty"` + Outputs []string `json:"output,omitempty" hcl:"output,omitempty"` } func (t *Target) normalize() { @@ -235,6 +240,9 @@ func (t *Target) normalize() { t.Secrets = removeDupes(t.Secrets) t.SSH = removeDupes(t.SSH) t.Platforms = removeDupes(t.Platforms) + t.CacheFrom = removeDupes(t.CacheFrom) + t.CacheTo = removeDupes(t.CacheTo) + t.Outputs = removeDupes(t.Outputs) } func TargetsToBuildOpt(m map[string]Target) (map[string]build.Options, error) { @@ -312,6 +320,12 @@ func toBuildOpt(t Target) (*build.Options, error) { } bo.CacheTo = cacheExports + outputs, err := build.ParseOutputs(t.Outputs) + if err != nil { + return nil, err + } + bo.Exports = outputs + return bo, nil } @@ -341,9 +355,6 @@ func merge(t1, t2 Target) Target { if t2.Tags != nil { // no merge t1.Tags = t2.Tags } - if t2.CacheFrom != nil { - t1.CacheFrom = t2.CacheFrom - } if t2.Target != nil { t1.Target = t2.Target } @@ -356,12 +367,15 @@ func merge(t1, t2 Target) Target { if t2.Platforms != nil { // no merge t1.Platforms = t2.Platforms } - if len(t2.CacheFrom) > 0 { // no merge - t1.CacheFrom = t2.CacheFrom + if t2.CacheFrom != nil { // no merge + t1.CacheFrom = append(t1.CacheFrom, t2.CacheFrom...) } - if len(t2.CacheTo) > 0 { // no merge + if t2.CacheTo != nil { // no merge t1.CacheTo = t2.CacheTo } + if t2.Outputs != nil { // no merge + t1.Outputs = t2.Outputs + } t1.Inherits = append(t1.Inherits, t2.Inherits...) return t1 } From b979216b2d6c69d3eef6820bad37b3785cd4b68b Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 23 Apr 2019 14:15:30 -0700 Subject: [PATCH 2/2] build: support for tar output Signed-off-by: Tonis Tiigi --- build/output.go | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/build/output.go b/build/output.go index 31430cd4..ac4d0ff5 100644 --- a/build/output.go +++ b/build/output.go @@ -21,29 +21,40 @@ func ParseOutputs(inp []string) ([]client.ExportEntry, error) { if err != nil { return nil, err } - if len(fields) == 1 && fields[0] == s && !strings.HasPrefix(s, "type=") { - outs = append(outs, client.ExportEntry{ - Type: client.ExporterLocal, - OutputDir: s, - }) - continue - } out := client.ExportEntry{ Attrs: map[string]string{}, } - for _, field := range fields { - parts := strings.SplitN(field, "=", 2) - if len(parts) != 2 { - return nil, errors.Errorf("invalid value %s", field) + if len(fields) == 1 && fields[0] == s && !strings.HasPrefix(s, "type=") { + if s != "-" { + outs = append(outs, client.ExportEntry{ + Type: client.ExporterLocal, + OutputDir: s, + }) + continue } - key := strings.TrimSpace(strings.ToLower(parts[0])) - value := parts[1] - switch key { - case "type": - out.Type = value - default: - out.Attrs[key] = value + out = client.ExportEntry{ + Type: client.ExporterTar, + Attrs: map[string]string{ + "dest": s, + }, + } + } + + if out.Type == "" { + for _, field := range fields { + parts := strings.SplitN(field, "=", 2) + if len(parts) != 2 { + return nil, errors.Errorf("invalid value %s", field) + } + key := strings.TrimSpace(strings.ToLower(parts[0])) + value := parts[1] + switch key { + case "type": + out.Type = value + default: + out.Attrs[key] = value + } } } if out.Type == "" { @@ -59,7 +70,7 @@ func ParseOutputs(inp []string) ([]client.ExportEntry, error) { } out.OutputDir = dest delete(out.Attrs, "dest") - case client.ExporterOCI, client.ExporterDocker: + case client.ExporterOCI, client.ExporterDocker, client.ExporterTar: dest, ok := out.Attrs["dest"] if !ok { if out.Type != client.ExporterDocker {