mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-18 03:56:52 +02:00
feat: allow templates on docker tags
Allow to template docker tags. Closes #433
This commit is contained in:
parent
6dc31b53bd
commit
14d1347ed9
@ -181,14 +181,15 @@ type Checksum struct {
|
||||
|
||||
// Docker image config
|
||||
type Docker struct {
|
||||
Binary string `yaml:",omitempty"`
|
||||
Goos string `yaml:",omitempty"`
|
||||
Goarch string `yaml:",omitempty"`
|
||||
Goarm string `yaml:",omitempty"`
|
||||
Image string `yaml:",omitempty"`
|
||||
Dockerfile string `yaml:",omitempty"`
|
||||
Latest bool `yaml:",omitempty"`
|
||||
Files []string `yaml:"extra_files,omitempty"`
|
||||
Binary string `yaml:",omitempty"`
|
||||
Goos string `yaml:",omitempty"`
|
||||
Goarch string `yaml:",omitempty"`
|
||||
Goarm string `yaml:",omitempty"`
|
||||
Image string `yaml:",omitempty"`
|
||||
Dockerfile string `yaml:",omitempty"`
|
||||
Latest bool `yaml:",omitempty"`
|
||||
TagTemplate string `yaml:"tag_template,omitempty"`
|
||||
Files []string `yaml:"extra_files,omitempty"`
|
||||
|
||||
// Capture all undefined fields and should be empty after loading
|
||||
XXX map[string]interface{} `yaml:",inline"`
|
||||
|
@ -2,11 +2,13 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/alecthomas/template"
|
||||
"github.com/apex/log"
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
@ -38,6 +40,11 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
|
||||
// Default sets the pipe defaults
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
for i := range ctx.Config.Dockers {
|
||||
if ctx.Config.Dockers[i].TagTemplate == "" {
|
||||
ctx.Config.Dockers[i].TagTemplate = "{{ .Version }}"
|
||||
}
|
||||
}
|
||||
// only set defaults if there is exacly 1 docker setup in the config file.
|
||||
if len(ctx.Config.Dockers) != 1 {
|
||||
return nil
|
||||
@ -80,10 +87,30 @@ func doRun(ctx *context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagName(ctx *context.Context, docker config.Docker) (string, error) {
|
||||
var out bytes.Buffer
|
||||
t, err := template.New("tag").Parse(docker.TagTemplate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data := struct {
|
||||
Version, Tag string
|
||||
}{
|
||||
Version: ctx.Version,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
}
|
||||
err = t.Execute(&out, data)
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
func process(ctx *context.Context, folder string, docker config.Docker, binary context.Binary) error {
|
||||
var root = filepath.Join(ctx.Config.Dist, folder)
|
||||
var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile))
|
||||
var image = fmt.Sprintf("%s:%s", docker.Image, ctx.Version)
|
||||
tag, err := tagName(ctx, docker)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var image = fmt.Sprintf("%s:%s", docker.Image, tag)
|
||||
var latest = fmt.Sprintf("%s:latest", docker.Image)
|
||||
|
||||
if err := os.Link(docker.Dockerfile, dockerfile); err != nil {
|
||||
|
@ -40,50 +40,88 @@ func TestRunPipe(t *testing.T) {
|
||||
var binPath = filepath.Join(dist, "mybin", "mybin")
|
||||
_, err = os.Create(binPath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var table = map[string]struct {
|
||||
docker config.Docker
|
||||
err string
|
||||
}{
|
||||
"valid": {
|
||||
docker: config.Docker{
|
||||
Image: "localhost:5000/goreleaser/test_run_pipe",
|
||||
Goos: "linux",
|
||||
Goarch: "amd64",
|
||||
Dockerfile: "testdata/Dockerfile",
|
||||
Binary: "mybin",
|
||||
Latest: true,
|
||||
TagTemplate: "{{.Tag}}",
|
||||
},
|
||||
err: "",
|
||||
},
|
||||
"invalid": {
|
||||
docker: config.Docker{
|
||||
Image: "localhost:5000/goreleaser/test_run_pipe_nope",
|
||||
Goos: "linux",
|
||||
Goarch: "amd64",
|
||||
Dockerfile: "testdata/Dockerfile",
|
||||
Binary: "otherbin",
|
||||
TagTemplate: "{{.Version}}",
|
||||
},
|
||||
err: "",
|
||||
},
|
||||
"template_error": {
|
||||
docker: config.Docker{
|
||||
Image: "localhost:5000/goreleaser/test_run_pipe_template_error",
|
||||
Goos: "linux",
|
||||
Goarch: "amd64",
|
||||
Dockerfile: "testdata/Dockerfile",
|
||||
Binary: "mybin",
|
||||
Latest: true,
|
||||
TagTemplate: "{{.Tag}",
|
||||
},
|
||||
err: `template: tag:1: unexpected "}" in operand; missing space?`,
|
||||
},
|
||||
}
|
||||
var images = []string{
|
||||
"localhost:5000/goreleaser/test_run_pipe:1.0.0",
|
||||
"localhost:5000/goreleaser/test_run_pipe:v1.0.0",
|
||||
"localhost:5000/goreleaser/test_run_pipe:latest",
|
||||
}
|
||||
// this might fail as the image doesnt exist yet, so lets ignore the error
|
||||
for _, img := range images {
|
||||
_ = exec.Command("docker", "rmi", img).Run()
|
||||
}
|
||||
var ctx = &context.Context{
|
||||
Version: "1.0.0",
|
||||
Publish: true,
|
||||
Config: config.Project{
|
||||
ProjectName: "mybin",
|
||||
Dist: dist,
|
||||
Dockers: []config.Docker{
|
||||
{
|
||||
Image: "localhost:5000/goreleaser/test_run_pipe",
|
||||
Goos: "linux",
|
||||
Goarch: "amd64",
|
||||
Dockerfile: "testdata/Dockerfile",
|
||||
Binary: "mybin",
|
||||
Latest: true,
|
||||
|
||||
for name, docker := range table {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var ctx = &context.Context{
|
||||
Version: "1.0.0",
|
||||
Publish: true,
|
||||
Git: context.GitInfo{
|
||||
CurrentTag: "v1.0.0",
|
||||
},
|
||||
{
|
||||
Image: "localhost:5000/goreleaser/test_run_pipe_nope",
|
||||
Goos: "linux",
|
||||
Goarch: "amd64",
|
||||
Dockerfile: "testdata/Dockerfile",
|
||||
Binary: "otherbin",
|
||||
Config: config.Project{
|
||||
ProjectName: "mybin",
|
||||
Dist: dist,
|
||||
Dockers: []config.Docker{
|
||||
docker.docker,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64"} {
|
||||
ctx.AddBinary(plat, "mybin", "mybin", binPath)
|
||||
}
|
||||
if docker.err != "" {
|
||||
assert.EqualError(t, Pipe{}.Run(ctx), docker.err)
|
||||
} else {
|
||||
assert.NoError(t, Pipe{}.Run(ctx))
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64"} {
|
||||
ctx.AddBinary(plat, "mybin", "mybin", binPath)
|
||||
}
|
||||
assert.NoError(t, Pipe{}.Run(ctx))
|
||||
|
||||
// this might should not fail as the image should have been created when
|
||||
// the step ran
|
||||
for _, img := range images {
|
||||
assert.NoError(t, exec.Command("docker", "rmi", img).Run())
|
||||
}
|
||||
|
||||
// the test_run_pipe_nope image should not have been created, so deleting
|
||||
// it should fail
|
||||
assert.Error(t,
|
||||
@ -152,6 +190,7 @@ func TestDefault(t *testing.T) {
|
||||
assert.Equal(t, "amd64", docker.Goarch)
|
||||
assert.Equal(t, ctx.Config.Builds[0].Binary, docker.Binary)
|
||||
assert.Equal(t, "Dockerfile", docker.Dockerfile)
|
||||
assert.Equal(t, "{{ .Version }}", docker.TagTemplate)
|
||||
}
|
||||
|
||||
func TestDefaultNoDockers(t *testing.T) {
|
||||
@ -183,5 +222,6 @@ func TestDefaultSet(t *testing.T) {
|
||||
assert.Equal(t, "windows", docker.Goos)
|
||||
assert.Equal(t, "i386", docker.Goarch)
|
||||
assert.Equal(t, "bar", docker.Binary)
|
||||
assert.Equal(t, "{{ .Version }}", docker.TagTemplate)
|
||||
assert.Equal(t, "Dockerfile.foo", docker.Dockerfile)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user