Skip to content

Commit 00cc36c

Browse files
authored
Merge pull request #3678 from tonistiigi/bake-subdir-remote-fix
bake: fix remote named context subdir handling
2 parents 8f02b16 + d39a81e commit 00cc36c

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

bake/bake.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,14 +1299,17 @@ func updateContext(t *build.Inputs, inp *Input) {
12991299
for k, v := range t.NamedContexts {
13001300
if v.Path == "." {
13011301
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
1302+
continue
13021303
}
13031304
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
13041305
continue
13051306
}
13061307
if urlutil.IsRemoteURL(v.Path) {
13071308
continue
13081309
}
1309-
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
1310+
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/", &llb.CopyInfo{
1311+
CopyDirContentsOnly: true,
1312+
}), llb.WithCustomNamef("set context %s to %s", k, v.Path))
13101313
t.NamedContexts[k] = build.NamedContext{State: &st, Path: inp.URL}
13111314
}
13121315

tests/bake.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
5151
testBakeLocalCwdOverride,
5252
testBakeRemoteCmdContextOverride,
5353
testBakeRemoteContextSubdir,
54+
testBakeRemoteNamedContextSubdir,
55+
testBakeRemoteNamedContextDot,
5456
testBakeRemoteCmdContextEscapeRoot,
5557
testBakeRemoteCmdContextEscapeRelative,
5658
testBakeRemoteDockerfileCwd,
@@ -920,6 +922,80 @@ COPY super-cool.txt /
920922
require.FileExists(t, filepath.Join(dirDest, "super-cool.txt"))
921923
}
922924

925+
// https://github.com/docker/buildx/issues/3670
926+
func testBakeRemoteNamedContextSubdir(t *testing.T, sb integration.Sandbox) {
927+
bakefile := []byte(`
928+
target default {
929+
context = "./build"
930+
dockerfile = "Dockerfile"
931+
contexts = {
932+
files = "./files-src/"
933+
}
934+
}
935+
`)
936+
dockerfile := []byte(`
937+
FROM scratch
938+
COPY --from=files file.txt /file.txt
939+
`)
940+
941+
dir := tmpdir(
942+
t,
943+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
944+
fstest.CreateDir("build", 0700),
945+
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
946+
fstest.CreateDir("files-src", 0700),
947+
fstest.CreateFile("files-src/file.txt", []byte("hello"), 0600),
948+
)
949+
dirDest := t.TempDir()
950+
951+
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
952+
require.NoError(t, err)
953+
gittestutil.GitInit(git, t)
954+
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "files-src")
955+
gittestutil.GitCommit(git, t, "initial commit")
956+
addr := gittestutil.GitServeHTTP(git, t)
957+
958+
out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
959+
require.NoError(t, err, out)
960+
require.FileExists(t, filepath.Join(dirDest, "file.txt"))
961+
}
962+
963+
func testBakeRemoteNamedContextDot(t *testing.T, sb integration.Sandbox) {
964+
bakefile := []byte(`
965+
target default {
966+
context = "./build"
967+
dockerfile = "Dockerfile"
968+
contexts = {
969+
files = "."
970+
}
971+
}
972+
`)
973+
dockerfile := []byte(`
974+
FROM scratch
975+
COPY --from=files marker.txt /marker.txt
976+
`)
977+
978+
dir := tmpdir(
979+
t,
980+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
981+
fstest.CreateDir("build", 0700),
982+
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
983+
fstest.CreateFile("marker.txt", []byte("hello"), 0600),
984+
)
985+
dirDest := t.TempDir()
986+
987+
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
988+
require.NoError(t, err)
989+
gittestutil.GitInit(git, t)
990+
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "marker.txt")
991+
gittestutil.GitCommit(git, t, "initial commit")
992+
addr := gittestutil.GitServeHTTP(git, t)
993+
994+
out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
995+
require.NoError(t, err, out)
996+
require.FileExists(t, filepath.Join(dirDest, "marker.txt"))
997+
}
998+
923999
func testBakeRemoteCmdContextEscapeRoot(t *testing.T, sb integration.Sandbox) {
9241000
dirSrc := tmpdir(
9251001
t,

0 commit comments

Comments
 (0)