Skip to content

Commit

Permalink
support metadata file with call flag for build and bake commands
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Aug 5, 2024
1 parent 6034c58 commit 5ec076f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
8 changes: 5 additions & 3 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,15 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
if progressMode != progressui.QuietMode && progressMode != progressui.RawJSONMode {
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
}
if callFunc == nil && len(in.metadataFile) > 0 {
if len(in.metadataFile) > 0 {
dt := make(map[string]interface{})
for t, r := range resp {
dt[t] = decodeExporterResponse(r.ExporterResponse)
}
if warnings := printer.Warnings(); len(warnings) > 0 && confutil.MetadataWarningsEnabled() {
dt["buildx.build.warnings"] = warnings
if callFunc == nil {
if warnings := printer.Warnings(); len(warnings) > 0 && confutil.MetadataWarningsEnabled() {
dt["buildx.build.warnings"] = warnings
}
}
if err := writeMetadataFile(in.metadataFile, dt); err != nil {
return err
Expand Down
33 changes: 21 additions & 12 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,23 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
return errors.Wrap(err, "writing image ID file")
}
}
if options.metadataFile != "" {
dt := decodeExporterResponse(resp.ExporterResponse)
if opts.PrintFunc == nil {
if warnings := printer.Warnings(); len(warnings) > 0 && confutil.MetadataWarningsEnabled() {
dt["buildx.build.warnings"] = warnings
}
}
if err := writeMetadataFile(options.metadataFile, dt); err != nil {
return err
}
}
if opts.PrintFunc != nil {
if exitcode, err := printResult(dockerCli.Out(), opts.PrintFunc, resp.ExporterResponse); err != nil {
return err
} else if exitcode != 0 {
os.Exit(exitcode)
}
} else if options.metadataFile != "" {
dt := decodeExporterResponse(resp.ExporterResponse)
if warnings := printer.Warnings(); len(warnings) > 0 && confutil.MetadataWarningsEnabled() {
dt["buildx.build.warnings"] = warnings
}
if err := writeMetadataFile(options.metadataFile, dt); err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -733,10 +736,16 @@ func writeMetadataFile(filename string, dt interface{}) error {
func decodeExporterResponse(exporterResponse map[string]string) map[string]interface{} {
out := make(map[string]interface{})
for k, v := range exporterResponse {
dt, err := base64.StdEncoding.DecodeString(v)
if err != nil {
out[k] = v
continue
var dt []byte
var err error
if k == "result.json" {
dt = []byte(v)
} else {
dt, err = base64.StdEncoding.DecodeString(v)
if err != nil {
out[k] = v
continue
}
}
var raw map[string]interface{}
if err = json.Unmarshal(dt, &raw); err != nil || len(raw) == 0 {
Expand Down

0 comments on commit 5ec076f

Please sign in to comment.