1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

fix: docker: build several tags

avoid needing to call docker tag later
This commit is contained in:
Carlos Alexandro Becker 2018-10-25 14:18:58 -03:00 committed by Carlos Alexandro Becker
parent 9500155274
commit 3010023b3a
2 changed files with 15 additions and 29 deletions

View File

@ -151,14 +151,9 @@ func process(ctx *context.Context, docker config.Docker, bin artifact.Artifact)
return err
}
if err := dockerBuild(ctx, tmp, images[0], buildFlags); err != nil {
if err := dockerBuild(ctx, tmp, images, buildFlags); err != nil {
return err
}
for _, img := range images[1:] {
if err := dockerTag(ctx, images[0], img); err != nil {
return err
}
}
if docker.SkipPush {
// TODO: this should also be better handled
log.Warn(pipe.Skip("skip_push is set").Error())
@ -249,10 +244,10 @@ func link(src, dest string) error {
})
}
func dockerBuild(ctx *context.Context, root, image string, flags []string) error {
log.WithField("image", image).Info("building docker image")
func dockerBuild(ctx *context.Context, root string, images, flags []string) error {
log.WithField("image", images[0]).Info("building docker image")
/* #nosec */
var cmd = exec.CommandContext(ctx, "docker", buildCommand(image, flags)...)
var cmd = exec.CommandContext(ctx, "docker", buildCommand(images, flags)...)
cmd.Dir = root
log.WithField("cmd", cmd.Args).WithField("cwd", cmd.Dir).Debug("running")
out, err := cmd.CombinedOutput()
@ -263,25 +258,15 @@ func dockerBuild(ctx *context.Context, root, image string, flags []string) error
return nil
}
func buildCommand(image string, flags []string) []string {
base := []string{"build", "-t", image, "."}
func buildCommand(images, flags []string) []string {
base := []string{"build", "."}
for _, image := range images {
base = append(base, "-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 */
var cmd = exec.CommandContext(ctx, "docker", "tag", image, tag)
log.WithField("cmd", cmd.Args).Debug("running")
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "failed to tag docker image: \n%s", string(out))
}
log.Debugf("docker tag output: \n%s", string(out))
return nil
}
func dockerPush(ctx *context.Context, image artifact.Artifact) error {
log.WithField("image", image.Name).Info("pushing docker image")
/* #nosec */

View File

@ -604,31 +604,32 @@ func TestRunPipe(t *testing.T) {
}
func TestBuildCommand(t *testing.T) {
image := "goreleaser/test_build_flag"
images := []string{"goreleaser/test_build_flag", "goreleaser/test_multiple_tags"}
tests := []struct {
name string
images []string
flags []string
expect []string
}{
{
name: "no flags",
flags: []string{},
expect: []string{"build", "-t", image, "."},
expect: []string{"build", ".", "-t", images[0], "-t", images[1]},
},
{
name: "single flag",
flags: []string{"--label=foo"},
expect: []string{"build", "-t", image, ".", "--label=foo"},
expect: []string{"build", ".", "-t", images[0], "-t", images[1], "--label=foo"},
},
{
name: "multiple flags",
flags: []string{"--label=foo", "--build-arg=bar=baz"},
expect: []string{"build", "-t", image, ".", "--label=foo", "--build-arg=bar=baz"},
expect: []string{"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(image, tt.flags)
command := buildCommand(images, tt.flags)
assert.Equal(t, tt.expect, command)
})
}