2019-04-05 15:04:19 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-09-17 13:52:40 +08:00
|
|
|
"context"
|
2019-04-05 15:04:19 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2021-09-03 05:06:00 +08:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/bake"
|
2020-09-17 13:52:40 +08:00
|
|
|
"github.com/docker/buildx/build"
|
2021-10-30 12:15:04 +08:00
|
|
|
"github.com/docker/buildx/util/confutil"
|
2020-09-17 13:52:40 +08:00
|
|
|
"github.com/docker/buildx/util/progress"
|
2021-07-03 12:39:57 +08:00
|
|
|
"github.com/docker/buildx/util/tracing"
|
2019-04-05 15:04:19 +08:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bakeOptions struct {
|
|
|
|
files []string
|
|
|
|
overrides []string
|
2021-10-26 18:53:35 +08:00
|
|
|
printOnly bool
|
2019-04-05 15:04:19 +08:00
|
|
|
commonOptions
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error) {
|
2019-04-05 15:04:19 +08:00
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
2021-07-03 12:39:57 +08:00
|
|
|
ctx, end, err := tracing.TraceCurrentCommand(ctx, "bake")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
end(err)
|
|
|
|
}()
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
var url string
|
2021-07-13 08:53:04 +08:00
|
|
|
cmdContext := "cwd://"
|
2020-09-17 13:52:40 +08:00
|
|
|
|
|
|
|
if len(targets) > 0 {
|
|
|
|
if bake.IsRemoteURL(targets[0]) {
|
|
|
|
url = targets[0]
|
|
|
|
targets = targets[1:]
|
2021-07-13 08:53:04 +08:00
|
|
|
if len(targets) > 0 {
|
|
|
|
if bake.IsRemoteURL(targets[0]) {
|
|
|
|
cmdContext = targets[0]
|
|
|
|
targets = targets[1:]
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(targets) == 0 {
|
|
|
|
targets = []string{"default"}
|
|
|
|
}
|
|
|
|
|
2019-10-17 06:04:54 +08:00
|
|
|
overrides := in.overrides
|
|
|
|
if in.exportPush {
|
|
|
|
if in.exportLoad {
|
|
|
|
return errors.Errorf("push and load may not be set together at the moment")
|
|
|
|
}
|
2021-09-21 07:22:27 +08:00
|
|
|
overrides = append(overrides, "*.push=true")
|
2019-10-17 06:04:54 +08:00
|
|
|
} else if in.exportLoad {
|
|
|
|
overrides = append(overrides, "*.output=type=docker")
|
|
|
|
}
|
2019-10-17 06:10:17 +08:00
|
|
|
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))
|
|
|
|
}
|
2020-09-17 13:52:40 +08:00
|
|
|
contextPathHash, _ := os.Getwd()
|
|
|
|
|
|
|
|
ctx2, cancel := context.WithCancel(context.TODO())
|
|
|
|
defer cancel()
|
2022-10-25 17:55:36 +08:00
|
|
|
printer, err := progress.NewPrinter(ctx2, os.Stderr, os.Stderr, in.progress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-17 06:04:54 +08:00
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
defer func() {
|
|
|
|
if printer != nil {
|
|
|
|
err1 := printer.Wait()
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-04 18:08:13 +08:00
|
|
|
var dis []build.DriverInfo
|
2020-09-17 13:52:40 +08:00
|
|
|
var files []bake.File
|
|
|
|
var inp *bake.Input
|
2021-07-13 08:53:04 +08:00
|
|
|
|
2022-10-04 18:08:13 +08:00
|
|
|
// instance only needed for reading remote bake files or building
|
|
|
|
if url != "" || !in.printOnly {
|
|
|
|
dis, err = getInstanceOrDefault(ctx, dockerCli, in.builder, contextPathHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
if url != "" {
|
|
|
|
files, inp, err = bake.ReadRemoteFiles(ctx, dis, url, in.files, printer)
|
|
|
|
} else {
|
|
|
|
files, err = bake.ReadLocalFiles(in.files)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-25 04:54:51 +08:00
|
|
|
tgts, grps, err := bake.ReadTargets(ctx, files, targets, overrides, map[string]string{
|
2022-07-31 22:53:39 +08:00
|
|
|
// don't forget to update documentation if you add a new
|
2022-11-05 02:47:00 +08:00
|
|
|
// built-in variable: docs/manuals/bake/file-definition.md#built-in-variables
|
2021-09-03 05:06:00 +08:00
|
|
|
"BAKE_CMD_CONTEXT": cmdContext,
|
|
|
|
"BAKE_LOCAL_PLATFORM": platforms.DefaultString(),
|
2021-07-13 08:53:04 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function can update target context string from the input so call before printOnly check
|
2021-11-25 04:54:51 +08:00
|
|
|
bo, err := bake.TargetsToBuildOpt(tgts, inp)
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if in.printOnly {
|
2021-08-12 15:02:28 +08:00
|
|
|
dt, err := json.MarshalIndent(struct {
|
2021-11-25 04:54:51 +08:00
|
|
|
Group map[string]*bake.Group `json:"group,omitempty"`
|
2021-08-12 15:02:28 +08:00
|
|
|
Target map[string]*bake.Target `json:"target"`
|
|
|
|
}{
|
2022-09-12 20:46:48 +08:00
|
|
|
grps,
|
2021-11-25 04:54:51 +08:00
|
|
|
tgts,
|
2021-08-12 15:02:28 +08:00
|
|
|
}, "", " ")
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-17 13:52:40 +08:00
|
|
|
err = printer.Wait()
|
|
|
|
printer = nil
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
fmt.Fprintln(dockerCli.Out(), string(dt))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 12:17:27 +08:00
|
|
|
resp, err := build.Build(ctx, dis, bo, dockerAPI(dockerCli), confutil.ConfigDir(dockerCli), printer)
|
2021-06-30 13:41:21 +08:00
|
|
|
if err != nil {
|
2022-02-23 13:55:44 +08:00
|
|
|
return wrapBuildError(err, true)
|
2021-06-30 13:41:21 +08:00
|
|
|
}
|
|
|
|
|
2022-03-09 02:06:03 +08:00
|
|
|
if len(in.metadataFile) > 0 {
|
|
|
|
dt := make(map[string]interface{})
|
|
|
|
for t, r := range resp {
|
|
|
|
dt[t] = decodeExporterResponse(r.ExporterResponse)
|
|
|
|
}
|
|
|
|
if err := writeMetadataFile(in.metadataFile, dt); err != nil {
|
|
|
|
return err
|
2021-06-30 13:41:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
return err
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
2019-04-05 15:04:19 +08:00
|
|
|
var options bakeOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "bake [OPTIONS] [TARGET...]",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Short: "Build from a file",
|
2020-05-01 04:01:45 +08:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
// reset to nil to avoid override is unset
|
|
|
|
if !cmd.Flags().Lookup("no-cache").Changed {
|
|
|
|
options.noCache = nil
|
|
|
|
}
|
|
|
|
if !cmd.Flags().Lookup("pull").Changed {
|
|
|
|
options.pull = nil
|
|
|
|
}
|
2020-07-08 19:02:13 +08:00
|
|
|
options.commonOptions.builder = rootOpts.builder
|
2020-05-01 04:01:45 +08:00
|
|
|
return runBake(dockerCli, args, options)
|
|
|
|
},
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file")
|
2021-11-22 17:51:54 +08:00
|
|
|
flags.BoolVar(&options.exportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
|
2019-04-05 15:04:19 +08:00
|
|
|
flags.BoolVar(&options.printOnly, "print", false, "Print the options without building")
|
2021-11-22 17:51:54 +08:00
|
|
|
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--set=*.output=type=registry"`)
|
|
|
|
flags.StringArrayVar(&options.overrides, "set", nil, `Override target value (e.g., "targetpattern.key=value")`)
|
2019-04-05 15:04:19 +08:00
|
|
|
|
2020-03-26 07:32:46 +08:00
|
|
|
commonBuildFlags(&options.commonOptions, flags)
|
2019-04-05 15:04:19 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|