Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1299,14 +1299,17 @@ func updateContext(t *build.Inputs, inp *Input) {
for k, v := range t.NamedContexts {
if v.Path == "." {
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
continue
}
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
continue
}
if urlutil.IsRemoteURL(v.Path) {
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.CopyInfo{
CopyDirContentsOnly: true,
}), llb.WithCustomNamef("set context %s to %s", k, v.Path))
t.NamedContexts[k] = build.NamedContext{State: &st, Path: inp.URL}
}

Expand Down
76 changes: 76 additions & 0 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
testBakeLocalCwdOverride,
testBakeRemoteCmdContextOverride,
testBakeRemoteContextSubdir,
testBakeRemoteNamedContextSubdir,
testBakeRemoteNamedContextDot,
testBakeRemoteCmdContextEscapeRoot,
testBakeRemoteCmdContextEscapeRelative,
testBakeRemoteDockerfileCwd,
Expand Down Expand Up @@ -920,6 +922,80 @@ COPY super-cool.txt /
require.FileExists(t, filepath.Join(dirDest, "super-cool.txt"))
}

// https://github.com/docker/buildx/issues/3670
func testBakeRemoteNamedContextSubdir(t *testing.T, sb integration.Sandbox) {
bakefile := []byte(`
target default {
context = "./build"
dockerfile = "Dockerfile"
contexts = {
files = "./files-src/"
}
}
`)
dockerfile := []byte(`
FROM scratch
COPY --from=files file.txt /file.txt
`)

dir := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
fstest.CreateDir("build", 0700),
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
fstest.CreateDir("files-src", 0700),
fstest.CreateFile("files-src/file.txt", []byte("hello"), 0600),
)
dirDest := t.TempDir()

git, err := gitutil.New(gitutil.WithWorkingDir(dir))
require.NoError(t, err)
gittestutil.GitInit(git, t)
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "files-src")
gittestutil.GitCommit(git, t, "initial commit")
addr := gittestutil.GitServeHTTP(git, t)

out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
require.NoError(t, err, out)
require.FileExists(t, filepath.Join(dirDest, "file.txt"))
}

func testBakeRemoteNamedContextDot(t *testing.T, sb integration.Sandbox) {
bakefile := []byte(`
target default {
context = "./build"
dockerfile = "Dockerfile"
contexts = {
files = "."
}
}
`)
dockerfile := []byte(`
FROM scratch
COPY --from=files marker.txt /marker.txt
`)

dir := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
fstest.CreateDir("build", 0700),
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
fstest.CreateFile("marker.txt", []byte("hello"), 0600),
)
dirDest := t.TempDir()

git, err := gitutil.New(gitutil.WithWorkingDir(dir))
require.NoError(t, err)
gittestutil.GitInit(git, t)
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "marker.txt")
gittestutil.GitCommit(git, t, "initial commit")
addr := gittestutil.GitServeHTTP(git, t)

out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
require.NoError(t, err, out)
require.FileExists(t, filepath.Join(dirDest, "marker.txt"))
}

func testBakeRemoteCmdContextEscapeRoot(t *testing.T, sb integration.Sandbox) {
dirSrc := tmpdir(
t,
Expand Down
Loading