From 7cf7c6414f5681ba0dbd596e15d9c4d64ddda41c Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 7 Jul 2025 15:01:15 +0200 Subject: [PATCH] build resolves enabled service after project has been loaded Signed-off-by: Nicolas De Loof --- cmd/compose/build.go | 1 + pkg/e2e/build_test.go | 12 ++++++++++++ .../fixtures/build-test/profiles/Dockerfile | 19 +++++++++++++++++++ .../fixtures/build-test/profiles/compose.yaml | 12 ++++++++++++ .../build-test/profiles/test-secret.txt | 1 + 5 files changed, 45 insertions(+) create mode 100644 pkg/e2e/fixtures/build-test/profiles/Dockerfile create mode 100644 pkg/e2e/fixtures/build-test/profiles/compose.yaml create mode 100644 pkg/e2e/fixtures/build-test/profiles/test-secret.txt diff --git a/cmd/compose/build.go b/cmd/compose/build.go index f33689338..e29a9d89e 100644 --- a/cmd/compose/build.go +++ b/cmd/compose/build.go @@ -145,6 +145,7 @@ func buildCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) } func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, opts buildOptions, services []string) error { + opts.All = true // do not drop resources as build may involve some dependencies by additional_contexts project, _, err := opts.ToProject(ctx, dockerCli, nil, cli.WithResolvedPaths(true), cli.WithoutEnvironmentResolution) if err != nil { return err diff --git a/pkg/e2e/build_test.go b/pkg/e2e/build_test.go index 0449a46e7..202e2d88d 100644 --- a/pkg/e2e/build_test.go +++ b/pkg/e2e/build_test.go @@ -596,3 +596,15 @@ func TestBuildLongOutputLine(t *testing.T) { out = res.Combined() assert.Check(t, strings.Contains(out, "long-line Built")) } + +func TestBuildDependentImageWithProfile(t *testing.T) { + c := NewParallelCLI(t) + + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/profiles/compose.yaml", "down", "--rmi=local") + }) + + res := c.RunDockerComposeCmd(t, "-f", "fixtures/build-test/profiles/compose.yaml", "build", "secret-build-test") + out := res.Combined() + assert.Check(t, strings.Contains(out, "secret-build-test Built")) +} diff --git a/pkg/e2e/fixtures/build-test/profiles/Dockerfile b/pkg/e2e/fixtures/build-test/profiles/Dockerfile new file mode 100644 index 000000000..94eb80e9c --- /dev/null +++ b/pkg/e2e/fixtures/build-test/profiles/Dockerfile @@ -0,0 +1,19 @@ +# Copyright 2020 Docker Compose CLI authors + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +FROM alpine +RUN --mount=type=secret,id=test-secret ls -la /run/secrets/; cp /run/secrets/test-secret /tmp + +CMD ["cat", "/tmp/test-secret"] \ No newline at end of file diff --git a/pkg/e2e/fixtures/build-test/profiles/compose.yaml b/pkg/e2e/fixtures/build-test/profiles/compose.yaml new file mode 100644 index 000000000..877babc0e --- /dev/null +++ b/pkg/e2e/fixtures/build-test/profiles/compose.yaml @@ -0,0 +1,12 @@ +secrets: + test-secret: + file: test-secret.txt + +services: + secret-build-test: + profiles: ["test"] + build: + context: . + dockerfile: Dockerfile + secrets: + - test-secret \ No newline at end of file diff --git a/pkg/e2e/fixtures/build-test/profiles/test-secret.txt b/pkg/e2e/fixtures/build-test/profiles/test-secret.txt new file mode 100644 index 000000000..788821219 --- /dev/null +++ b/pkg/e2e/fixtures/build-test/profiles/test-secret.txt @@ -0,0 +1 @@ +SECRET \ No newline at end of file