Skip to content

Commit eca1365

Browse files
authored
cli: dry run support for build (docker#10502)
* add dry-run support for classic builder * add dry-run support for buildkit Signed-off-by: Guillaume Lours <[email protected]>
1 parent 03f4c0e commit eca1365

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

pkg/api/dryrunclient.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,20 @@ func (d *DryRunClient) CopyToContainer(ctx context.Context, container, path stri
165165
}
166166

167167
func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options moby.ImageBuildOptions) (moby.ImageBuildResponse, error) {
168-
return moby.ImageBuildResponse{}, ErrNotImplemented
168+
jsonMessage, err := json.Marshal(&jsonmessage.JSONMessage{
169+
Status: fmt.Sprintf("%[1]sSuccessfully built: dryRunID\n%[1]sSuccessfully tagged: %[2]s\n", DRYRUN_PREFIX, options.Tags[0]),
170+
Progress: &jsonmessage.JSONProgress{},
171+
ID: "",
172+
})
173+
if err != nil {
174+
return moby.ImageBuildResponse{}, err
175+
}
176+
rc := io.NopCloser(bytes.NewReader(jsonMessage))
177+
178+
return moby.ImageBuildResponse{
179+
Body: rc,
180+
OSType: "",
181+
}, nil
169182
}
170183

171184
func (d *DryRunClient) ImageInspectWithRaw(ctx context.Context, imageName string) (moby.ImageInspect, []byte, error) {

pkg/compose/build.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
5353
}, s.stderr(), "Building")
5454
}
5555

56-
//nolint:gocyclo
5756
func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions) (map[string]string, error) {
5857
args := options.Args.Resolve(envResolver(project.Environment))
5958

@@ -75,16 +74,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
7574
return nil
7675
}
7776

78-
//TODO:glours - condition to be removed when dry-run support of build will be implemented.
79-
if s.dryRun {
80-
builder := "buildkit"
81-
if !buildkitEnabled {
82-
builder = "legacy builder"
83-
}
84-
fmt.Printf("%sBuilding image %s with %s\n", api.DRYRUN_PREFIX, service.Image, builder)
85-
return nil
86-
}
87-
8877
if !buildkitEnabled {
8978
if service.Build.Args == nil {
9079
service.Build.Args = args

pkg/compose/build_buildkit.go

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ package compose
1818

1919
import (
2020
"context"
21+
"crypto/sha1"
22+
"fmt"
2123
"os"
2224
"path/filepath"
2325

2426
_ "github.com/docker/buildx/driver/docker" //nolint:blank-imports
2527
_ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
2628
_ "github.com/docker/buildx/driver/kubernetes" //nolint:blank-imports
2729
_ "github.com/docker/buildx/driver/remote" //nolint:blank-imports
30+
"github.com/moby/buildkit/client"
2831

2932
"github.com/docker/buildx/build"
3033
"github.com/docker/buildx/builder"
3134
"github.com/docker/buildx/util/dockerutil"
3235
xprogress "github.com/docker/buildx/util/progress"
36+
"github.com/docker/compose/v2/pkg/progress"
3337
)
3438

3539
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
4347
return nil, err
4448
}
4549

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+
}
6371
}
6472

6573
imagesBuilt := map[string]string{}
@@ -76,3 +84,30 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]bu
7684

7785
return imagesBuilt, err
7886
}
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

Comments
 (0)