Merge pull request #10745 from glours/add-builder-support

add support of --builder and BUILDX_BUILDER
This commit is contained in:
Guillaume Lours 2023-07-03 10:46:02 +02:00 committed by GitHub
commit 827e864ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 3 deletions

View File

@ -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")

View File

@ -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 |

View File

@ -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"

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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"),
})
})
}