You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-09-16 09:26:52 +02:00
feat: Add support for docker build-flags
I added `BuildFlags` field to Docker image config. Docker pipe is updated to append build flags to the `docker build` command. Build flags are not validated, leaving it Docker to error when given an unknown flag. See #813
This commit is contained in:
committed by
Carlos Alexandro Becker
parent
282fd74450
commit
c773e57165
@@ -128,7 +128,7 @@ func process(ctx *context.Context, docker config.Docker, artifact artifact.Artif
|
||||
if err := os.Link(artifact.Path, filepath.Join(tmp, filepath.Base(artifact.Path))); err != nil {
|
||||
return errors.Wrap(err, "failed to link binary")
|
||||
}
|
||||
if err := dockerBuild(ctx, tmp, images[0]); err != nil {
|
||||
if err := dockerBuild(ctx, tmp, images[0], docker.BuildFlags); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, img := range images[1:] {
|
||||
@@ -181,10 +181,10 @@ func publish(ctx *context.Context, docker config.Docker, images []string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func dockerBuild(ctx *context.Context, root, image string) error {
|
||||
func dockerBuild(ctx *context.Context, root, image string, flags []string) error {
|
||||
log.WithField("image", image).Info("building docker image")
|
||||
/* #nosec */
|
||||
var cmd = exec.CommandContext(ctx, "docker", "build", "-t", image, ".")
|
||||
var cmd = exec.CommandContext(ctx, "docker", buildCommand(image, flags)...)
|
||||
cmd.Dir = root
|
||||
log.WithField("cmd", cmd.Args).WithField("cwd", cmd.Dir).Debug("running")
|
||||
out, err := cmd.CombinedOutput()
|
||||
@@ -195,6 +195,12 @@ func dockerBuild(ctx *context.Context, root, image string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildCommand(image string, flags []string) []string {
|
||||
base := []string{"build", "-t", image, "."}
|
||||
base = append(base, flags...)
|
||||
return base
|
||||
}
|
||||
|
||||
func dockerTag(ctx *context.Context, image, tag string) error {
|
||||
log.WithField("image", image).WithField("tag", tag).Info("tagging docker image")
|
||||
/* #nosec */
|
||||
|
@@ -235,6 +235,47 @@ func TestRunPipe(t *testing.T) {
|
||||
},
|
||||
assertError: shouldNotErr,
|
||||
},
|
||||
"valid build args": {
|
||||
publish: false,
|
||||
dockers: []config.Docker{
|
||||
{
|
||||
Image: registry + "goreleaser/test_build_args",
|
||||
Goos: "linux",
|
||||
Goarch: "amd64",
|
||||
Dockerfile: "testdata/Dockerfile",
|
||||
Binary: "mybin",
|
||||
TagTemplates: []string{
|
||||
"latest",
|
||||
},
|
||||
BuildFlags: []string{
|
||||
"--label=foo=bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
expect: []string{
|
||||
registry + "goreleaser/test_build_args:latest",
|
||||
},
|
||||
assertError: shouldNotErr,
|
||||
},
|
||||
"bad build args": {
|
||||
publish: false,
|
||||
dockers: []config.Docker{
|
||||
{
|
||||
Image: registry + "goreleaser/test_build_args",
|
||||
Goos: "linux",
|
||||
Goarch: "amd64",
|
||||
Dockerfile: "testdata/Dockerfile",
|
||||
Binary: "mybin",
|
||||
TagTemplates: []string{
|
||||
"latest",
|
||||
},
|
||||
BuildFlags: []string{
|
||||
"--bad-flag",
|
||||
},
|
||||
},
|
||||
},
|
||||
assertError: shouldErr("unknown flag: --bad-flag"),
|
||||
},
|
||||
"bad_dockerfile": {
|
||||
publish: true,
|
||||
dockers: []config.Docker{
|
||||
@@ -415,6 +456,37 @@ func TestRunPipe(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCommand(t *testing.T) {
|
||||
image := "goreleaser/test_build_flag"
|
||||
tests := []struct {
|
||||
name string
|
||||
flags []string
|
||||
expect []string
|
||||
}{
|
||||
{
|
||||
name: "no flags",
|
||||
flags: []string{},
|
||||
expect: []string{"build", "-t", image, "."},
|
||||
},
|
||||
{
|
||||
name: "single flag",
|
||||
flags: []string{"--label=foo"},
|
||||
expect: []string{"build", "-t", image, ".", "--label=foo"},
|
||||
},
|
||||
{
|
||||
name: "multiple flags",
|
||||
flags: []string{"--label=foo", "--build-arg=bar=baz"},
|
||||
expect: []string{"build", "-t", image, ".", "--label=foo", "--build-arg=bar=baz"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
command := buildCommand(image, tt.flags)
|
||||
assert.Equal(t, tt.expect, command)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescription(t *testing.T) {
|
||||
assert.NotEmpty(t, Pipe{}.String())
|
||||
}
|
||||
@@ -508,6 +580,7 @@ func TestDefaultSet(t *testing.T) {
|
||||
assert.Equal(t, []string{"{{ .Version }}"}, docker.TagTemplates)
|
||||
assert.Equal(t, "Dockerfile.foo", docker.Dockerfile)
|
||||
}
|
||||
|
||||
func TestLinkFile(t *testing.T) {
|
||||
const srcFile = "/tmp/test"
|
||||
const dstFile = "/tmp/linked"
|
||||
|
@@ -247,6 +247,7 @@ type Docker struct {
|
||||
SkipPush bool `yaml:"skip_push,omitempty"`
|
||||
TagTemplates []string `yaml:"tag_templates,omitempty"`
|
||||
Files []string `yaml:"extra_files,omitempty"`
|
||||
BuildFlags []string `yaml:"build_flags,omitempty"`
|
||||
}
|
||||
|
||||
// Filters config
|
||||
|
Reference in New Issue
Block a user