Merge pull request #1710 from jedevc/use-buildkit-gitutil-parsegitref

Use buildkit's gitutil package to detect remote git files
pull/1713/head
Justin Chadwell 2 years ago committed by GitHub
commit 16e41ba297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,6 @@ import (
"github.com/docker/buildx/util/buildflags" "github.com/docker/buildx/util/buildflags"
"github.com/docker/buildx/util/platformutil" "github.com/docker/buildx/util/platformutil"
"github.com/docker/cli/cli/config" "github.com/docker/cli/cli/config"
"github.com/docker/docker/builder/remotecontext/urlutil"
hcl "github.com/hashicorp/hcl/v2" hcl "github.com/hashicorp/hcl/v2"
"github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/session/auth/authprovider" "github.com/moby/buildkit/session/auth/authprovider"
@ -790,7 +789,7 @@ func updateContext(t *build.Inputs, inp *Input) {
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") { if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
continue continue
} }
if IsRemoteURL(v.Path) { if build.IsRemoteURL(v.Path) {
continue continue
} }
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path)) st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
@ -804,7 +803,7 @@ func updateContext(t *build.Inputs, inp *Input) {
if strings.HasPrefix(t.ContextPath, "cwd://") { if strings.HasPrefix(t.ContextPath, "cwd://") {
return return
} }
if IsRemoteURL(t.ContextPath) { if build.IsRemoteURL(t.ContextPath) {
return return
} }
st := llb.Scratch().File(llb.Copy(*inp.State, t.ContextPath, "/"), llb.WithCustomNamef("set context to %s", t.ContextPath)) st := llb.Scratch().File(llb.Copy(*inp.State, t.ContextPath, "/"), llb.WithCustomNamef("set context to %s", t.ContextPath))
@ -840,7 +839,7 @@ func validateContextsEntitlements(t build.Inputs, inp *Input) error {
} }
func checkPath(p string) error { func checkPath(p string) error {
if IsRemoteURL(p) || strings.HasPrefix(p, "target:") || strings.HasPrefix(p, "docker-image:") { if build.IsRemoteURL(p) || strings.HasPrefix(p, "target:") || strings.HasPrefix(p, "docker-image:") {
return nil return nil
} }
p, err := filepath.EvalSymlinks(p) p, err := filepath.EvalSymlinks(p)
@ -876,7 +875,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
if t.Context != nil { if t.Context != nil {
contextPath = *t.Context contextPath = *t.Context
} }
if !strings.HasPrefix(contextPath, "cwd://") && !IsRemoteURL(contextPath) { if !strings.HasPrefix(contextPath, "cwd://") && !build.IsRemoteURL(contextPath) {
contextPath = path.Clean(contextPath) contextPath = path.Clean(contextPath)
} }
dockerfilePath := "Dockerfile" dockerfilePath := "Dockerfile"
@ -884,7 +883,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
dockerfilePath = *t.Dockerfile dockerfilePath = *t.Dockerfile
} }
if !isRemoteResource(contextPath) && !path.IsAbs(dockerfilePath) { if !build.IsRemoteURL(contextPath) && !path.IsAbs(dockerfilePath) {
dockerfilePath = path.Join(contextPath, dockerfilePath) dockerfilePath = path.Join(contextPath, dockerfilePath)
} }
@ -1040,10 +1039,6 @@ func removeDupes(s []string) []string {
return s[:i] return s[:i]
} }
func isRemoteResource(str string) bool {
return urlutil.IsGitURL(str) || urlutil.IsURL(str)
}
func parseOutputType(str string) string { func parseOutputType(str string) string {
csvReader := csv.NewReader(strings.NewReader(str)) csvReader := csv.NewReader(strings.NewReader(str))
fields, err := csvReader.Read() fields, err := csvReader.Read()

@ -83,16 +83,6 @@ func ReadRemoteFiles(ctx context.Context, nodes []builder.Node, url string, name
return files, inp, nil return files, inp, nil
} }
func IsRemoteURL(url string) bool {
if _, _, ok := detectHTTPContext(url); ok {
return true
}
if _, ok := detectGitContext(url); ok {
return true
}
return false
}
func detectHTTPContext(url string) (*llb.State, string, bool) { func detectHTTPContext(url string) (*llb.State, string, bool) {
if httpPrefix.MatchString(url) { if httpPrefix.MatchString(url) {
httpContext := llb.HTTP(url, llb.Filename("context"), llb.WithCustomName("[internal] load remote build context")) httpContext := llb.HTTP(url, llb.Filename("context"), llb.WithCustomName("[internal] load remote build context"))

@ -1278,7 +1278,6 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
target.LocalDirs["context"] = inp.ContextPath target.LocalDirs["context"] = inp.ContextPath
} }
} }
case isLocalDir(inp.ContextPath): case isLocalDir(inp.ContextPath):
target.LocalDirs["context"] = inp.ContextPath target.LocalDirs["context"] = inp.ContextPath
switch inp.DockerfilePath { switch inp.DockerfilePath {
@ -1290,8 +1289,7 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
dockerfileDir = filepath.Dir(inp.DockerfilePath) dockerfileDir = filepath.Dir(inp.DockerfilePath)
dockerfileName = filepath.Base(inp.DockerfilePath) dockerfileName = filepath.Base(inp.DockerfilePath)
} }
case IsRemoteURL(inp.ContextPath):
case urlutil.IsGitURL(inp.ContextPath), urlutil.IsURL(inp.ContextPath):
if inp.DockerfilePath == "-" { if inp.DockerfilePath == "-" {
dockerfileReader = inp.InStream dockerfileReader = inp.InStream
} }
@ -1346,7 +1344,7 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
continue continue
} }
if urlutil.IsGitURL(v.Path) || urlutil.IsURL(v.Path) || strings.HasPrefix(v.Path, "docker-image://") || strings.HasPrefix(v.Path, "target:") { if IsRemoteURL(v.Path) || strings.HasPrefix(v.Path, "docker-image://") || strings.HasPrefix(v.Path, "target:") {
target.FrontendAttrs["context:"+k] = v.Path target.FrontendAttrs["context:"+k] = v.Path
continue continue
} }

