From 22e1901581bd039bea0d3499a9347e8742e2f2b9 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 9 May 2023 17:15:44 +0100 Subject: [PATCH 1/2] bake: reorganize input creation together Signed-off-by: Justin Chadwell --- bake/bake.go | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index d9145d9f..6168f155 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -1027,11 +1027,34 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { if t.Dockerfile != nil { dockerfilePath = *t.Dockerfile } - if !build.IsRemoteURL(contextPath) && !path.IsAbs(dockerfilePath) { dockerfilePath = path.Join(contextPath, dockerfilePath) } + bi := build.Inputs{ + ContextPath: contextPath, + DockerfilePath: dockerfilePath, + NamedContexts: toNamedContexts(t.Contexts), + } + if t.DockerfileInline != nil { + bi.DockerfileInline = *t.DockerfileInline + } + updateContext(&bi, inp) + if strings.HasPrefix(bi.ContextPath, "cwd://") { + bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")) + } + for k, v := range bi.NamedContexts { + if strings.HasPrefix(v.Path, "cwd://") { + bi.NamedContexts[k] = build.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))} + } + } + + if err := validateContextsEntitlements(bi, inp); err != nil { + return nil, err + } + + t.Context = &bi.ContextPath + args := map[string]string{} for k, v := range t.Args { if v == nil { @@ -1061,30 +1084,6 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { networkMode = *t.NetworkMode } - bi := build.Inputs{ - ContextPath: contextPath, - DockerfilePath: dockerfilePath, - NamedContexts: toNamedContexts(t.Contexts), - } - if t.DockerfileInline != nil { - bi.DockerfileInline = *t.DockerfileInline - } - updateContext(&bi, inp) - if strings.HasPrefix(bi.ContextPath, "cwd://") { - bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")) - } - for k, v := range bi.NamedContexts { - if strings.HasPrefix(v.Path, "cwd://") { - bi.NamedContexts[k] = build.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))} - } - } - - if err := validateContextsEntitlements(bi, inp); err != nil { - return nil, err - } - - t.Context = &bi.ContextPath - bo := &build.Options{ Inputs: bi, Tags: t.Tags, From 12b6a3ad9aeda4bdf67a6a55d22fc43594125ec4 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 9 May 2023 17:19:25 +0100 Subject: [PATCH 2/2] bake: copy remote context contents to root When resolving remote contexts locally in bake, then we need to ensure that we properly unpack the contents of that context to the root directory, instead of leaving it in the subdirectory. Otherwise, any files will be found in the wrong location. Along with this change, we also need a change to the dockerfile location lookup to ensure that it is found at the root instead of in the subdirectory. Signed-off-by: Justin Chadwell --- bake/bake.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index 6168f155..59956925 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -951,7 +951,12 @@ func updateContext(t *build.Inputs, inp *Input) { if build.IsRemoteURL(t.ContextPath) { 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.CopyInfo{ + CopyDirContentsOnly: true, + }), + llb.WithCustomNamef("set context to %s", t.ContextPath), + ) t.ContextState = &st } @@ -1027,9 +1032,6 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { if t.Dockerfile != nil { dockerfilePath = *t.Dockerfile } - if !build.IsRemoteURL(contextPath) && !path.IsAbs(dockerfilePath) { - dockerfilePath = path.Join(contextPath, dockerfilePath) - } bi := build.Inputs{ ContextPath: contextPath, @@ -1040,6 +1042,9 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { bi.DockerfileInline = *t.DockerfileInline } updateContext(&bi, inp) + if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !path.IsAbs(bi.DockerfilePath) { + bi.DockerfilePath = path.Join(bi.ContextPath, bi.DockerfilePath) + } if strings.HasPrefix(bi.ContextPath, "cwd://") { bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")) }