Skip to content

Commit dba32a2

Browse files
committed
only look for required image in bake metadata
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 213c03f commit dba32a2

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

pkg/compose/build_bake.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
140140
Targets: map[string]bakeTarget{},
141141
}
142142
var (
143-
group bakeGroup
144-
privileged bool
145-
read []string
146-
expectedImages = make(map[string]string, len(serviceToBeBuild)) // service name -> expected image
147-
targets = make(map[string]string, len(serviceToBeBuild)) // service name -> build target
143+
group bakeGroup
144+
privileged bool
145+
read []string
146+
targets = make(map[string]string, len(serviceToBeBuild)) // service name -> build target
148147
)
149148

150149
// produce a unique ID for service used as bake target
@@ -173,9 +172,6 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
173172
args[k] = *v
174173
}
175174

176-
image := api.GetImageNameOrDefault(service, project.Name)
177-
expectedImages[serviceName] = image
178-
179175
entitlements := build.Entitlements
180176
if slices.Contains(build.Entitlements, "security.insecure") {
181177
privileged = true
@@ -360,10 +356,11 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
360356

361357
cw := progress.ContextWriter(ctx)
362358
results := map[string]string{}
363-
for service, name := range expectedImages {
364-
built, ok := md[targets[service]]
359+
for name := range serviceToBeBuild {
360+
target := targets[name]
361+
built, ok := md[target]
365362
if !ok {
366-
return nil, fmt.Errorf("build result not found in Bake metadata for service %s", service)
363+
return nil, fmt.Errorf("build result not found in Bake metadata for service %s", name)
367364
}
368365
results[name] = built.Digest
369366
cw.Event(progress.BuiltEvent(name))

pkg/e2e/build_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,31 @@ func TestBuildDependsOn(t *testing.T) {
536536
out := res.Combined()
537537
assert.Check(t, strings.Contains(out, "test1 Built"))
538538
}
539+
540+
func TestBuildSubset(t *testing.T) {
541+
c := NewParallelCLI(t)
542+
543+
t.Cleanup(func() {
544+
c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/subset/compose.yaml", "down", "--rmi=local")
545+
})
546+
547+
res := c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/subset/compose.yaml", "build", "main")
548+
out := res.Combined()
549+
assert.Check(t, strings.Contains(out, "main Built"))
550+
}
551+
552+
func TestBuildDependentImage(t *testing.T) {
553+
c := NewParallelCLI(t)
554+
555+
t.Cleanup(func() {
556+
c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/dependencies/compose.yaml", "down", "--rmi=local")
557+
})
558+
559+
res := c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/dependencies/compose.yaml", "build", "firstbuild")
560+
out := res.Combined()
561+
assert.Check(t, strings.Contains(out, "firstbuild Built"))
562+
563+
res = c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/dependencies/compose.yaml", "build", "secondbuild")
564+
out = res.Combined()
565+
assert.Check(t, strings.Contains(out, "secondbuild Built"))
566+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
services:
2+
firstbuild:
3+
build:
4+
dockerfile_inline: |
5+
FROM alpine
6+
additional_contexts:
7+
dep1: service:dep1
8+
entrypoint: ["echo", "Hello from firstbuild"]
9+
depends_on:
10+
- dep1
11+
12+
secondbuild:
13+
build:
14+
dockerfile_inline: |
15+
FROM alpine
16+
additional_contexts:
17+
dep1: service:dep1
18+
entrypoint: ["echo", "Hello from secondbuild"]
19+
depends_on:
20+
- dep1
21+
22+
dep1:
23+
build:
24+
dockerfile_inline: |
25+
FROM alpine
26+
entrypoint: ["echo", "Hello from dep1"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
main:
3+
build:
4+
dockerfile_inline: |
5+
FROM alpine
6+
entrypoint: ["echo", "Hello from main"]
7+
depends_on:
8+
- dep1
9+
10+
dep1:
11+
build:
12+
dockerfile_inline: |
13+
FROM alpine
14+
entrypoint: ["echo", "Hello from dep1"]

0 commit comments

Comments
 (0)