Skip to content

Commit 0badcf3

Browse files
ndeloofglours
authored andcommitted
include implicit build dependencies in build command
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent ec49db9 commit 0badcf3

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

cmd/compose/build.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/docker/cli/cli/command"
2828
cliopts "github.com/docker/cli/opts"
2929
ui "github.com/docker/compose/v2/pkg/progress"
30+
"github.com/docker/compose/v2/pkg/utils"
3031
buildkit "github.com/moby/buildkit/util/progress/progressui"
3132
"github.com/spf13/cobra"
3233

@@ -145,6 +146,8 @@ func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, o
145146
return err
146147
}
147148

149+
services = addBuildDependencies(services, project)
150+
148151
if err := applyPlatforms(project, false); err != nil {
149152
return err
150153
}
@@ -156,3 +159,21 @@ func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, o
156159

157160
return backend.Build(ctx, project, apiBuildOptions)
158161
}
162+
163+
func addBuildDependencies(services []string, project *types.Project) []string {
164+
servicesWithDependencies := utils.NewSet(services...)
165+
for _, service := range services {
166+
build := project.Services[service].Build
167+
if build != nil {
168+
for _, target := range build.AdditionalContexts {
169+
if s, found := strings.CutPrefix(target, types.ServicePrefix); found {
170+
servicesWithDependencies.Add(s)
171+
}
172+
}
173+
}
174+
}
175+
if len(servicesWithDependencies) > len(services) {
176+
return addBuildDependencies(servicesWithDependencies.Elements(), project)
177+
}
178+
return servicesWithDependencies.Elements()
179+
}

cmd/compose/build_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"slices"
21+
"testing"
22+
23+
"github.com/compose-spec/compose-go/v2/types"
24+
"gotest.tools/v3/assert"
25+
)
26+
27+
func Test_addBuildDependencies(t *testing.T) {
28+
project := &types.Project{Services: types.Services{
29+
"test": types.ServiceConfig{
30+
Build: &types.BuildConfig{
31+
AdditionalContexts: map[string]string{
32+
"foo": "service:foo",
33+
"bar": "service:bar",
34+
},
35+
},
36+
},
37+
"foo": types.ServiceConfig{
38+
Build: &types.BuildConfig{
39+
AdditionalContexts: map[string]string{
40+
"zot": "service:zot",
41+
},
42+
},
43+
},
44+
"bar": types.ServiceConfig{
45+
Build: &types.BuildConfig{},
46+
},
47+
"zot": types.ServiceConfig{
48+
Build: &types.BuildConfig{},
49+
},
50+
}}
51+
52+
services := addBuildDependencies([]string{"test"}, project)
53+
expected := []string{"test", "foo", "bar", "zot"}
54+
slices.Sort(services)
55+
slices.Sort(expected)
56+
assert.DeepEqual(t, services, expected)
57+
}

0 commit comments

Comments
 (0)