Skip to content

Commit 8e38397

Browse files
committed
introduce build --check
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 2dbef23 commit 8e38397

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

cmd/compose/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type buildOptions struct {
4545
builder string
4646
deps bool
4747
print bool
48+
check bool
4849
}
4950

5051
func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
@@ -79,6 +80,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
7980
Deps: opts.deps,
8081
Memory: int64(opts.memory),
8182
Print: opts.print,
83+
Check: opts.check,
8284
SSHs: SSHKeys,
8385
Builder: builderName,
8486
}, nil
@@ -135,6 +137,7 @@ func buildCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
135137
flags.StringVar(&p.Progress, "progress", string(buildkit.AutoMode), fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
136138
flags.MarkHidden("progress") //nolint:errcheck
137139
flags.BoolVar(&opts.print, "print", false, "Print equivalent bake file")
140+
flags.BoolVar(&opts.check, "check", false, "Check build configuration")
138141

139142
return cmd
140143
}

docs/reference/compose_build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ run `docker compose build` to rebuild it.
1717
|:----------------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------|
1818
| `--build-arg` | `stringArray` | | Set build-time variables for services |
1919
| `--builder` | `string` | | Set builder to use |
20+
| `--check` | `bool` | | Check build configuration |
2021
| `--dry-run` | `bool` | | Execute command in dry run mode |
2122
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
2223
| `--no-cache` | `bool` | | Do not use cache when building the image |

docs/reference/docker_compose_build.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ options:
3333
experimentalcli: false
3434
kubernetes: false
3535
swarm: false
36+
- option: check
37+
value_type: bool
38+
default_value: "false"
39+
description: Check build configuration
40+
deprecated: false
41+
hidden: false
42+
experimental: false
43+
experimentalcli: false
44+
kubernetes: false
45+
swarm: false
3646
- option: compress
3747
value_type: bool
3848
default_value: "true"

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ type BuildOptions struct {
157157
Builder string
158158
// Print don't actually run builder but print equivalent build config
159159
Print bool
160+
// Check let builder validate build configuration
161+
Check bool
160162
}
161163

162164
// Apply mutates project according to build options

pkg/compose/build_bake.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,16 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
176176
privileged = true
177177
}
178178

179-
var output string
179+
var outputs []string
180+
var call string
180181
push := options.Push && service.Image != ""
181-
if len(service.Build.Platforms) > 1 {
182-
output = fmt.Sprintf("type=image,push=%t", push)
183-
} else {
184-
output = fmt.Sprintf("type=docker,load=true,push=%t", push)
182+
switch {
183+
case options.Check:
184+
call = "lint"
185+
case len(service.Build.Platforms) > 1:
186+
outputs = []string{fmt.Sprintf("type=image,push=%t", push)}
187+
default:
188+
outputs = []string{fmt.Sprintf("type=docker,load=true,push=%t", push)}
185189
}
186190

187191
read = append(read, build.Context)
@@ -212,7 +216,9 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
212216
ShmSize: build.ShmSize,
213217
Ulimits: toBakeUlimits(build.Ulimits),
214218
Entitlements: entitlements,
215-
Outputs: []string{output},
219+
220+
Outputs: outputs,
221+
Call: call,
216222
}
217223
group.Targets = append(group.Targets, serviceName)
218224
}
@@ -284,7 +290,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
284290
return nil, err
285291
}
286292

287-
var errMessage string
293+
var errMessage []string
288294
scanner := bufio.NewScanner(pipe)
289295
scanner.Split(bufio.ScanLines)
290296

@@ -300,7 +306,9 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
300306
err := decoder.Decode(&status)
301307
if err != nil {
302308
if strings.HasPrefix(line, "ERROR: ") {
303-
errMessage = line[7:]
309+
errMessage = append(errMessage, line[7:])
310+
} else {
311+
errMessage = append(errMessage, line)
304312
}
305313
continue
306314
}
@@ -310,8 +318,8 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
310318

311319
err = eg.Wait()
312320
if err != nil {
313-
if errMessage != "" {
314-
return nil, errors.New(errMessage)
321+
if len(errMessage) > 0 {
322+
return nil, errors.New(strings.Join(errMessage, "\n"))
315323
}
316324
return nil, fmt.Errorf("failed to execute bake: %w", err)
317325
}

pkg/e2e/build_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ func TestBuildImageDependencies(t *testing.T) {
300300
"DOCKER_BUILDKIT=1", "COMPOSE_BAKE=1",
301301
"COMPOSE_FILE=./fixtures/build-dependencies/compose.yaml",
302302
))
303-
doTest(t, cli, "build")
304-
doTest(t, cli, "build", "service")
303+
doTest(t, cli, "--verbose", "build")
304+
doTest(t, cli, "--verbose", "build", "service")
305305
})
306306
}
307307

0 commit comments

Comments
 (0)