From 28301fb1a43ce184e69998f2acfe2ab203f39978 Mon Sep 17 00:00:00 2001 From: Guillaume Lours <705411+glours@users.noreply.github.com> Date: Tue, 27 Jun 2023 12:32:47 +0200 Subject: [PATCH] add support of --builder and BUILDX_BUILDER Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com> --- cmd/compose/build.go | 7 ++++++ docs/reference/compose_build.md | 1 + docs/reference/docker_compose_build.yaml | 9 ++++++++ pkg/api/api.go | 2 ++ pkg/compose/build.go | 2 +- pkg/compose/build_buildkit.go | 4 ++-- pkg/e2e/build_test.go | 28 ++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 3 deletions(-) diff --git a/cmd/compose/build.go b/cmd/compose/build.go index d7ef65ff5..d8993a26c 100644 --- a/cmd/compose/build.go +++ b/cmd/compose/build.go @@ -43,6 +43,7 @@ type buildOptions struct { noCache bool memory cliopts.MemBytes ssh string + builder string } func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) { @@ -54,6 +55,10 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, return api.BuildOptions{}, err } } + builderName := opts.builder + if builderName == "" { + builderName = os.Getenv("BUILDX_BUILDER") + } return api.BuildOptions{ Pull: opts.pull, @@ -64,6 +69,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, Quiet: opts.quiet, Services: services, SSHs: SSHKeys, + Builder: builderName, }, nil } @@ -101,6 +107,7 @@ func buildCommand(p *ProjectOptions, progress *string, backend api.Service) *cob 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") diff --git a/docs/reference/compose_build.md b/docs/reference/compose_build.md index b978c83f6..1eaec63e8 100644 --- a/docs/reference/compose_build.md +++ b/docs/reference/compose_build.md @@ -8,6 +8,7 @@ Build or rebuild services | 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 | diff --git a/docs/reference/docker_compose_build.yaml b/docs/reference/docker_compose_build.yaml index efab6c055..88a487494 100644 --- a/docs/reference/docker_compose_build.yaml +++ b/docs/reference/docker_compose_build.yaml @@ -24,6 +24,15 @@ options: experimentalcli: false kubernetes: false swarm: false + - option: builder + value_type: string + description: Set builder to use. + deprecated: false + hidden: false + experimental: false + experimentalcli: false + kubernetes: false + swarm: false - option: compress value_type: bool default_value: "true" diff --git a/pkg/api/api.go b/pkg/api/api.go index e6ae1a961..a693abb50 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -130,6 +130,8 @@ type BuildOptions struct { SSHs []types.SSHKey // Memory limit for the build container Memory int64 + // Builder name passed in the command line + Builder string } // Apply mutates project according to build options diff --git a/pkg/compose/build.go b/pkg/compose/build.go index 896eefe7d..f513b309a 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -116,7 +116,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti } buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, flatten(args)) - digest, err := s.doBuildBuildkit(ctx, service.Name, buildOptions, w) + digest, err := s.doBuildBuildkit(ctx, service.Name, buildOptions, w, options.Builder) if err != nil { return err } diff --git a/pkg/compose/build_buildkit.go b/pkg/compose/build_buildkit.go index 8a8a7a0fe..43bb94f8e 100644 --- a/pkg/compose/build_buildkit.go +++ b/pkg/compose/build_buildkit.go @@ -34,8 +34,8 @@ import ( "github.com/moby/buildkit/client" ) -func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer) (string, error) { - b, err := builder.New(s.dockerCli) +func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer, builderName string) (string, error) { + b, err := builder.New(s.dockerCli, builder.WithName(builderName)) if err != nil { return "", err } diff --git a/pkg/e2e/build_test.go b/pkg/e2e/build_test.go index 3af4347db..76b8c6427 100644 --- a/pkg/e2e/build_test.go +++ b/pkg/e2e/build_test.go @@ -17,6 +17,7 @@ package e2e import ( + "fmt" "net/http" "runtime" "strings" @@ -423,3 +424,30 @@ func TestBuildPlatformsStandardErrors(t *testing.T) { }) } + +func TestBuildBuilder(t *testing.T) { + c := NewParallelCLI(t) + builderName := "build-with-builder" + // declare builder + result := c.RunDockerCmd(t, "buildx", "create", "--name", builderName, "--use", "--bootstrap") + assert.NilError(t, result.Error) + + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/", "down") + _ = c.RunDockerCmd(t, "buildx", "rm", "-f", builderName) + }) + + t.Run("use specific builder to run build command", func(t *testing.T) { + res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test", "build", "--builder", builderName) + assert.NilError(t, res.Error, res.Stderr()) + }) + + t.Run("error when using specific builder to run build command", func(t *testing.T) { + res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test", "build", "--builder", "unknown-builder") + res.Assert(t, icmd.Expected{ + ExitCode: 1, + Err: fmt.Sprintf(`no builder %q found`, "unknown-builder"), + }) + }) + +}