Skip to content

Commit

Permalink
Add metadata-file flag
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 29, 2021
1 parent 908a856 commit f86b850
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
2 changes: 2 additions & 0 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ func (c Config) newOverrides(v []string) (map[string]*Target, error) {
return nil, errors.Errorf("invalid value %s for boolean key pull", parts[1])
}
t.Pull = &pull
case "metadata-file":
continue
default:
return nil, errors.Errorf("unknown key: %s", keys[1])
}
Expand Down
10 changes: 5 additions & 5 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ type Options struct {
CacheTo []client.CacheOptionsEntry

Allow []entitlements.Entitlement
// DockerTarget
}

type Inputs struct {
Expand Down Expand Up @@ -336,7 +335,7 @@ func toSolveOpt(ctx context.Context, d driver.Driver, multiDriver bool, opt Opti
}
}()

if opt.ImageIDFile != "" {
if len(opt.ImageIDFile) > 0 {
// Avoid leaving a stale file if we eventually fail
if err := os.Remove(opt.ImageIDFile); err != nil && !os.IsNotExist(err) {
return nil, nil, errors.Wrap(err, "removing image ID file")
Expand Down Expand Up @@ -449,7 +448,7 @@ func toSolveOpt(ctx context.Context, d driver.Driver, multiDriver bool, opt Opti

// set up exporters
for i, e := range opt.Exports {
if (e.Type == "local" || e.Type == "tar") && opt.ImageIDFile != "" {
if (e.Type == "local" || e.Type == "tar") && len(opt.ImageIDFile) > 0 {
return nil, nil, errors.Errorf("local and tar exporters are incompatible with image ID file")
}
if e.Type == "oci" && !d.Features()[driver.OCIExporter] {
Expand Down Expand Up @@ -656,8 +655,9 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
respMu.Lock()
resp[k] = res[0]
respMu.Unlock()

if len(res) == 1 {
if opt.ImageIDFile != "" {
if len(opt.ImageIDFile) > 0 {
return ioutil.WriteFile(opt.ImageIDFile, []byte(res[0].ExporterResponse["containerimage.digest"]), 0644)
}
return nil
Expand Down Expand Up @@ -687,7 +687,7 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
if err != nil {
return err
}
if opt.ImageIDFile != "" {
if len(opt.ImageIDFile) > 0 {
return ioutil.WriteFile(opt.ImageIDFile, []byte(desc.Digest), 0644)
}

Expand Down
29 changes: 27 additions & 2 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/docker/buildx/bake"
Expand Down Expand Up @@ -53,6 +54,9 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
if in.pull != nil {
overrides = append(overrides, fmt.Sprintf("*.pull=%t", *in.pull))
}
if len(in.metadataFile) > 0 {
overrides = append(overrides, fmt.Sprintf("*.metadata-file=%s", in.metadataFile))
}
contextPathHash, _ := os.Getwd()

ctx2, cancel := context.WithCancel(context.TODO())
Expand Down Expand Up @@ -90,7 +94,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
}

if in.printOnly {
dt, err := json.MarshalIndent(map[string]map[string]*bake.Target{"target": m}, "", " ")
dt, err := json.MarshalIndent(map[string]map[string]*bake.Target{"target": m}, "", " ")
if err != nil {
return err
}
Expand All @@ -108,7 +112,28 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
return err
}

_, err = build.Build(ctx, dis, bo, dockerAPI(dockerCli), dockerCli.ConfigFile(), printer)
resp, err := build.Build(ctx, dis, bo, dockerAPI(dockerCli), dockerCli.ConfigFile(), printer)
if err != nil {
return err
}

if len(in.metadataFile) > 0 && resp != nil {
if err := os.Remove(in.metadataFile); err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "removing metadata file")
}
mdata := map[string]map[string]string{}
for k, r := range resp {
mdata[k] = r.ExporterResponse
}
mdatab, err := json.MarshalIndent(mdata, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(in.metadataFile, mdatab, 0644); err != nil {
return err
}
}

return err
}

Expand Down
35 changes: 28 additions & 7 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package commands

import (
"context"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -64,10 +66,11 @@ type buildOptions struct {
}

type commonOptions struct {
builder string
noCache *bool
progress string
pull *bool
builder string
noCache *bool
progress string
pull *bool
metadataFile string
// golangci-lint#826
// nolint:structcheck
exportPush bool
Expand Down Expand Up @@ -200,10 +203,10 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
contextPathHash = in.contextPath
}

return buildTargets(ctx, dockerCli, map[string]build.Options{"default": opts}, in.progress, contextPathHash, in.builder)
return buildTargets(ctx, dockerCli, map[string]build.Options{"default": opts}, in.progress, contextPathHash, in.builder, in.metadataFile)
}

func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, instance string) error {
func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, instance string, metadataFile string) error {
dis, err := getInstanceOrDefault(ctx, dockerCli, instance, contextPathHash)
if err != nil {
return err
Expand All @@ -213,11 +216,28 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu
defer cancel()
printer := progress.NewPrinter(ctx2, os.Stderr, progressMode)

_, err = build.Build(ctx, dis, opts, dockerAPI(dockerCli), dockerCli.ConfigFile(), printer)
resp, err := build.Build(ctx, dis, opts, dockerAPI(dockerCli), dockerCli.ConfigFile(), printer)
err1 := printer.Wait()
if err == nil {
err = err1
}
if err != nil {
return err
}

if len(metadataFile) > 0 && resp != nil {
// Avoid leaving a stale file if we eventually fail
if err := os.Remove(metadataFile); err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "removing metadata file")
}
mdatab, err := json.MarshalIndent(resp["default"].ExporterResponse, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(metadataFile, mdatab, 0644); err != nil {
return err
}
}

return err
}
Expand Down Expand Up @@ -333,6 +353,7 @@ func commonBuildFlags(options *commonOptions, flags *pflag.FlagSet) {
flags.StringVar(&options.progress, "progress", defaultProgress, "Set type of progress output (auto, plain, tty). Use plain to show container output")

options.pull = flags.Bool("pull", false, "Always attempt to pull a newer version of the image")
flags.StringVar(&options.metadataFile, "metadata-file", "", "Write build result metadata")
}

func listToMap(values []string, defaultEnv bool) map[string]string {
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 @@ -18,6 +18,7 @@ Build from a file
| `--builder string` | Override the configured builder instance |
| [`-f`](#file), [`--file stringArray`](#file) | Build definition file |
| `--load` | Shorthand for --set=*.output=type=docker |
| `--metadata-file string` | Write build result metadata |
| [`--no-cache`](#no-cache) | Do not use cache when building the image |
| [`--print`](#print) | Print the options without building |
| [`--progress string`](#progress) | Set type of progress output (auto, plain, tty). Use plain to show container output |
Expand Down
1 change: 1 addition & 0 deletions docs/reference/buildx_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Start a build
| `--iidfile string` | Write the image ID to the file |
| `--label stringArray` | Set metadata for an image |
| [`--load`](#load) | Shorthand for --output=type=docker |
| `--metadata-file string` | Write build result metadata |
| `--network string` | Set the networking mode for the RUN instructions during build |
| `--no-cache` | Do not use cache when building the image |
| [`-o`](#output), [`--output stringArray`](#output) | Output destination (format: type=local,dest=path) |
Expand Down

0 comments on commit f86b850

Please sign in to comment.