@ -8,6 +8,8 @@ import (
"strings" "strings"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/builder/remotecontext/urlutil"
"github.com/moby/buildkit/util/gitutil"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -20,6 +22,16 @@ const (
mobyHostGatewayName = "host-gateway" mobyHostGatewayName = "host-gateway"
) )
func IsRemoteURL(c string) bool {
if urlutil.IsURL(c) {
return true
}
if _, err := gitutil.ParseGitRef(c); err == nil {
return true
}
return false
}
func isLocalDir(c string) bool { func isLocalDir(c string) bool {
st, err := os.Stat(c) st, err := os.Stat(c)
return err == nil && st.IsDir() return err == nil && st.IsDir()

@ -47,11 +47,11 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
cmdContext := "cwd://" cmdContext := "cwd://"
if len(targets) > 0 { if len(targets) > 0 {
if bake.IsRemoteURL(targets[0]) { if build.IsRemoteURL(targets[0]) {
url = targets[0] url = targets[0]
targets = targets[1:] targets = targets[1:]
if len(targets) > 0 { if len(targets) > 0 {
if bake.IsRemoteURL(targets[0]) { if build.IsRemoteURL(targets[0]) {
cmdContext = targets[0] cmdContext = targets[0]
targets = targets[1:] targets = targets[1:]
} }

@ -14,6 +14,7 @@ import (
"strings" "strings"
"github.com/containerd/console" "github.com/containerd/console"
"github.com/docker/buildx/build"
"github.com/docker/buildx/controller" "github.com/docker/buildx/controller"
cbuild "github.com/docker/buildx/controller/build" cbuild "github.com/docker/buildx/controller/build"
"github.com/docker/buildx/controller/control" "github.com/docker/buildx/controller/control"
@ -29,7 +30,6 @@ 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/docker/builder/remotecontext/urlutil"
"github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/ioutils"
"github.com/moby/buildkit/client" "github.com/moby/buildkit/client"
"github.com/moby/buildkit/exporter/containerimage/exptypes" "github.com/moby/buildkit/exporter/containerimage/exptypes"
@ -670,7 +670,7 @@ func dockerUlimitToControllerUlimit(u *dockeropts.UlimitOpt) *controllerapi.Ulim
// and replaces them to absolute paths. // and replaces them to absolute paths.
func resolvePaths(options *controllerapi.BuildOptions) (_ *controllerapi.BuildOptions, err error) { func resolvePaths(options *controllerapi.BuildOptions) (_ *controllerapi.BuildOptions, err error) {
if options.ContextPath != "" && options.ContextPath != "-" { if options.ContextPath != "" && options.ContextPath != "-" {
if !urlutil.IsGitURL(options.ContextPath) && !urlutil.IsURL(options.ContextPath) { if !build.IsRemoteURL(options.ContextPath) {
options.ContextPath, err = filepath.Abs(options.ContextPath) options.ContextPath, err = filepath.Abs(options.ContextPath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -685,7 +685,7 @@ func resolvePaths(options *controllerapi.BuildOptions) (_ *controllerapi.BuildOp
} }
var contexts map[string]string var contexts map[string]string
for k, v := range options.NamedContexts { for k, v := range options.NamedContexts {
if urlutil.IsGitURL(v) || urlutil.IsURL(v) || strings.HasPrefix(v, "docker-image://") { if build.IsRemoteURL(v) || strings.HasPrefix(v, "docker-image://") {
// url prefix, this is a remote path // url prefix, this is a remote path
} else if strings.HasPrefix(v, "oci-layout://") { } else if strings.HasPrefix(v, "oci-layout://") {
// oci layout prefix, this is a local path // oci layout prefix, this is a local path

Loading…
Cancel
Save