@@ -18,18 +18,22 @@ package compose
18
18
19
19
import (
20
20
"context"
21
+ "crypto/sha1"
22
+ "fmt"
21
23
"os"
22
24
"path/filepath"
23
25
24
26
_ "github.com/docker/buildx/driver/docker" //nolint:blank-imports
25
27
_ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
26
28
_ "github.com/docker/buildx/driver/kubernetes" //nolint:blank-imports
27
29
_ "github.com/docker/buildx/driver/remote" //nolint:blank-imports
30
+ "github.com/moby/buildkit/client"
28
31
29
32
"github.com/docker/buildx/build"
30
33
"github.com/docker/buildx/builder"
31
34
"github.com/docker/buildx/util/dockerutil"
32
35
xprogress "github.com/docker/buildx/util/progress"
36
+ "github.com/docker/compose/v2/pkg/progress"
33
37
)
34
38
35
39
func (s * composeService ) doBuildBuildkit (ctx context.Context , opts map [string ]build.Options , mode string ) (map [string ]string , error ) {
@@ -43,23 +47,27 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]bu
43
47
return nil , err
44
48
}
45
49
46
- // Progress needs its own context that lives longer than the
47
- // build one otherwise it won't read all the messages from
48
- // build and will lock
49
- progressCtx , cancel := context .WithCancel (context .Background ())
50
- defer cancel ()
51
- w , err := xprogress .NewPrinter (progressCtx , s .stdout (), os .Stdout , mode )
52
- if err != nil {
53
- return nil , err
54
- }
55
-
56
- response , err := build .Build (ctx , nodes , opts , dockerutil .NewClient (s .dockerCli ), filepath .Dir (s .configFile ().Filename ), w )
57
- errW := w .Wait ()
58
- if err == nil {
59
- err = errW
60
- }
61
- if err != nil {
62
- return nil , WrapCategorisedComposeError (err , BuildFailure )
50
+ var response map [string ]* client.SolveResponse
51
+ if s .dryRun {
52
+ response = s .dryRunBuildResponse (ctx , opts )
53
+ } else {
54
+ // Progress needs its own context that lives longer than the
55
+ // build one otherwise it won't read all the messages from
56
+ // build and will lock
57
+ progressCtx , cancel := context .WithCancel (context .Background ())
58
+ defer cancel ()
59
+ w , err := xprogress .NewPrinter (progressCtx , s .stdout (), os .Stdout , mode )
60
+ if err != nil {
61
+ return nil , err
62
+ }
63
+ response , err = build .Build (ctx , nodes , opts , dockerutil .NewClient (s .dockerCli ), filepath .Dir (s .configFile ().Filename ), w )
64
+ errW := w .Wait ()
65
+ if err == nil {
66
+ err = errW
67
+ }
68
+ if err != nil {
69
+ return nil , WrapCategorisedComposeError (err , BuildFailure )
70
+ }
63
71
}
64
72
65
73
imagesBuilt := map [string ]string {}
@@ -76,3 +84,30 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]bu
76
84
77
85
return imagesBuilt , err
78
86
}
87
+
88
+ func (s composeService ) dryRunBuildResponse (ctx context.Context , options map [string ]build.Options ) map [string ]* client.SolveResponse {
89
+ w := progress .ContextWriter (ctx )
90
+ buildResponse := map [string ]* client.SolveResponse {}
91
+ for name , option := range options {
92
+ dryRunUUID := fmt .Sprintf ("dryRun-%x" , sha1 .Sum ([]byte (name )))
93
+ w .Event (progress.Event {
94
+ ID : " " ,
95
+ Status : progress .Done ,
96
+ Text : fmt .Sprintf ("build service %s" , name ),
97
+ })
98
+ w .Event (progress.Event {
99
+ ID : "==>" ,
100
+ Status : progress .Done ,
101
+ Text : fmt .Sprintf ("==> writing image %s" , dryRunUUID ),
102
+ })
103
+ w .Event (progress.Event {
104
+ ID : "==> ==>" ,
105
+ Status : progress .Done ,
106
+ Text : fmt .Sprintf (`naming to %s` , option .Tags [0 ]),
107
+ })
108
+ buildResponse [name ] = & client.SolveResponse {ExporterResponse : map [string ]string {
109
+ "containerimage.digest" : dryRunUUID ,
110
+ }}
111
+ }
112
+ return buildResponse
113
+ }
0 commit comments