1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-04 03:11:55 +02:00

feat: support templates on docker.binaries

This commit is contained in:
Carlos Alexandro Becker 2019-01-17 18:22:12 -02:00 committed by Carlos Alexandro Becker
parent 7d7951a223
commit ef0bb10863
2 changed files with 53 additions and 4 deletions

View File

@ -111,6 +111,14 @@ func doRun(ctx *context.Context) error {
docker := docker
g.Go(func() error {
log.WithField("docker", docker).Debug("looking for binaries matching")
var binaryNames = make([]string, len(docker.Binaries))
for i := range docker.Binaries {
bin, err := tmpl.New(ctx).Apply(docker.Binaries[i])
if err != nil {
return errors.Wrapf(err, "failed to execute binary template '%s'", docker.Binaries[i])
}
binaryNames[i] = bin
}
var binaries = ctx.Artifacts.Filter(
artifact.And(
artifact.ByGoos(docker.Goos),
@ -118,7 +126,7 @@ func doRun(ctx *context.Context) error {
artifact.ByGoarm(docker.Goarm),
artifact.ByType(artifact.Binary),
func(a artifact.Artifact) bool {
for _, bin := range docker.Binaries {
for _, bin := range binaryNames {
if a.ExtraOr("Binary", "").(string) == bin {
return true
}

View File

@ -89,6 +89,7 @@ func TestRunPipe(t *testing.T) {
var table = map[string]struct {
dockers []config.Docker
env map[string]string
publish bool
expect []string
assertImageLabels imageLabelFinder
@ -97,6 +98,9 @@ func TestRunPipe(t *testing.T) {
}{
"valid": {
publish: true,
env: map[string]string{
"FOO": "123",
},
dockers: []config.Docker{
{
ImageTemplates: []string{
@ -415,6 +419,9 @@ func TestRunPipe(t *testing.T) {
SkipPush: true,
},
},
env: map[string]string{
"FOO": "123",
},
expect: []string{
registry + "goreleaser/mybin:v1.0.0-123",
registry + "goreleaser/mybin:latest",
@ -505,6 +512,42 @@ func TestRunPipe(t *testing.T) {
},
},
// TODO: add a test case for multiple matching binaries for the same name
"templated_binaries": {
publish: true,
env: map[string]string{
"BIN_NAME": "mybin",
},
dockers: []config.Docker{
{
ImageTemplates: []string{registry + "goreleaser/templatedbins:latest"},
Goos: "darwin",
Goarch: "amd64",
Binaries: []string{"{{.Env.BIN_NAME}}"},
Dockerfile: "testdata/Dockerfile",
},
},
assertImageLabels: noLabels,
assertError: shouldNotErr,
pubAssertError: shouldNotErr,
expect: []string{
registry + "goreleaser/templatedbins:latest",
},
},
"binaries_template_error": {
dockers: []config.Docker{
{
ImageTemplates: []string{
registry + "goreleaser/binaries_template_error:latest",
},
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binaries: []string{"{{.Env.BAR}"},
},
},
assertImageLabels: noLabels,
assertError: shouldErr(`template: tmpl:1: unexpected "}" in operand`),
},
}
killAndRm(t)
@ -529,9 +572,7 @@ func TestRunPipe(t *testing.T) {
Dockers: docker.dockers,
})
ctx.SkipPublish = !docker.publish
ctx.Env = map[string]string{
"FOO": "123",
}
ctx.Env = docker.env
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{
CurrentTag: "v1.0.0",