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 24, 2021
1 parent e93dc9d commit 204e570
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 63 deletions.
35 changes: 17 additions & 18 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +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 "iidfile":
s := parts[1]
t.ImageIDFile = &s
case "metadata-file":
continue
default:
return nil, errors.Errorf("unknown key: %s", keys[1])
}
Expand Down Expand Up @@ -437,7 +436,7 @@ type Target struct {
Outputs []string `json:"output,omitempty" hcl:"output,optional"`
Pull *bool `json:"pull,omitempty" hcl:"pull,optional"`
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional"`
ImageIDFile *string `json:"iidfile,omitempty" hcl:"iidfile,optional"`
MetadataFile *string `json:"-" hcl:"-"`

// IMPORTANT: if you add more fields here, do not forget to update newOverrides and README.
}
Expand All @@ -452,13 +451,16 @@ func (t *Target) normalize() {
t.Outputs = removeDupes(t.Outputs)
}

func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Options, error) {
func TargetsToBuildOpt(m map[string]*Target, inp *Input, metadataFile string) (map[string]build.Options, error) {
m2 := make(map[string]build.Options, len(m))
for k, v := range m {
bo, err := toBuildOpt(v, inp)
if err != nil {
return nil, err
}
if len(metadataFile) > 0 {
bo.MetadataFile = metadataFile
}
m2[k] = *bo
}
return m2, nil
Expand Down Expand Up @@ -506,9 +508,9 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
if t.Pull != nil {
pull = *t.Pull
}
iidFile := ""
if t.ImageIDFile != nil {
iidFile = *t.ImageIDFile
metadataFile := ""
if t.MetadataFile != nil {
metadataFile = *t.MetadataFile
}

bi := build.Inputs{
Expand All @@ -521,13 +523,13 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
updateContext(&bi, inp)

bo := &build.Options{
Inputs: bi,
Tags: t.Tags,
BuildArgs: t.Args,
Labels: t.Labels,
NoCache: noCache,
Pull: pull,
ImageIDFile: iidFile,
Inputs: bi,
Tags: t.Tags,
BuildArgs: t.Args,
Labels: t.Labels,
NoCache: noCache,
Pull: pull,
MetadataFile: metadataFile,
}

platforms, err := platformutil.Parse(t.Platforms)
Expand Down Expand Up @@ -635,9 +637,6 @@ func merge(t1, t2 *Target) *Target {
if t2.NoCache != nil {
t1.NoCache = t2.NoCache
}
if t2.ImageIDFile != nil {
t1.ImageIDFile = t2.ImageIDFile
}
t1.Inherits = append(t1.Inherits, t2.Inherits...)
return t1
}
Expand Down
78 changes: 54 additions & 24 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,35 @@ var (
)

type Options struct {
Inputs Inputs
Tags []string
Labels map[string]string
BuildArgs map[string]string
Pull bool
ImageIDFile string
ExtraHosts []string
NetworkMode string

NoCache bool
Target string
Platforms []specs.Platform
Exports []client.ExportEntry
Session []session.Attachable

CacheFrom []client.CacheOptionsEntry
CacheTo []client.CacheOptionsEntry

Allow []entitlements.Entitlement
Inputs Inputs `json:"inputs,omitempty"`
Tags []string `json:"tags,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
BuildArgs map[string]string `json:"build-args,omitempty"`
Pull bool `json:"pull,omitempty"`
ImageIDFile string `json:"iidfile,omitempty"`
MetadataFile string `json:"metadata-file,omitempty"`
ExtraHosts []string `json:"extra-hosts,omitempty"`
NetworkMode string `json:"network-mode,omitempty"`

NoCache bool `json:"no-cache,omitempty"`
Target string `json:"target,omitempty"`
Platforms []specs.Platform `json:"platform,omitempty"`
Exports []client.ExportEntry `json:"-"`
Session []session.Attachable `json:"-"`

CacheFrom []client.CacheOptionsEntry `json:"cache-from,omitempty"`
CacheTo []client.CacheOptionsEntry `json:"cache-to,omitempty"`

Allow []entitlements.Entitlement `json:"allow,omitempty"`
// DockerTarget
}

type Inputs struct {
ContextPath string
DockerfilePath string
InStream io.Reader
ContextState *llb.State
DockerfileInline string
ContextPath string `json:"context,omitempty"`
DockerfilePath string `json:"dockerfile,omitempty"`
InStream io.Reader `json:"-"`
ContextState *llb.State `json:"-"`
DockerfileInline string `json:"dockerfile-inline,omitempty"`
}

type DriverInfo struct {
Expand Down Expand Up @@ -342,6 +343,12 @@ func toSolveOpt(ctx context.Context, d driver.Driver, multiDriver bool, opt Opti
return nil, nil, errors.Wrap(err, "removing image ID file")
}
}
if opt.MetadataFile != "" {
// Avoid leaving a stale file if we eventually fail
if err := os.Remove(opt.MetadataFile); err != nil && !os.IsNotExist(err) {
return nil, nil, errors.Wrap(err, "removing metadata file")
}
}

// inline cache from build arg
if v, ok := opt.BuildArgs["BUILDKIT_INLINE_CACHE"]; ok {
Expand Down Expand Up @@ -656,6 +663,29 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
respMu.Lock()
resp[k] = res[0]
respMu.Unlock()

if opt.MetadataFile != "" {
metadata := make([]map[string]string, 0, len(res))
for _, r := range res {
if _, ok := r.ExporterResponse["containerimage.digest"]; ok {
metadata = append(metadata, r.ExporterResponse)
}
}
mdatab, err := json.MarshalIndent(map[string]interface{}{"metadata": struct {
BuildOpts Options `json:"build-opts"`
SolveOpts []map[string]string `json:"solve-opts"`
}{
BuildOpts: opt,
SolveOpts: metadata,
}}, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(opt.MetadataFile, mdatab, 0644); err != nil {
return err
}
}

if len(res) == 1 {
if opt.ImageIDFile != "" {
return ioutil.WriteFile(opt.ImageIDFile, []byte(res[0].ExporterResponse["containerimage.digest"]), 0644)
Expand Down
6 changes: 3 additions & 3 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ 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.imageIDFile) > 0 {
overrides = append(overrides, fmt.Sprintf("*.iidfile=%s", in.imageIDFile))
if len(in.metadataFile) > 0 {
overrides = append(overrides, fmt.Sprintf("*.metadata-file=%s", in.metadataFile))
}
contextPathHash, _ := os.Getwd()

Expand Down Expand Up @@ -106,7 +106,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
return nil
}

bo, err := bake.TargetsToBuildOpt(m, inp)
bo, err := bake.TargetsToBuildOpt(m, inp, in.metadataFile)
if err != nil {
return err
}
Expand Down
33 changes: 18 additions & 15 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type buildOptions struct {
secrets []string
ssh []string
outputs []string
imageIDFile string
extraHosts []string
networkMode string

Expand Down Expand Up @@ -63,11 +64,11 @@ type buildOptions struct {
}

type commonOptions struct {
builder string
noCache *bool
progress string
pull *bool
imageIDFile string
builder string
noCache *bool
progress string
pull *bool
metadataFile string
// golangci-lint#826
// nolint:structcheck
exportPush bool
Expand Down Expand Up @@ -100,15 +101,16 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
DockerfilePath: in.dockerfileName,
InStream: os.Stdin,
},
Tags: in.tags,
Labels: listToMap(in.labels, false),
BuildArgs: listToMap(in.buildArgs, true),
Pull: pull,
NoCache: noCache,
Target: in.target,
ImageIDFile: in.imageIDFile,
ExtraHosts: in.extraHosts,
NetworkMode: in.networkMode,
Tags: in.tags,
Labels: listToMap(in.labels, false),
BuildArgs: listToMap(in.buildArgs, true),
Pull: pull,
NoCache: noCache,
Target: in.target,
ImageIDFile: in.imageIDFile,
MetadataFile: in.metadataFile,
ExtraHosts: in.extraHosts,
NetworkMode: in.networkMode,
}

platforms, err := platformutil.Parse(in.platforms)
Expand Down Expand Up @@ -265,6 +267,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags.StringVar(&options.networkMode, "network", "default", "Set the networking mode for the RUN instructions during build")
flags.StringSliceVar(&options.extraHosts, "add-host", []string{}, "Add a custom host-to-IP mapping (host:ip)")
flags.SetAnnotation("add-host", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#add-entries-to-container-hosts-file---add-host"})
flags.StringVar(&options.imageIDFile, "iidfile", "", "Write the image ID to the file")
flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer")
flags.MarkHidden("quiet")
flags.MarkHidden("squash")
Expand Down Expand Up @@ -332,7 +335,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.imageIDFile, "iidfile", "", "Write the image ID to the file")
flags.StringVar(&options.metadataFile, "metadata-file", "", "Write build metadata to a file as JSON")
}

func listToMap(values []string, defaultEnv bool) map[string]string {
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/buildx_bake.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Build from a file
| --- | --- |
| `--builder string` | Override the configured builder instance |
| [`-f`](#file), [`--file stringArray`](#file) | Build definition file |
| `--iidfile string` | Write the image ID to the file |
| `--load` | Shorthand for --set=*.output=type=docker |
| `--metadata-file string` | Write build metadata to a file as JSON |
| [`--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 Expand Up @@ -204,8 +204,8 @@ target "db" {

Complete list of valid target fields:

`args`, `cache-from`, `cache-to`, `context`, `dockerfile`, `iidfile`, `inherits`,
`labels`, `no-cache`, `output`, `platform`, `pull`, `secrets`, `ssh`, `tags`, `target`
`args`, `cache-from`, `cache-to`, `context`, `dockerfile`, `inherits`, `labels`,
`no-cache`, `output`, `platform`, `pull`, `secrets`, `ssh`, `tags`, `target`

### HCL variables and functions

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 metadata to a file as JSON |
| `--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 204e570

Please sign in to comment.