Skip to content

Commit

Permalink
feat: use docker buildx build
Browse files Browse the repository at this point in the history
refs #1989

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Jan 4, 2021
1 parent ea83297 commit 0097bdc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Expand Up @@ -38,6 +38,7 @@ dockers:
- 'goreleaser/goreleaser:{{ .Tag }}-amd64'
- 'ghcr.io/goreleaser/goreleaser:{{ .Tag }}-amd64'
dockerfile: Dockerfile
use_buildx: true
binaries:
- goreleaser
build_flag_templates:
Expand All @@ -54,6 +55,7 @@ dockers:
- 'goreleaser/goreleaser:{{ .Tag }}-arm64'
- 'ghcr.io/goreleaser/goreleaser:{{ .Tag }}-arm64'
dockerfile: Dockerfile
use_buildx: true
binaries:
- goreleaser
build_flag_templates:
Expand Down
13 changes: 8 additions & 5 deletions internal/pipe/docker/docker.go
Expand Up @@ -169,7 +169,7 @@ func process(ctx *context.Context, docker config.Docker, bins []*artifact.Artifa
return err
}

if err := dockerBuild(ctx, tmp, images, buildFlags); err != nil {
if err := dockerBuild(ctx, tmp, images, buildFlags, docker.Buildx); err != nil {
return err
}

Expand Down Expand Up @@ -256,10 +256,10 @@ func link(src, dest string) error {
})
}

func dockerBuild(ctx *context.Context, root string, images, flags []string) error {
log.WithField("image", images[0]).Info("building docker image")
func dockerBuild(ctx *context.Context, root string, images, flags []string, buildx bool) error {
log.WithField("image", images[0]).WithField("buildx", buildx).Info("building docker image")
/* #nosec */
var cmd = exec.CommandContext(ctx, "docker", buildCommand(images, flags)...)
var cmd = exec.CommandContext(ctx, "docker", buildCommand(buildx, images, flags)...)
cmd.Dir = root
log.WithField("cmd", cmd.Args).WithField("cwd", cmd.Dir).Debug("running")
out, err := cmd.CombinedOutput()
Expand All @@ -270,8 +270,11 @@ func dockerBuild(ctx *context.Context, root string, images, flags []string) erro
return nil
}

func buildCommand(images, flags []string) []string {
func buildCommand(buildx bool, images, flags []string) []string {
base := []string{"build", "."}
if buildx {
base = []string{"buildx", "build", "."}
}
for _, image := range images {
base = append(base, "-t", image)
}
Expand Down
10 changes: 8 additions & 2 deletions internal/pipe/docker/docker_test.go
Expand Up @@ -847,6 +847,7 @@ func TestBuildCommand(t *testing.T) {
tests := []struct {
name string
flags []string
buildx bool
expect []string
}{
{
Expand All @@ -864,11 +865,16 @@ func TestBuildCommand(t *testing.T) {
flags: []string{"--label=foo", "--build-arg=bar=baz"},
expect: []string{"build", ".", "-t", images[0], "-t", images[1], "--label=foo", "--build-arg=bar=baz"},
},
{
name: "buildx",
buildx: true,
flags: []string{"--label=foo", "--build-arg=bar=baz"},
expect: []string{"buildx", "build", ".", "-t", images[0], "-t", images[1], "--label=foo", "--build-arg=bar=baz"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
command := buildCommand(images, tt.flags)
require.Equal(t, tt.expect, command)
require.Equal(t, tt.expect, buildCommand(tt.buildx, images, tt.flags))
})
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -494,6 +494,7 @@ type Docker struct {
SkipPush string `yaml:"skip_push,omitempty"`
Files []string `yaml:"extra_files,omitempty"`
BuildFlagTemplates []string `yaml:"build_flag_templates,omitempty"`
Buildx bool `yaml:"use_buildx,omitempty"`
}

// DockerManifest config.
Expand Down
9 changes: 9 additions & 0 deletions www/docs/customization/docker.md
Expand Up @@ -80,6 +80,11 @@ dockers:
# Path to the Dockerfile (from the project root).
dockerfile: Dockerfile

# Wether to use `docker buildx build` instead of `docker build`.
# You probably want to set it to true when using flags like `--platform`.
# Defaults to false.
use_buildx: true

# Template of the docker build flags.
build_flag_templates:
- "--pull"
Expand All @@ -88,6 +93,7 @@ dockers:
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--build-arg=FOO={{.Env.Bar}}"
- "--platform=linux/arm64"

# If your Dockerfile copies files other than the binary itself,
# you should list them here as well.
Expand All @@ -105,6 +111,9 @@ dockers:
!!! tip
Learn more about the [name template engine](/customization/templates/).

!!! tip
You can also create multi-platform images using the [docker_manifests](/customization/docker_manifest/) config.

These settings should allow you to generate multiple Docker images,
for example, using multiple `FROM` statements,
as well as generate one image for each binary in your project.
Expand Down

1 comment on commit 0097bdc

@vercel
Copy link

@vercel vercel bot commented on 0097bdc Jan 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.