Skip to content

Commit f557220

Browse files
gloursndeloof
authored andcommitted
identify services to build and don't display 'building' if none
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 8e1b323 commit f557220

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

pkg/compose/build.go

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"path/filepath"
25+
"sync"
2526

2627
"github.com/compose-spec/compose-go/types"
2728
"github.com/containerd/containerd/platforms"
@@ -33,6 +34,10 @@ import (
3334
xprogress "github.com/docker/buildx/util/progress"
3435
"github.com/docker/cli/cli/command"
3536
cliopts "github.com/docker/cli/opts"
37+
"github.com/docker/compose/v2/internal/tracing"
38+
"github.com/docker/compose/v2/pkg/api"
39+
"github.com/docker/compose/v2/pkg/progress"
40+
"github.com/docker/compose/v2/pkg/utils"
3641
"github.com/docker/docker/builder/remotecontext/urlutil"
3742
"github.com/docker/go-units"
3843
bclient "github.com/moby/buildkit/client"
@@ -44,11 +49,6 @@ import (
4449
specs "github.com/opencontainers/image-spec/specs-go/v1"
4550
"github.com/sirupsen/logrus"
4651

47-
"github.com/docker/compose/v2/internal/tracing"
48-
"github.com/docker/compose/v2/pkg/api"
49-
"github.com/docker/compose/v2/pkg/progress"
50-
"github.com/docker/compose/v2/pkg/utils"
51-
5252
// required to get default driver registered
5353
_ "github.com/docker/buildx/driver/docker"
5454
)
@@ -64,13 +64,48 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
6464
}, s.stdinfo(), "Building")
6565
}
6666

67+
type serviceToBuild struct {
68+
idx int
69+
service types.ServiceConfig
70+
}
71+
6772
//nolint:gocyclo
6873
func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions, localImages map[string]string) (map[string]string, error) {
6974
buildkitEnabled, err := s.dockerCli.BuildKitEnabled()
7075
if err != nil {
7176
return nil, err
7277
}
7378

79+
imageIDs := map[string]string{}
80+
serviceToBeBuild := map[string]serviceToBuild{}
81+
mapServiceMutx := sync.Mutex{}
82+
err = InDependencyOrder(ctx, project, func(ctx context.Context, name string) error {
83+
if len(options.Services) > 0 && !utils.Contains(options.Services, name) {
84+
return nil
85+
}
86+
service, idx := getServiceIndex(project, name)
87+
88+
if service.Build == nil {
89+
return nil
90+
}
91+
92+
image := api.GetImageNameOrDefault(service, project.Name)
93+
_, localImagePresent := localImages[image]
94+
if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
95+
return nil
96+
}
97+
mapServiceMutx.Lock()
98+
serviceToBeBuild[name] = serviceToBuild{idx: idx, service: service}
99+
mapServiceMutx.Unlock()
100+
return nil
101+
}, func(traversal *graphTraversal) {
102+
traversal.maxConcurrency = s.maxConcurrency
103+
})
104+
105+
if err != nil || len(serviceToBeBuild) == 0 {
106+
return imageIDs, err
107+
}
108+
74109
// Initialize buildkit nodes
75110
var (
76111
b *builder.Builder
@@ -114,17 +149,12 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
114149
if len(options.Services) > 0 && !utils.Contains(options.Services, name) {
115150
return nil
116151
}
117-
service, idx := getServiceIndex(project, name)
118-
119-
if service.Build == nil {
120-
return nil
121-
}
122-
123-
image := api.GetImageNameOrDefault(service, project.Name)
124-
_, localImagePresent := localImages[image]
125-
if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
152+
serviceToBuild, ok := serviceToBeBuild[name]
153+
if !ok {
126154
return nil
127155
}
156+
service := serviceToBuild.service
157+
idx := serviceToBuild.idx
128158

129159
if !buildkitEnabled {
130160
id, err := s.doBuildClassic(ctx, project, service, options)
@@ -170,7 +200,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
170200
return nil, err
171201
}
172202

173-
imageIDs := map[string]string{}
174203
for i, imageDigest := range builtDigests {
175204
if imageDigest != "" {
176205
imageRef := api.GetImageNameOrDefault(project.Services[i], project.Name)

0 commit comments

Comments
 (0)