1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-29 21:47:01 +02:00

also supporting latest docker tag

This commit is contained in:
Carlos Alexandro Becker 2017-09-14 20:16:49 -03:00
parent f4c79826da
commit 2175bf1e80
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
4 changed files with 39 additions and 4 deletions

@ -167,6 +167,7 @@ type Docker struct {
Goarm string `yaml:",omitempty"`
Image string `yaml:",omitempty"`
Dockerfile string `yaml:",omitempty"`
Latest bool `yaml:",omitempty"`
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`

@ -50,6 +50,8 @@ dockers:
image: myuser/myimage
# Path to the Dockerfile (from the project root)
dockerfile: Dockerfile
# Also tag and push myuser/myimage:latest
latest: true
```
These settings should allow you to generate multiple docker images, using

@ -62,6 +62,7 @@ func doRun(ctx *context.Context, folder string, docker config.Docker, binary con
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)
var latest = fmt.Sprintf("%s:latest", docker.Image)
if err := os.Link(docker.Dockerfile, dockerfile); err != nil {
return errors.Wrap(err, "failed to link dockerfile")
@ -69,6 +70,11 @@ func doRun(ctx *context.Context, folder string, docker config.Docker, binary con
if err := dockerBuild(root, dockerfile, image); err != nil {
return err
}
if docker.Latest {
if err := dockerTag(image, latest); err != nil {
return err
}
}
// TODO: improve this so it can log into to stdout
if !ctx.Publish {
@ -80,6 +86,11 @@ func doRun(ctx *context.Context, folder string, docker config.Docker, binary con
if err := dockerPush(image); err != nil {
return err
}
if docker.Latest {
if err := dockerTag(image, latest); err != nil {
return err
}
}
return nil
}
@ -95,6 +106,18 @@ func dockerBuild(root, dockerfile, image string) error {
return nil
}
func dockerTag(image, tag string) error {
log.WithField("image", image).WithField("tag", tag).Info("building docker image")
var cmd = exec.Command("docker", "tag", image, tag)
log.WithField("cmd", cmd).Debug("executing")
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "failed to tag docker image: \n%s", string(out))
}
log.Debugf("docker tag output: \n%s", string(out))
return nil
}
func dockerPush(image string) error {
log.WithField("image", image).Info("pushing docker image")
var cmd = exec.Command("docker", "push", image)

@ -24,8 +24,14 @@ func TestRunPipe(t *testing.T) {
var binPath = filepath.Join(dist, "mybin", "mybin")
_, err = os.Create(binPath)
assert.NoError(err)
var images = []string{
"goreleaser/test_run_pipe:1.0.0",
"goreleaser/test_run_pipe:latest",
}
// this might fail as the image doesnt exist yet, so lets ignore the error
_ = exec.Command("docker", "rmi", "goreleaser/test_run_pipe:v1.0.0").Run()
for _, img := range images {
_ = exec.Command("docker", "rmi", img).Run()
}
var ctx = &context.Context{
Version: "1.0.0",
Publish: true,
@ -39,6 +45,7 @@ func TestRunPipe(t *testing.T) {
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile",
Binary: "mybin",
Latest: true,
},
{
Image: "goreleaser/test_run_pipe_nope",
@ -54,11 +61,13 @@ func TestRunPipe(t *testing.T) {
ctx.AddBinary(plat, "mybin", "mybin", binPath)
}
assert.NoError(Pipe{}.Run(ctx))
// this might should not fail as the image should have been created when
// the step ran
assert.NoError(
exec.Command("docker", "rmi", "goreleaser/test_run_pipe:1.0.0").Run(),
)
for _, img := range images {
assert.NoError(exec.Command("docker", "rmi", img).Run())
}
// the test_run_pipe_nope image should not have been created, so deleting
// it should fail
assert.Error(