Skip to content

Commit

Permalink
feat: support templates on docker.binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jan 18, 2019
1 parent 7d7951a commit ef0bb10
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
10 changes: 9 additions & 1 deletion internal/pipe/docker/docker.go
Expand Up @@ -111,14 +111,22 @@ 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),
artifact.ByGoarch(docker.Goarch),
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
}
Expand Down
47 changes: 44 additions & 3 deletions internal/pipe/docker/docker_test.go
Expand Up @@ -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
Expand All @@ -97,6 +98,9 @@ func TestRunPipe(t *testing.T) {
}{
"valid": {
publish: true,
env: map[string]string{
"FOO": "123",
},
dockers: []config.Docker{
{
ImageTemplates: []string{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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",
Expand Down

0 comments on commit ef0bb10

Please sign in to comment.