1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-09-16 09:26:52 +02:00

feat: more fields on docker tag templates

This commit is contained in:
Carlos Alexandro Becker
2018-07-08 21:05:18 -07:00
parent 4f39039c8a
commit 4557346fc6
2 changed files with 9 additions and 37 deletions

View File

@@ -2,16 +2,13 @@
package docker package docker
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"text/template"
"github.com/apex/log" "github.com/apex/log"
"github.com/masterminds/semver"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
@@ -19,6 +16,7 @@ import (
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/deprecate" "github.com/goreleaser/goreleaser/internal/deprecate"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pipeline" "github.com/goreleaser/goreleaser/pipeline"
) )
@@ -116,44 +114,16 @@ func doRun(ctx *context.Context) error {
return g.Wait() return g.Wait()
} }
func tagName(ctx *context.Context, tagTemplate string) (string, error) {
var out bytes.Buffer
t, err := template.New("tag").Option("missingkey=error").Parse(tagTemplate)
if err != nil {
return "", err
}
sv, err := semver.NewVersion(ctx.Git.CurrentTag)
if err != nil {
return "", err
}
data := struct {
Version string
Tag string
Commit string
Major int64
Minor int64
Patch int64
Env map[string]string
}{
Version: ctx.Version,
Commit: ctx.Git.Commit,
Tag: ctx.Git.CurrentTag,
Env: ctx.Env,
Major: sv.Major(),
Minor: sv.Minor(),
Patch: sv.Patch(),
}
err = t.Execute(&out, data)
return out.String(), err
}
func process(ctx *context.Context, docker config.Docker, artifact artifact.Artifact, seed int) error { func process(ctx *context.Context, docker config.Docker, artifact artifact.Artifact, seed int) error {
var root = filepath.Dir(artifact.Path) var root = filepath.Dir(artifact.Path)
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile)) + fmt.Sprintf(".%d", seed) var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile)) + fmt.Sprintf(".%d", seed)
// nolint:prealloc // nolint:prealloc
var images []string var images []string
for _, tagTemplate := range docker.TagTemplates { for _, tagTemplate := range docker.TagTemplates {
tag, err := tagName(ctx, tagTemplate) // TODO: add overrides support to config
tag, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(tagTemplate)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to execute tag template '%s'", tagTemplate) return errors.Wrapf(err, "failed to execute tag template '%s'", tagTemplate)
} }

View File

@@ -80,6 +80,7 @@ func TestRunPipe(t *testing.T) {
"v{{.Major}}", "v{{.Major}}",
"v{{.Major}}.{{.Minor}}", "v{{.Major}}.{{.Minor}}",
"commint-{{.Commit}}", "commint-{{.Commit}}",
"le-{{.Os}}",
"latest", "latest",
}, },
Files: []string{ Files: []string{
@@ -91,6 +92,7 @@ func TestRunPipe(t *testing.T) {
registry + "goreleaser/test_run_pipe:v1.0.0-123", registry + "goreleaser/test_run_pipe:v1.0.0-123",
registry + "goreleaser/test_run_pipe:v1", registry + "goreleaser/test_run_pipe:v1",
registry + "goreleaser/test_run_pipe:v1.0", registry + "goreleaser/test_run_pipe:v1.0",
registry + "goreleaser/test_run_pipe:le-linux",
registry + "goreleaser/test_run_pipe:latest", registry + "goreleaser/test_run_pipe:latest",
}, },
assertError: shouldNotErr, assertError: shouldNotErr,
@@ -226,7 +228,7 @@ func TestRunPipe(t *testing.T) {
}, },
}, },
}, },
assertError: shouldErr(`template: tag:1: unexpected "}" in operand`), assertError: shouldErr(`template: tmpl:1: unexpected "}" in operand`),
}, },
"missing_env_on_template": { "missing_env_on_template": {
publish: true, publish: true,
@@ -242,7 +244,7 @@ func TestRunPipe(t *testing.T) {
}, },
}, },
}, },
assertError: shouldErr(`template: tag:1:6: executing "tag" at <.Env.NOPE>: map has no entry for key "NOPE"`), assertError: shouldErr(`template: tmpl:1:6: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`),
}, },
"no_permissions": { "no_permissions": {
publish: true, publish: true,