From 2175bf1e80e47b0e9cafc70877b174a715bc3f16 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 14 Sep 2017 20:16:49 -0300 Subject: [PATCH] also supporting latest docker tag --- config/config.go | 1 + docs/130-docker.md | 2 ++ pipeline/docker/docker.go | 23 +++++++++++++++++++++++ pipeline/docker/docker_test.go | 17 +++++++++++++---- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 995657c37..72351c87f 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` diff --git a/docs/130-docker.md b/docs/130-docker.md index 365cc00bd..65e5f4550 100644 --- a/docs/130-docker.md +++ b/docs/130-docker.md @@ -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 diff --git a/pipeline/docker/docker.go b/pipeline/docker/docker.go index cb9a84d3d..6f28113ba 100644 --- a/pipeline/docker/docker.go +++ b/pipeline/docker/docker.go @@ -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) diff --git a/pipeline/docker/docker_test.go b/pipeline/docker/docker_test.go index 76ecbb96c..0b2c5faa4 100644 --- a/pipeline/docker/docker_test.go +++ b/pipeline/docker/docker_test.go @@ -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(