mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
feat: more fields on docker tag templates
This commit is contained in:
parent
4f39039c8a
commit
4557346fc6
@ -2,16 +2,13 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/masterminds/semver"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
@ -19,6 +16,7 @@ import (
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/goreleaser/goreleaser/internal/deprecate"
|
||||
"github.com/goreleaser/goreleaser/internal/tmpl"
|
||||
"github.com/goreleaser/goreleaser/pipeline"
|
||||
)
|
||||
|
||||
@ -116,44 +114,16 @@ func doRun(ctx *context.Context) error {
|
||||
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 {
|
||||
var root = filepath.Dir(artifact.Path)
|
||||
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile)) + fmt.Sprintf(".%d", seed)
|
||||
// nolint:prealloc
|
||||
var images []string
|
||||
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 {
|
||||
return errors.Wrapf(err, "failed to execute tag template '%s'", tagTemplate)
|
||||
}
|
||||
|
@ -80,6 +80,7 @@ func TestRunPipe(t *testing.T) {
|
||||
"v{{.Major}}",
|
||||
"v{{.Major}}.{{.Minor}}",
|
||||
"commint-{{.Commit}}",
|
||||
"le-{{.Os}}",
|
||||
"latest",
|
||||
},
|
||||
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",
|
||||
registry + "goreleaser/test_run_pipe:v1.0",
|
||||
registry + "goreleaser/test_run_pipe:le-linux",
|
||||
registry + "goreleaser/test_run_pipe:latest",
|
||||
},
|
||||
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": {
|
||||
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": {
|
||||
publish: true,
|
||||
|
Loading…
x
Reference in New Issue
Block a user