From 97530790fa8e5b9779331746cf4ce72508d6c60d Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 11 Jun 2025 12:01:53 +0200 Subject: [PATCH] only look for required image in bake metadata Signed-off-by: Nicolas De Loof --- pkg/compose/build_bake.go | 21 ++++++-------- pkg/e2e/build_test.go | 28 +++++++++++++++++++ .../build-test/dependencies/compose.yaml | 26 +++++++++++++++++ .../fixtures/build-test/subset/compose.yaml | 14 ++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 pkg/e2e/fixtures/build-test/dependencies/compose.yaml create mode 100644 pkg/e2e/fixtures/build-test/subset/compose.yaml diff --git a/pkg/compose/build_bake.go b/pkg/compose/build_bake.go index 4594e4b77..2280ed3ab 100644 --- a/pkg/compose/build_bake.go +++ b/pkg/compose/build_bake.go @@ -140,11 +140,10 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project Targets: map[string]bakeTarget{}, } var ( - group bakeGroup - privileged bool - read []string - expectedImages = make(map[string]string, len(serviceToBeBuild)) // service name -> expected image - targets = make(map[string]string, len(serviceToBeBuild)) // service name -> build target + group bakeGroup + privileged bool + read []string + targets = make(map[string]string, len(serviceToBeBuild)) // service name -> build target ) // produce a unique ID for service used as bake target @@ -173,9 +172,6 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project args[k] = *v } - image := api.GetImageNameOrDefault(service, project.Name) - expectedImages[serviceName] = image - entitlements := build.Entitlements if slices.Contains(build.Entitlements, "security.insecure") { privileged = true @@ -213,7 +209,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project DockerfileInline: strings.ReplaceAll(build.DockerfileInline, "${", "$${"), Args: args, Labels: build.Labels, - Tags: append(build.Tags, image), + Tags: append(build.Tags, api.GetImageNameOrDefault(service, project.Name)), CacheFrom: build.CacheFrom, // CacheTo: TODO @@ -360,10 +356,11 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project cw := progress.ContextWriter(ctx) results := map[string]string{} - for service, name := range expectedImages { - built, ok := md[targets[service]] + for name := range serviceToBeBuild { + target := targets[name] + built, ok := md[target] if !ok { - return nil, fmt.Errorf("build result not found in Bake metadata for service %s", service) + return nil, fmt.Errorf("build result not found in Bake metadata for service %s", name) } results[name] = built.Digest cw.Event(progress.BuiltEvent(name)) diff --git a/pkg/e2e/build_test.go b/pkg/e2e/build_test.go index afab806db..f87abbd1d 100644 --- a/pkg/e2e/build_test.go +++ b/pkg/e2e/build_test.go @@ -536,3 +536,31 @@ func TestBuildDependsOn(t *testing.T) { out := res.Combined() assert.Check(t, strings.Contains(out, "test1 Built")) } + +func TestBuildSubset(t *testing.T) { + c := NewParallelCLI(t) + + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/subset/compose.yaml", "down", "--rmi=local") + }) + + res := c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/subset/compose.yaml", "build", "main") + out := res.Combined() + assert.Check(t, strings.Contains(out, "main Built")) +} + +func TestBuildDependentImage(t *testing.T) { + c := NewParallelCLI(t) + + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/dependencies/compose.yaml", "down", "--rmi=local") + }) + + res := c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/dependencies/compose.yaml", "build", "firstbuild") + out := res.Combined() + assert.Check(t, strings.Contains(out, "firstbuild Built")) + + res = c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/dependencies/compose.yaml", "build", "secondbuild") + out = res.Combined() + assert.Check(t, strings.Contains(out, "secondbuild Built")) +} diff --git a/pkg/e2e/fixtures/build-test/dependencies/compose.yaml b/pkg/e2e/fixtures/build-test/dependencies/compose.yaml new file mode 100644 index 000000000..eb5de31f9 --- /dev/null +++ b/pkg/e2e/fixtures/build-test/dependencies/compose.yaml @@ -0,0 +1,26 @@ +services: + firstbuild: + build: + dockerfile_inline: | + FROM alpine + additional_contexts: + dep1: service:dep1 + entrypoint: ["echo", "Hello from firstbuild"] + depends_on: + - dep1 + + secondbuild: + build: + dockerfile_inline: | + FROM alpine + additional_contexts: + dep1: service:dep1 + entrypoint: ["echo", "Hello from secondbuild"] + depends_on: + - dep1 + + dep1: + build: + dockerfile_inline: | + FROM alpine + entrypoint: ["echo", "Hello from dep1"] \ No newline at end of file diff --git a/pkg/e2e/fixtures/build-test/subset/compose.yaml b/pkg/e2e/fixtures/build-test/subset/compose.yaml new file mode 100644 index 000000000..6bae0f126 --- /dev/null +++ b/pkg/e2e/fixtures/build-test/subset/compose.yaml @@ -0,0 +1,14 @@ +services: + main: + build: + dockerfile_inline: | + FROM alpine + entrypoint: ["echo", "Hello from main"] + depends_on: + - dep1 + + dep1: + build: + dockerfile_inline: | + FROM alpine + entrypoint: ["echo", "Hello from dep1"] \ No newline at end of file