Skip to content

Commit

Permalink
Merge pull request #10745 from glours/add-builder-support
Browse files Browse the repository at this point in the history
add support of --builder and BUILDX_BUILDER
  • Loading branch information
glours committed Jul 3, 2023
2 parents fa3e16c + 28301fb commit 827e864
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cmd/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -64,6 +69,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
Quiet: opts.quiet,
Services: services,
SSHs: SSHKeys,
Builder: builderName,
}, nil
}

Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions docs/reference/compose_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/docker_compose_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/build_buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e

import (
"fmt"
"net/http"
"runtime"
"strings"
Expand Down Expand Up @@ -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"),
})
})

}

0 comments on commit 827e864

Please sign in to comment.