Repository: goreleaser/goreleaser
Pull request #978 (2019-03-05 10:13:56)

ability to skip Docker update for pre-releases (#977)
If applied, this commit will skip docker pushes on `skip_publish` builds, draft builds and on pre-releases if `skip_push` is set to `auto`

Closes https://github.com/goreleaser/goreleaser/issues/977

I directly followed approach introduced in https://github.com/goreleaser/goreleaser/pull/943
So:
- Change `config.Docker.SkipPush` to `string`
- Respect `skip_publish` setting
- Respect `draft` setting
- Skip `docker push` if `skip_push` is set to `auto` and version is a pre-release

codecov-io: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/978?src=pr&el=h1) Report
> Merging [#978](https://codecov.io/gh/goreleaser/goreleaser/pull/978?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/3d110afc77fb01690b2699221c634b77fbb79c5a?src=pr&el=desc) will **decrease** coverage by `0.21%`.
> The diff coverage is `25%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/978/graphs/tree.svg?width=650&token=3bJvv6b7hN&height=150&src=pr)](https://codecov.io/gh/goreleaser/goreleaser/pull/978?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #978      +/-   ##
==========================================
- Coverage   88.27%   88.05%   -0.22%     
==========================================
  Files          52       52              
  Lines        2498     2503       +5     
==========================================
- Hits         2205     2204       -1     
- Misses        233      236       +3     
- Partials       60       63       +3
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/978?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/978/diff?src=pr&el=tree#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `100% <ø> (ø)` | :arrow_up: |
| [internal/pipe/docker/docker.go](https://codecov.io/gh/goreleaser/goreleaser/pull/978/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9kb2NrZXIvZG9ja2VyLmdv) | `93.14% <25%> (-3.33%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/978?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/978?src=pr&el=footer). Last update [3d110af...fb67c9b](https://codecov.io/gh/goreleaser/goreleaser/pull/978?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go
index 09559ea8..0e54d810 100644
--- a/internal/pipe/docker/docker.go
+++ b/internal/pipe/docker/docker.go
@@ -188,10 +188,18 @@ func process(ctx *context.Context, docker config.Docker, bins []artifact.Artifac
 	if err := dockerBuild(ctx, tmp, images, buildFlags); err != nil {
 		return err
 	}
-	if docker.SkipPush {
-		// TODO: this should also be better handled
-		log.Warn(pipe.Skip("skip_push is set").Error())
-		return nil
+
+	if strings.TrimSpace(docker.SkipPush) == "true" {
+		return pipe.Skip("docker.skip_push is set")
+	}
+	if ctx.SkipPublish {
+		return pipe.ErrSkipPublishEnabled
+	}
+	if ctx.Config.Release.Draft {
+		return pipe.Skip("release is marked as draft")
+	}
+	if strings.TrimSpace(docker.SkipPush) == "auto" && ctx.Semver.Prerelease != "" {
+		return pipe.Skip("prerelease detected with 'auto' push, skipping docker publish")
 	}
 	for _, img := range images {
 		ctx.Artifacts.Add(artifact.Artifact{
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 225ebf0d..0346c549 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -251,7 +251,7 @@ type Docker struct {
 	Image              string   `yaml:",omitempty"`
 	Dockerfile         string   `yaml:",omitempty"`
 	ImageTemplates     []string `yaml:"image_templates,omitempty"`
-	SkipPush           bool     `yaml:"skip_push,omitempty"`
+	SkipPush           string   `yaml:"skip_push,omitempty"`
 	TagTemplates       []string `yaml:"tag_templates,omitempty"`
 	Files              []string `yaml:"extra_files,omitempty"`
 	BuildFlagTemplates []string `yaml:"build_flag_templates,omitempty"`
diff --git a/www/content/docker.md b/www/content/docker.md
index b84f71fd..eec3547d 100644
--- a/www/content/docker.md
+++ b/www/content/docker.md
@@ -65,6 +65,8 @@ dockers:
     - "myuser/myimage:v{{ .Major }}"
     - "gcr.io/myuser/myimage:latest"
     # Skips the docker push. Could be useful if you also do draft releases.
+    # If set to auto, the release will not be pushed to the docker repository
+    # in case there is an indicator for prerelease in the tag e.g. v1.0.0-rc1
     # Defaults to false.
     skip_push: false
     # Path to the Dockerfile (from the project root).


Test patch:
diff --git a/internal/pipe/docker/docker_test.go b/internal/pipe/docker/docker_test.go
index 65013fbe..2f4a42fa 100644
--- a/internal/pipe/docker/docker_test.go
+++ b/internal/pipe/docker/docker_test.go
@@ -13,6 +13,7 @@ import (
 
 	"github.com/goreleaser/goreleaser/internal/artifact"
 	"github.com/goreleaser/goreleaser/internal/pipe"
+	"github.com/goreleaser/goreleaser/internal/testlib"
 	"github.com/goreleaser/goreleaser/pkg/config"
 	"github.com/goreleaser/goreleaser/pkg/context"
 	"github.com/stretchr/testify/assert"
@@ -223,15 +224,14 @@ func TestRunPipe(t *testing.T) {
 					Goarch:     "amd64",
 					Dockerfile: "testdata/Dockerfile",
 					Binaries:   []string{"mybin"},
-					SkipPush:   true,
+					SkipPush:   "true",
 				},
 			},
 			expect: []string{
 				registry + "goreleaser/test_run_pipe:latest",
 			},
 			assertImageLabels: noLabels,
-			assertError:       shouldNotErr,
-			pubAssertError:    shouldNotErr,
+			assertError:       testlib.AssertSkipped,
 		},
 		"valid_no_latest": {
 			dockers: []config.Docker{
@@ -384,7 +384,7 @@ func TestRunPipe(t *testing.T) {
 					Goarch:     "amd64",
 					Dockerfile: "testdata/Dockerfile",
 					Binaries:   []string{"mybin"},
-					SkipPush:   true,
+					SkipPush:   "true",
 				},
 			},
 			env: map[string]string{
@@ -395,8 +395,7 @@ func TestRunPipe(t *testing.T) {
 				registry + "goreleaser/mybin:latest",
 			},
 			assertImageLabels: noLabels,
-			assertError:       shouldNotErr,
-			pubAssertError:    shouldNotErr,
+			assertError:       testlib.AssertSkipped,
 		},
 		"no_permissions": {
 			dockers: []config.Docker{
@@ -768,7 +767,7 @@ func Test_processImageTemplates(t *testing.T) {
 						"gcr.io/image:{{.Tag}}-{{.Env.FOO}}",
 						"gcr.io/image:v{{.Major}}.{{.Minor}}",
 					},
-					SkipPush: true,
+					SkipPush: "true",
 				},
 			},
 		},
diff --git a/internal/pipe/publish/publish_test.go b/internal/pipe/publish/publish_test.go
index d214495f..9b610d24 100644
--- a/internal/pipe/publish/publish_test.go
+++ b/internal/pipe/publish/publish_test.go
@@ -23,7 +23,7 @@ func TestPublish(t *testing.T) {
 	var ctx = context.New(config.Project{})
 	ctx.Config.Release.Disable = true
 	for i := range ctx.Config.Dockers {
-		ctx.Config.Dockers[i].SkipPush = true
+		ctx.Config.Dockers[i].SkipPush = "true"
 	}
 	require.NoError(t, Pipe{}.Run(ctx))
 }