bake: allow overriding no-cache and pull per target via --set

Signed-off-by: Tibor Vass <tibor@docker.com>
pull/165/head
Tibor Vass 5 years ago
parent 0e1f0e3c73
commit 2a257a8252

@ -566,8 +566,12 @@ docker buildx bake --set target.args.mybuildarg=value
docker buildx bake --set target.platform=linux/arm64 docker buildx bake --set target.platform=linux/arm64
docker buildx bake --set foo*.args.mybuildarg=value # overrides build arg for all targets starting with 'foo' docker buildx bake --set foo*.args.mybuildarg=value # overrides build arg for all targets starting with 'foo'
docker buildx bake --set *.platform=linux/arm64 # overrides platform for all targets docker buildx bake --set *.platform=linux/arm64 # overrides platform for all targets
docker buildx bake --set foo*.no-cache # bypass caching only for targets starting with 'foo'
``` ```
Complete list of overridable fields:
args, cache-from, cache-to, context, dockerfile, labels, no-cache, output, platform, pull, secrets, ssh, tags, target
#### File definition #### File definition
In addition to compose files, bake supports a JSON and an equivalent HCL file format for defining build groups and targets. In addition to compose files, bake supports a JSON and an equivalent HCL file format for defining build groups and targets.
@ -607,6 +611,8 @@ target "db" {
} }
``` ```
Complete list of valid target fields:
args, cache-from, cache-to, context, dockerfile, inherits, labels, no-cache, output, platform, pull, secrets, ssh, tags, target
### `buildx imagetools create [OPTIONS] [SOURCE] [SOURCE...]` ### `buildx imagetools create [OPTIONS] [SOURCE] [SOURCE...]`

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"github.com/docker/buildx/build" "github.com/docker/buildx/build"
@ -194,6 +195,18 @@ func (c Config) newOverrides(v []string) (map[string]Target, error) {
t.Platforms = append(t.Platforms, parts[1]) t.Platforms = append(t.Platforms, parts[1])
case "output": case "output":
t.Outputs = append(t.Outputs, parts[1]) t.Outputs = append(t.Outputs, parts[1])
case "no-cache":
noCache, err := strconv.ParseBool(parts[1])
if err != nil {
return nil, errors.Errorf("invalid value %s for boolean key no-cache", parts[1])
}
t.NoCache = noCache
case "pull":
pull, err := strconv.ParseBool(parts[1])
if err != nil {
return nil, errors.Errorf("invalid value %s for boolean key pull", parts[1])
}
t.Pull = pull
default: default:
return nil, errors.Errorf("unknown key: %s", keys[1]) return nil, errors.Errorf("unknown key: %s", keys[1])
} }
@ -270,7 +283,9 @@ type Group struct {
} }
type Target struct { type Target struct {
Inherits []string `json:"inherits,omitempty" hcl:"inherits,omitempty"` // Inherits is the only field that cannot be overridden with --set
Inherits []string `json:"inherits,omitempty" hcl:"inherits,omitempty"`
Context *string `json:"context,omitempty" hcl:"context,omitempty"` Context *string `json:"context,omitempty" hcl:"context,omitempty"`
Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,omitempty"` Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,omitempty"`
Args map[string]string `json:"args,omitempty" hcl:"args,omitempty"` Args map[string]string `json:"args,omitempty" hcl:"args,omitempty"`
@ -283,6 +298,9 @@ type Target struct {
SSH []string `json:"ssh,omitempty" hcl:"ssh,omitempty"` SSH []string `json:"ssh,omitempty" hcl:"ssh,omitempty"`
Platforms []string `json:"platforms,omitempty" hcl:"platforms,omitempty"` Platforms []string `json:"platforms,omitempty" hcl:"platforms,omitempty"`
Outputs []string `json:"output,omitempty" hcl:"output,omitempty"` Outputs []string `json:"output,omitempty" hcl:"output,omitempty"`
Pull bool `json:"pull,omitempty": hcl:"pull,omitempty"`
NoCache bool `json:"no-cache,omitempty": hcl:"no-cache,omitempty"`
// IMPORTANT: if you add more fields here, do not forget to update newOverrides and README.
} }
func (t *Target) normalize() { func (t *Target) normalize() {
@ -295,10 +313,10 @@ func (t *Target) normalize() {
t.Outputs = removeDupes(t.Outputs) t.Outputs = removeDupes(t.Outputs)
} }
func TargetsToBuildOpt(m map[string]Target, noCache, pull bool) (map[string]build.Options, error) { func TargetsToBuildOpt(m map[string]Target) (map[string]build.Options, error) {
m2 := make(map[string]build.Options, len(m)) m2 := make(map[string]build.Options, len(m))
for k, v := range m { for k, v := range m {
bo, err := toBuildOpt(v, noCache, pull) bo, err := toBuildOpt(v)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -307,7 +325,7 @@ func TargetsToBuildOpt(m map[string]Target, noCache, pull bool) (map[string]buil
return m2, nil return m2, nil
} }
func toBuildOpt(t Target, noCache, pull bool) (*build.Options, error) { func toBuildOpt(t Target) (*build.Options, error) {
if v := t.Context; v != nil && *v == "-" { if v := t.Context; v != nil && *v == "-" {
return nil, errors.Errorf("context from stdin not allowed in bake") return nil, errors.Errorf("context from stdin not allowed in bake")
} }
@ -336,8 +354,8 @@ func toBuildOpt(t Target, noCache, pull bool) (*build.Options, error) {
Tags: t.Tags, Tags: t.Tags,
BuildArgs: t.Args, BuildArgs: t.Args,
Labels: t.Labels, Labels: t.Labels,
NoCache: noCache, NoCache: t.NoCache,
Pull: pull, Pull: t.Pull,
} }
platforms, err := platformutil.Parse(t.Platforms) platforms, err := platformutil.Parse(t.Platforms)

@ -46,6 +46,12 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) error {
} else if in.exportLoad { } else if in.exportLoad {
overrides = append(overrides, "*.output=type=docker") overrides = append(overrides, "*.output=type=docker")
} }
if in.noCache != nil {
overrides = append(overrides, fmt.Sprintf("*.no-cache=%t", *in.noCache))
}
if in.pull != nil {
overrides = append(overrides, fmt.Sprintf("*.pull=%t", *in.pull))
}
m, err := bake.ReadTargets(ctx, in.files, targets, overrides) m, err := bake.ReadTargets(ctx, in.files, targets, overrides)
if err != nil { if err != nil {
@ -61,7 +67,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) error {
return nil return nil
} }
bo, err := bake.TargetsToBuildOpt(m, in.noCache, in.pull) bo, err := bake.TargetsToBuildOpt(m)
if err != nil { if err != nil {
return err return err
} }

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/docker/buildx/build" "github.com/docker/buildx/build"
"github.com/docker/buildx/util/flagutil"
"github.com/docker/buildx/util/platformutil" "github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress" "github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
@ -62,9 +63,9 @@ type buildOptions struct {
} }
type commonOptions struct { type commonOptions struct {
noCache bool noCache *bool
progress string progress string
pull bool pull *bool
exportPush bool exportPush bool
exportLoad bool exportLoad bool
} }
@ -79,6 +80,15 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
ctx := appcontext.Context() ctx := appcontext.Context()
noCache := false
if in.noCache != nil {
noCache = *in.noCache
}
pull := false
if in.pull != nil {
pull = *in.pull
}
opts := build.Options{ opts := build.Options{
Inputs: build.Inputs{ Inputs: build.Inputs{
ContextPath: in.contextPath, ContextPath: in.contextPath,
@ -88,8 +98,8 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
Tags: in.tags, Tags: in.tags,
Labels: listToMap(in.labels, false), Labels: listToMap(in.labels, false),
BuildArgs: listToMap(in.buildArgs, true), BuildArgs: listToMap(in.buildArgs, true),
Pull: in.pull, Pull: pull,
NoCache: in.noCache, NoCache: noCache,
Target: in.target, Target: in.target,
ImageIDFile: in.imageIDFile, ImageIDFile: in.imageIDFile,
ExtraHosts: in.extraHosts, ExtraHosts: in.extraHosts,
@ -293,9 +303,9 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
} }
func commonFlags(options *commonOptions, flags *pflag.FlagSet) { func commonFlags(options *commonOptions, flags *pflag.FlagSet) {
flags.BoolVar(&options.noCache, "no-cache", false, "Do not use cache when building the image") flags.Var(flagutil.Tristate(options.noCache), "no-cache", "Do not use cache when building the image")
flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output") flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
flags.BoolVar(&options.pull, "pull", false, "Always attempt to pull a newer version of the image") flags.Var(flagutil.Tristate(options.pull), "pull", "Always attempt to pull a newer version of the image")
} }
func listToMap(values []string, defaultEnv bool) map[string]string { func listToMap(values []string, defaultEnv bool) map[string]string {

@ -0,0 +1,36 @@
package flagutil
import "strconv"
type tristate struct {
opt *bool
}
// Tristate is a tri-state boolean flag type.
// It can be set, but not unset.
func Tristate(opt *bool) tristate {
return tristate{opt}
}
func (t tristate) Type() string {
return "tristate"
}
func (t tristate) String() string {
if t.opt == nil {
return "(unset)"
}
if *t.opt {
return "true"
}
return "false"
}
func (t tristate) Set(s string) error {
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
t.opt = &b
return nil
}
Loading…
Cancel
Save