Skip to content

Commit

Permalink
Add imageIDFile opt for bake
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 May 2, 2021
1 parent 908a856 commit 9d9e812
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
24 changes: 18 additions & 6 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ 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
default:
return nil, errors.Errorf("unknown key: %s", keys[1])
}
Expand Down Expand Up @@ -434,6 +437,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"`

// IMPORTANT: if you add more fields here, do not forget to update newOverrides and README.
}
Expand Down Expand Up @@ -502,6 +506,10 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
if t.Pull != nil {
pull = *t.Pull
}
iidFile := ""
if t.ImageIDFile != nil {
iidFile = *t.ImageIDFile
}

bi := build.Inputs{
ContextPath: contextPath,
Expand All @@ -513,12 +521,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,
Inputs: bi,
Tags: t.Tags,
BuildArgs: t.Args,
Labels: t.Labels,
NoCache: noCache,
Pull: pull,
ImageIDFile: iidFile,
}

platforms, err := platformutil.Parse(t.Platforms)
Expand Down Expand Up @@ -626,6 +635,9 @@ 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
3 changes: 3 additions & 0 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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.imageIDFile) > 0 {
overrides = append(overrides, fmt.Sprintf("*.iidfile=%s", in.imageIDFile))
}
contextPathHash, _ := os.Getwd()

ctx2, cancel := context.WithCancel(context.TODO())
Expand Down
12 changes: 6 additions & 6 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type buildOptions struct {
secrets []string
ssh []string
outputs []string
imageIDFile string
extraHosts []string
networkMode string

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

type commonOptions struct {
builder string
noCache *bool
progress string
pull *bool
builder string
noCache *bool
progress string
pull *bool
imageIDFile string
// golangci-lint#826
// nolint:structcheck
exportPush bool
Expand Down Expand Up @@ -265,7 +265,6 @@ 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 @@ -333,6 +332,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")
}

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 @@ -17,6 +17,7 @@ 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 |
| [`--no-cache`](#no-cache) | Do not use cache when building the image |
| [`--print`](#print) | Print the options without building |
Expand Down

0 comments on commit 9d9e812

Please sign in to comment.