|
|
|
@ -2,6 +2,7 @@ package commands
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
@ -12,6 +13,7 @@ import (
|
|
|
|
|
"github.com/docker/buildx/util/progress"
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
|
"github.com/moby/buildkit/session/auth/authprovider"
|
|
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
@ -21,6 +23,8 @@ import (
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const defaultTargetName = "default"
|
|
|
|
|
|
|
|
|
|
type buildOptions struct {
|
|
|
|
|
commonOptions
|
|
|
|
|
contextPath string
|
|
|
|
@ -64,10 +68,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
|
|
|
|
@ -200,10 +205,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{defaultTargetName: 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
|
|
|
|
@ -213,11 +218,24 @@ 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 {
|
|
|
|
|
mdatab, err := json.MarshalIndent(resp[defaultTargetName].ExporterResponse, "", " ")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := ioutils.AtomicWriteFile(metadataFile, mdatab, 0644); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -333,6 +351,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 to the file")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func listToMap(values []string, defaultEnv bool) map[string]string {
|
|
|
|
|