introduce build --with-dependencies

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-12-20 15:18:29 +01:00 committed by Nicolas De loof
parent 1baa4f4489
commit 17da54da20
5 changed files with 53 additions and 33 deletions

View File

@ -43,6 +43,7 @@ type buildOptions struct {
memory cliopts.MemBytes
ssh string
builder string
deps bool
}
func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
@ -74,6 +75,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
NoCache: opts.noCache,
Quiet: opts.quiet,
Services: services,
Deps: opts.deps,
SSHs: SSHKeys,
Builder: builderName,
}, nil
@ -108,24 +110,27 @@ func buildCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
}),
ValidArgsFunction: completeServiceNames(dockerCli, p),
}
cmd.Flags().BoolVar(&opts.push, "push", false, "Push service images.")
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
cmd.Flags().StringVar(&opts.ssh, "ssh", "", "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)")
cmd.Flags().StringVar(&opts.builder, "builder", "", "Set builder to use.")
cmd.Flags().Bool("parallel", true, "Build images in parallel. DEPRECATED")
cmd.Flags().MarkHidden("parallel") //nolint:errcheck
cmd.Flags().Bool("compress", true, "Compress the build context using gzip. DEPRECATED")
cmd.Flags().MarkHidden("compress") //nolint:errcheck
cmd.Flags().Bool("force-rm", true, "Always remove intermediate containers. DEPRECATED")
cmd.Flags().MarkHidden("force-rm") //nolint:errcheck
cmd.Flags().BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
cmd.Flags().Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
cmd.Flags().MarkHidden("no-rm") //nolint:errcheck
cmd.Flags().VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
cmd.Flags().StringVar(&p.Progress, "progress", string(buildkit.AutoMode), fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
cmd.Flags().MarkHidden("progress") //nolint:errcheck
flags := cmd.Flags()
flags.BoolVar(&opts.push, "push", false, "Push service images.")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
flags.BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
flags.StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
flags.StringVar(&opts.ssh, "ssh", "", "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)")
flags.StringVar(&opts.builder, "builder", "", "Set builder to use.")
flags.BoolVar(&opts.deps, "with-dependencies", false, "Also build dependencies (transitively).")
flags.Bool("parallel", true, "Build images in parallel. DEPRECATED")
flags.MarkHidden("parallel") //nolint:errcheck
flags.Bool("compress", true, "Compress the build context using gzip. DEPRECATED")
flags.MarkHidden("compress") //nolint:errcheck
flags.Bool("force-rm", true, "Always remove intermediate containers. DEPRECATED")
flags.MarkHidden("force-rm") //nolint:errcheck
flags.BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
flags.Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
flags.MarkHidden("no-rm") //nolint:errcheck
flags.VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
flags.StringVar(&p.Progress, "progress", string(buildkit.AutoMode), fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
flags.MarkHidden("progress") //nolint:errcheck
return cmd
}

View File

@ -5,17 +5,18 @@ Build or rebuild services
### Options
| Name | Type | Default | Description |
|:-----------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------|
| `--build-arg` | `stringArray` | | Set build-time variables for services. |
| `--builder` | `string` | | Set builder to use. |
| `--dry-run` | | | Execute command in dry run mode |
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
| `--no-cache` | | | Do not use cache when building the image |
| `--pull` | | | Always attempt to pull a newer version of the image. |
| `--push` | | | Push service images. |
| `-q`, `--quiet` | | | Don't print anything to STDOUT |
| `--ssh` | `string` | | Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent) |
| Name | Type | Default | Description |
|:----------------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------|
| `--build-arg` | `stringArray` | | Set build-time variables for services. |
| `--builder` | `string` | | Set builder to use. |
| `--dry-run` | | | Execute command in dry run mode |
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
| `--no-cache` | | | Do not use cache when building the image |
| `--pull` | | | Always attempt to pull a newer version of the image. |
| `--push` | | | Push service images. |
| `-q`, `--quiet` | | | Don't print anything to STDOUT |
| `--ssh` | `string` | | Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent) |
| `--with-dependencies` | | | Also build dependencies (transitively). |
<!---MARKER_GEN_END-->

View File

@ -147,6 +147,16 @@ options:
experimentalcli: false
kubernetes: false
swarm: false
- option: with-dependencies
value_type: bool
default_value: "false"
description: Also build dependencies (transitively).
deprecated: false
hidden: false
experimental: false
experimentalcli: false
kubernetes: false
swarm: false
inherited_options:
- option: dry-run
value_type: bool

View File

@ -135,6 +135,8 @@ type BuildOptions struct {
Quiet bool
// Services passed in the command line to be built
Services []string
// Deps also build selected services dependencies
Deps bool
// Ssh authentications passed in the command line
SSHs []types.SSHKey
// Memory limit for the build container

View File

@ -79,6 +79,11 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
imageIDs := map[string]string{}
serviceToBeBuild := map[string]serviceToBuild{}
var policy types.DependencyOption = types.IgnoreDependencies
if options.Deps {
policy = types.IncludeDependencies
}
err = project.WithServices(options.Services, func(service types.ServiceConfig) error {
if service.Build == nil {
return nil
@ -91,7 +96,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
name := service.Name
serviceToBeBuild[name] = serviceToBuild{name: name, service: service}
return nil
}, types.IgnoreDependencies)
}, policy)
if err != nil || len(serviceToBeBuild) == 0 {
return imageIDs, err
}
@ -146,9 +151,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
return -1
}
err = InDependencyOrder(ctx, project, func(ctx context.Context, name string) error {
if len(options.Services) > 0 && !utils.Contains(options.Services, name) {
return nil
}
serviceToBuild, ok := serviceToBeBuild[name]
if !ok {
return nil