introduce build --check

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2025-04-22 16:42:18 +02:00 committed by Nicolas De loof
parent 2dbef234dc
commit 16e83f002d
6 changed files with 36 additions and 12 deletions

View File

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

View File

@ -17,6 +17,7 @@ run `docker compose build` to rebuild it.
|:----------------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------| |:----------------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------|
| `--build-arg` | `stringArray` | | Set build-time variables for services | | `--build-arg` | `stringArray` | | Set build-time variables for services |
| `--builder` | `string` | | Set builder to use | | `--builder` | `string` | | Set builder to use |
| `--check` | `bool` | | Check build configuration |
| `--dry-run` | `bool` | | Execute command in dry run mode | | `--dry-run` | `bool` | | Execute command in dry run mode |
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. | | `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
| `--no-cache` | `bool` | | Do not use cache when building the image | | `--no-cache` | `bool` | | Do not use cache when building the image |

View File

@ -33,6 +33,16 @@ options:
experimentalcli: false experimentalcli: false
kubernetes: false kubernetes: false
swarm: false swarm: false
- option: check
value_type: bool
default_value: "false"
description: Check build configuration
deprecated: false
hidden: false
experimental: false
experimentalcli: false
kubernetes: false
swarm: false
- option: compress - option: compress
value_type: bool value_type: bool
default_value: "true" default_value: "true"

View File

@ -157,6 +157,8 @@ type BuildOptions struct {
Builder string Builder string
// Print don't actually run builder but print equivalent build config // Print don't actually run builder but print equivalent build config
Print bool Print bool
// Check let builder validate build configuration
Check bool
} }
// Apply mutates project according to build options // Apply mutates project according to build options

View File

@ -176,12 +176,16 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
privileged = true privileged = true
} }
var output string var outputs []string
var call string
push := options.Push && service.Image != "" push := options.Push && service.Image != ""
if len(service.Build.Platforms) > 1 { switch {
output = fmt.Sprintf("type=image,push=%t", push) case options.Check:
} else { call = "lint"
output = fmt.Sprintf("type=docker,load=true,push=%t", push) case len(service.Build.Platforms) > 1:
outputs = []string{fmt.Sprintf("type=image,push=%t", push)}
default:
outputs = []string{fmt.Sprintf("type=docker,load=true,push=%t", push)}
} }
read = append(read, build.Context) read = append(read, build.Context)
@ -212,7 +216,9 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
ShmSize: build.ShmSize, ShmSize: build.ShmSize,
Ulimits: toBakeUlimits(build.Ulimits), Ulimits: toBakeUlimits(build.Ulimits),
Entitlements: entitlements, Entitlements: entitlements,
Outputs: []string{output},
Outputs: outputs,
Call: call,
} }
group.Targets = append(group.Targets, serviceName) group.Targets = append(group.Targets, serviceName)
} }
@ -284,7 +290,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
return nil, err return nil, err
} }
var errMessage string var errMessage []string
scanner := bufio.NewScanner(pipe) scanner := bufio.NewScanner(pipe)
scanner.Split(bufio.ScanLines) scanner.Split(bufio.ScanLines)
@ -300,7 +306,9 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
err := decoder.Decode(&status) err := decoder.Decode(&status)
if err != nil { if err != nil {
if strings.HasPrefix(line, "ERROR: ") { if strings.HasPrefix(line, "ERROR: ") {
errMessage = line[7:] errMessage = append(errMessage, line[7:])
} else {
errMessage = append(errMessage, line)
} }
continue continue
} }
@ -310,8 +318,8 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
err = eg.Wait() err = eg.Wait()
if err != nil { if err != nil {
if errMessage != "" { if len(errMessage) > 0 {
return nil, errors.New(errMessage) return nil, errors.New(strings.Join(errMessage, "\n"))
} }
return nil, fmt.Errorf("failed to execute bake: %w", err) return nil, fmt.Errorf("failed to execute bake: %w", err)
} }

View File

@ -300,8 +300,8 @@ func TestBuildImageDependencies(t *testing.T) {
"DOCKER_BUILDKIT=1", "COMPOSE_BAKE=1", "DOCKER_BUILDKIT=1", "COMPOSE_BAKE=1",
"COMPOSE_FILE=./fixtures/build-dependencies/compose.yaml", "COMPOSE_FILE=./fixtures/build-dependencies/compose.yaml",
)) ))
doTest(t, cli, "build") doTest(t, cli, "--verbose", "build")
doTest(t, cli, "build", "service") doTest(t, cli, "--verbose", "build", "service")
}) })
} }