mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	build: add support for named contexts
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
		@@ -14,6 +14,7 @@ import (
 | 
				
			|||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
						"syscall"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/containerd/containerd/images"
 | 
						"github.com/containerd/containerd/images"
 | 
				
			||||||
@@ -79,6 +80,7 @@ type Inputs struct {
 | 
				
			|||||||
	InStream         io.Reader
 | 
						InStream         io.Reader
 | 
				
			||||||
	ContextState     *llb.State
 | 
						ContextState     *llb.State
 | 
				
			||||||
	DockerfileInline string
 | 
						DockerfileInline string
 | 
				
			||||||
 | 
						NamedContexts    map[string]string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type DriverInfo struct {
 | 
					type DriverInfo struct {
 | 
				
			||||||
@@ -1080,6 +1082,27 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	target.FrontendAttrs["filename"] = dockerfileName
 | 
						target.FrontendAttrs["filename"] = dockerfileName
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for k, v := range inp.NamedContexts {
 | 
				
			||||||
 | 
							target.FrontendAttrs["frontend.caps"] = "moby.buildkit.frontend.contexts+forward"
 | 
				
			||||||
 | 
							if urlutil.IsGitURL(v) || urlutil.IsURL(v) || strings.HasPrefix(v, "docker-image://") {
 | 
				
			||||||
 | 
								target.FrontendAttrs["context:"+k] = v
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							st, err := os.Stat(v)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, errors.Wrapf(err, "failed to get context %v", k)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if !st.IsDir() {
 | 
				
			||||||
 | 
								return nil, errors.Wrapf(syscall.ENOTDIR, "failed to get context path %v", v)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							localName := k
 | 
				
			||||||
 | 
							if k == "context" || k == "dockerfile" {
 | 
				
			||||||
 | 
								localName = "_" + k // underscore to avoid collisions
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							target.LocalDirs[localName] = v
 | 
				
			||||||
 | 
							target.FrontendAttrs["context:"+k] = "local:" + localName
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	release := func() {
 | 
						release := func() {
 | 
				
			||||||
		for _, dir := range toRemove {
 | 
							for _, dir := range toRemove {
 | 
				
			||||||
			os.RemoveAll(dir)
 | 
								os.RemoveAll(dir)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@ import (
 | 
				
			|||||||
	"github.com/docker/cli/cli"
 | 
						"github.com/docker/cli/cli"
 | 
				
			||||||
	"github.com/docker/cli/cli/command"
 | 
						"github.com/docker/cli/cli/command"
 | 
				
			||||||
	dockeropts "github.com/docker/cli/opts"
 | 
						dockeropts "github.com/docker/cli/opts"
 | 
				
			||||||
 | 
						"github.com/docker/distribution/reference"
 | 
				
			||||||
	"github.com/docker/docker/pkg/ioutils"
 | 
						"github.com/docker/docker/pkg/ioutils"
 | 
				
			||||||
	"github.com/docker/go-units"
 | 
						"github.com/docker/go-units"
 | 
				
			||||||
	"github.com/moby/buildkit/client"
 | 
						"github.com/moby/buildkit/client"
 | 
				
			||||||
@@ -44,6 +45,7 @@ type buildOptions struct {
 | 
				
			|||||||
	cacheFrom    []string
 | 
						cacheFrom    []string
 | 
				
			||||||
	cacheTo      []string
 | 
						cacheTo      []string
 | 
				
			||||||
	cgroupParent string
 | 
						cgroupParent string
 | 
				
			||||||
 | 
						contexts     []string
 | 
				
			||||||
	extraHosts   []string
 | 
						extraHosts   []string
 | 
				
			||||||
	imageIDFile  string
 | 
						imageIDFile  string
 | 
				
			||||||
	labels       []string
 | 
						labels       []string
 | 
				
			||||||
@@ -100,11 +102,17 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
 | 
				
			|||||||
		in.progress = "quiet"
 | 
							in.progress = "quiet"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						contexts, err := parseContextNames(in.contexts)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	opts := build.Options{
 | 
						opts := build.Options{
 | 
				
			||||||
		Inputs: build.Inputs{
 | 
							Inputs: build.Inputs{
 | 
				
			||||||
			ContextPath:    in.contextPath,
 | 
								ContextPath:    in.contextPath,
 | 
				
			||||||
			DockerfilePath: in.dockerfileName,
 | 
								DockerfilePath: in.dockerfileName,
 | 
				
			||||||
			InStream:       os.Stdin,
 | 
								InStream:       os.Stdin,
 | 
				
			||||||
 | 
								NamedContexts:  contexts,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		BuildArgs:   listToMap(in.buildArgs, true),
 | 
							BuildArgs:   listToMap(in.buildArgs, true),
 | 
				
			||||||
		ExtraHosts:  in.extraHosts,
 | 
							ExtraHosts:  in.extraHosts,
 | 
				
			||||||
@@ -339,6 +347,8 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
 | 
				
			|||||||
	flags.StringVar(&options.cgroupParent, "cgroup-parent", "", "Optional parent cgroup for the container")
 | 
						flags.StringVar(&options.cgroupParent, "cgroup-parent", "", "Optional parent cgroup for the container")
 | 
				
			||||||
	flags.SetAnnotation("cgroup-parent", annotation.ExternalURL, []string{"https://docs.docker.com/engine/reference/commandline/build/#use-a-custom-parent-cgroup---cgroup-parent"})
 | 
						flags.SetAnnotation("cgroup-parent", annotation.ExternalURL, []string{"https://docs.docker.com/engine/reference/commandline/build/#use-a-custom-parent-cgroup---cgroup-parent"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						flags.StringArrayVar(&options.contexts, "context", []string{}, "Additional named contexts (e.g., name=path)")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flags.StringVarP(&options.dockerfileName, "file", "f", "", `Name of the Dockerfile (default: "PATH/Dockerfile")`)
 | 
						flags.StringVarP(&options.dockerfileName, "file", "f", "", `Name of the Dockerfile (default: "PATH/Dockerfile")`)
 | 
				
			||||||
	flags.SetAnnotation("file", annotation.ExternalURL, []string{"https://docs.docker.com/engine/reference/commandline/build/#specify-a-dockerfile--f"})
 | 
						flags.SetAnnotation("file", annotation.ExternalURL, []string{"https://docs.docker.com/engine/reference/commandline/build/#specify-a-dockerfile--f"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -462,3 +472,23 @@ func listToMap(values []string, defaultEnv bool) map[string]string {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return result
 | 
						return result
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func parseContextNames(values []string) (map[string]string, error) {
 | 
				
			||||||
 | 
						if len(values) == 0 {
 | 
				
			||||||
 | 
							return nil, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						result := make(map[string]string, len(values))
 | 
				
			||||||
 | 
						for _, value := range values {
 | 
				
			||||||
 | 
							kv := strings.SplitN(value, "=", 2)
 | 
				
			||||||
 | 
							if len(kv) != 2 {
 | 
				
			||||||
 | 
								return nil, errors.Errorf("invalid context value: %s, expected key=value", value)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							named, err := reference.ParseNormalizedNamed(kv[0])
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, errors.Wrapf(err, "invalid context name %s", kv[0])
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							name := strings.TrimSuffix(reference.FamiliarString(named), ":latest")
 | 
				
			||||||
 | 
							result[name] = kv[1]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return result, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user