From d8ba9085385ef0086c14c7148abd0bc0954fc9b6 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 2 Nov 2017 15:53:28 +0800 Subject: [PATCH 01/12] keep only master branch for latest Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 39 +++++++++++++++------- docker.go | 26 ++++++++------- tags.go | 14 ++++++-- tags_test.go | 71 ++++++++++++++++++++++++++-------------- 4 files changed, 98 insertions(+), 52 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 3a1247f..55a2a10 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -192,6 +192,17 @@ func main() { Usage: "docker email", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, + cli.StringFlag{ + Name: "commit.branch", + Value: "master", + Usage: "git commit branch", + EnvVar: "DRONE_COMMIT_BRANCH", + }, + cli.StringFlag{ + Name: "default.branch", + Usage: "defualt latest branch", + EnvVar: "PLUGIN_DEFALUT_BRANCH", + }, } if err := app.Run(os.Args); err != nil { @@ -209,18 +220,20 @@ func run(c *cli.Context) error { Email: c.String("docker.email"), }, Build: docker.Build{ - Remote: c.String("remote.url"), - Name: c.String("commit.sha"), - Dockerfile: c.String("dockerfile"), - Context: c.String("context"), - Tags: c.StringSlice("tags"), - Args: c.StringSlice("args"), - ArgsEnv: c.StringSlice("args-from-env"), - Squash: c.Bool("squash"), - Pull: c.BoolT("pull-image"), - Compress: c.Bool("compress"), - Repo: c.String("repo"), - LabelSchema: c.StringSlice("label-schema"), + Remote: c.String("remote.url"), + Name: c.String("commit.sha"), + Branch: c.String("commit.branch"), + DefaultBranch: c.String("default.branch"), + Dockerfile: c.String("dockerfile"), + Context: c.String("context"), + Tags: c.StringSlice("tags"), + Args: c.StringSlice("args"), + ArgsEnv: c.StringSlice("args-from-env"), + Squash: c.Bool("squash"), + Pull: c.BoolT("pull-image"), + Compress: c.Bool("compress"), + Repo: c.String("repo"), + LabelSchema: c.StringSlice("label-schema"), }, Daemon: docker.Daemon{ Registry: c.String("docker.registry"), @@ -243,6 +256,8 @@ func run(c *cli.Context) error { plugin.Build.Tags = docker.DefaultTagSuffix( c.String("commit.ref"), c.String("tags.suffix"), + c.String("commit.branch"), + c.String("default.branch"), ) } diff --git a/docker.go b/docker.go index e7ca7e0..2f4728a 100644 --- a/docker.go +++ b/docker.go @@ -37,18 +37,20 @@ type ( // Build defines Docker build parameters. Build struct { - Remote string // Git remote URL - Name string // Docker build using default named tag - Dockerfile string // Docker build Dockerfile - Context string // Docker build context - Tags []string // Docker build tags - Args []string // Docker build args - ArgsEnv []string // Docker build args from env - Squash bool // Docker build squash - Pull bool // Docker build pull - Compress bool // Docker build compress - Repo string // Docker build repository - LabelSchema []string // Label schema map + Remote string // Git remote URL + Name string // Docker build using default named tag + Dockerfile string // Docker build Dockerfile + Context string // Docker build context + Tags []string // Docker build tags + Args []string // Docker build args + ArgsEnv []string // Docker build args from env + Squash bool // Docker build squash + Pull bool // Docker build pull + Compress bool // Docker build compress + Repo string // Docker build repository + LabelSchema []string // Label schema map + Branch string // Docker build branch + DefaultBranch string // Docker latest branch } // Plugin defines the Docker plugin parameters. diff --git a/tags.go b/tags.go index 5e1b36e..5482b53 100644 --- a/tags.go +++ b/tags.go @@ -9,8 +9,8 @@ import ( // DefaultTagSuffix returns a set of default suggested tags // based on the commit ref with an attached suffix. -func DefaultTagSuffix(ref, suffix string) []string { - tags := DefaultTags(ref) +func DefaultTagSuffix(ref, suffix, commitBranch, defaultBranch string) []string { + tags := DefaultTags(ref, commitBranch, defaultBranch) if len(suffix) == 0 { return tags } @@ -26,10 +26,18 @@ func DefaultTagSuffix(ref, suffix string) []string { // DefaultTags returns a set of default suggested tags based on // the commit ref. -func DefaultTags(ref string) []string { +func DefaultTags(ref, commitBranch, defaultBranch string) []string { + + if defaultBranch != "" && + commitBranch != defaultBranch && + !strings.HasPrefix(ref, "refs/tags/") { + return []string{} + } + if !strings.HasPrefix(ref, "refs/tags/") { return []string{"latest"} } + v := stripTagPrefix(ref) version, err := semver.NewVersion(v) if err != nil { diff --git a/tags_test.go b/tags_test.go index 9d356f8..f86d4ca 100644 --- a/tags_test.go +++ b/tags_test.go @@ -25,23 +25,30 @@ func Test_stripTagPrefix(t *testing.T) { func TestDefaultTags(t *testing.T) { var tests = []struct { - Before string - After []string + Before string + CommitBranch string + DefaultBranch string + After []string }{ - {"", []string{"latest"}}, - {"refs/heads/master", []string{"latest"}}, - {"refs/tags/0.9.0", []string{"0.9", "0.9.0"}}, - {"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}}, + {"", "master", "", []string{"latest"}}, + {"refs/heads/master", "master", "", []string{"latest"}}, + {"refs/tags/0.9.0", "master", "", []string{"0.9", "0.9.0"}}, + {"refs/tags/1.0.0", "master", "", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0", "master", "", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0-alpha.1", "master", "", []string{"1.0.0-alpha.1"}}, // malformed or errors - {"refs/tags/x1.0.0", []string{"latest"}}, - {"v1.0.0", []string{"latest"}}, + {"refs/tags/x1.0.0", "master", "", []string{"latest"}}, + {"v1.0.0", "master", "", []string{"latest"}}, + + // defualt branch + {"refs/heads/master", "master", "master", []string{"latest"}}, + {"refs/heads/test", "test", "master", []string{}}, + {"refs/tags/v1.0.0", "v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, } for _, test := range tests { - got, want := DefaultTags(test.Before), test.After + got, want := DefaultTags(test.Before, test.CommitBranch, test.DefaultBranch), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } @@ -50,16 +57,22 @@ func TestDefaultTags(t *testing.T) { func TestDefaultTagSuffix(t *testing.T) { var tests = []struct { - Before string - Suffix string - After []string + Before string + CommitBranch string + DefaultBranch string + Suffix string + After []string }{ // without suffix { - After: []string{"latest"}, + After: []string{"latest"}, + CommitBranch: "master", + DefaultBranch: "", }, { - Before: "refs/tags/v1.0.0", + Before: "refs/tags/v1.0.0", + CommitBranch: "master", + DefaultBranch: "", After: []string{ "1", "1.0", @@ -68,12 +81,16 @@ func TestDefaultTagSuffix(t *testing.T) { }, // with suffix { - Suffix: "linux-amd64", - After: []string{"linux-amd64"}, + CommitBranch: "master", + DefaultBranch: "", + Suffix: "linux-amd64", + After: []string{"linux-amd64"}, }, { - Before: "refs/tags/v1.0.0", - Suffix: "linux-amd64", + CommitBranch: "master", + DefaultBranch: "", + Before: "refs/tags/v1.0.0", + Suffix: "linux-amd64", After: []string{ "1-linux-amd64", "1.0-linux-amd64", @@ -81,12 +98,16 @@ func TestDefaultTagSuffix(t *testing.T) { }, }, { - Suffix: "nanoserver", - After: []string{"nanoserver"}, + CommitBranch: "master", + DefaultBranch: "", + Suffix: "nanoserver", + After: []string{"nanoserver"}, }, { - Before: "refs/tags/v1.9.2", - Suffix: "nanoserver", + CommitBranch: "master", + DefaultBranch: "", + Before: "refs/tags/v1.9.2", + Suffix: "nanoserver", After: []string{ "1-nanoserver", "1.9-nanoserver", @@ -96,7 +117,7 @@ func TestDefaultTagSuffix(t *testing.T) { } for _, test := range tests { - got, want := DefaultTagSuffix(test.Before, test.Suffix), test.After + got, want := DefaultTagSuffix(test.Before, test.Suffix, test.CommitBranch, test.DefaultBranch), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } From 0fb22dbadcf6ba9736fa724df46bdff25b9eb6f6 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 3 Nov 2017 09:33:37 +0800 Subject: [PATCH 02/12] add DRONE_ environment variable Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 55a2a10..5da0ded 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -201,7 +201,7 @@ func main() { cli.StringFlag{ Name: "default.branch", Usage: "defualt latest branch", - EnvVar: "PLUGIN_DEFALUT_BRANCH", + EnvVar: "DRONE_REPO_BRANCH,PLUGIN_DEFALUT_BRANCH", }, } From 197b737685de70e38765282e8e5b713d0d0ce3b8 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 3 Nov 2017 09:49:07 +0800 Subject: [PATCH 03/12] remove additional yaml attributes Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 5da0ded..d88cd0c 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -200,8 +200,8 @@ func main() { }, cli.StringFlag{ Name: "default.branch", - Usage: "defualt latest branch", - EnvVar: "DRONE_REPO_BRANCH,PLUGIN_DEFALUT_BRANCH", + Usage: "defualt repository branch", + EnvVar: "DRONE_REPO_BRANCH", }, } From 3c1cfa628f71d0847e1b9a50f1ce96e6b4ca42ba Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 6 Nov 2017 15:37:19 +0800 Subject: [PATCH 04/12] update Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 2 +- tags.go | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index d88cd0c..b51cb8a 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -200,7 +200,7 @@ func main() { }, cli.StringFlag{ Name: "default.branch", - Usage: "defualt repository branch", + Usage: "repository default branch", EnvVar: "DRONE_REPO_BRANCH", }, } diff --git a/tags.go b/tags.go index 5482b53..f6b7606 100644 --- a/tags.go +++ b/tags.go @@ -28,9 +28,7 @@ func DefaultTagSuffix(ref, suffix, commitBranch, defaultBranch string) []string // the commit ref. func DefaultTags(ref, commitBranch, defaultBranch string) []string { - if defaultBranch != "" && - commitBranch != defaultBranch && - !strings.HasPrefix(ref, "refs/tags/") { + if defaultBranch != "" && commitBranch != defaultBranch && !strings.HasPrefix(ref, "refs/tags/") { return []string{} } From 0927e34a03293409b1de12926974fd125083e7ce Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 10:05:50 +0800 Subject: [PATCH 05/12] replace commit branch with refs Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 14 +++------- tags.go | 17 ++++++++---- tags_test.go | 56 +++++++++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index b51cb8a..a142cc4 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -193,13 +193,7 @@ func main() { EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, cli.StringFlag{ - Name: "commit.branch", - Value: "master", - Usage: "git commit branch", - EnvVar: "DRONE_COMMIT_BRANCH", - }, - cli.StringFlag{ - Name: "default.branch", + Name: "repo.branch", Usage: "repository default branch", EnvVar: "DRONE_REPO_BRANCH", }, @@ -222,8 +216,7 @@ func run(c *cli.Context) error { Build: docker.Build{ Remote: c.String("remote.url"), Name: c.String("commit.sha"), - Branch: c.String("commit.branch"), - DefaultBranch: c.String("default.branch"), + DefaultBranch: c.String("repo.branch"), Dockerfile: c.String("dockerfile"), Context: c.String("context"), Tags: c.StringSlice("tags"), @@ -256,8 +249,7 @@ func run(c *cli.Context) error { plugin.Build.Tags = docker.DefaultTagSuffix( c.String("commit.ref"), c.String("tags.suffix"), - c.String("commit.branch"), - c.String("default.branch"), + c.String("repo.branch"), ) } diff --git a/tags.go b/tags.go index f6b7606..d83821a 100644 --- a/tags.go +++ b/tags.go @@ -9,8 +9,8 @@ import ( // DefaultTagSuffix returns a set of default suggested tags // based on the commit ref with an attached suffix. -func DefaultTagSuffix(ref, suffix, commitBranch, defaultBranch string) []string { - tags := DefaultTags(ref, commitBranch, defaultBranch) +func DefaultTagSuffix(ref, suffix, defaultBranch string) []string { + tags := DefaultTags(ref, defaultBranch) if len(suffix) == 0 { return tags } @@ -26,10 +26,12 @@ func DefaultTagSuffix(ref, suffix, commitBranch, defaultBranch string) []string // DefaultTags returns a set of default suggested tags based on // the commit ref. -func DefaultTags(ref, commitBranch, defaultBranch string) []string { +func DefaultTags(ref, defaultBranch string) []string { - if defaultBranch != "" && commitBranch != defaultBranch && !strings.HasPrefix(ref, "refs/tags/") { - return []string{} + if defaultBranch != "" && strings.HasPrefix(ref, "refs/heads/") { + if stripHeadPrefix(ref) != defaultBranch { + return []string{} + } } if !strings.HasPrefix(ref, "refs/tags/") { @@ -64,3 +66,8 @@ func stripTagPrefix(ref string) string { ref = strings.TrimPrefix(ref, "v") return ref } + +func stripHeadPrefix(ref string) string { + ref = strings.TrimPrefix(ref, "refs/heads/") + return ref +} diff --git a/tags_test.go b/tags_test.go index f86d4ca..30732a5 100644 --- a/tags_test.go +++ b/tags_test.go @@ -26,29 +26,28 @@ func Test_stripTagPrefix(t *testing.T) { func TestDefaultTags(t *testing.T) { var tests = []struct { Before string - CommitBranch string DefaultBranch string After []string }{ - {"", "master", "", []string{"latest"}}, - {"refs/heads/master", "master", "", []string{"latest"}}, - {"refs/tags/0.9.0", "master", "", []string{"0.9", "0.9.0"}}, - {"refs/tags/1.0.0", "master", "", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0", "master", "", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0-alpha.1", "master", "", []string{"1.0.0-alpha.1"}}, + {"", "master", []string{"latest"}}, + {"refs/heads/master", "master", []string{"latest"}}, + {"refs/tags/0.9.0", "master", []string{"0.9", "0.9.0"}}, + {"refs/tags/1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0-alpha.1", "master", []string{"1.0.0-alpha.1"}}, // malformed or errors - {"refs/tags/x1.0.0", "master", "", []string{"latest"}}, - {"v1.0.0", "master", "", []string{"latest"}}, + {"refs/tags/x1.0.0", "master", []string{"latest"}}, + {"v1.0.0", "master", []string{"latest"}}, // defualt branch - {"refs/heads/master", "master", "master", []string{"latest"}}, - {"refs/heads/test", "test", "master", []string{}}, - {"refs/tags/v1.0.0", "v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, + {"refs/heads/master", "master", []string{"latest"}}, + {"refs/heads/test", "master", []string{}}, + {"refs/tags/v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, } for _, test := range tests { - got, want := DefaultTags(test.Before, test.CommitBranch, test.DefaultBranch), test.After + got, want := DefaultTags(test.Before, test.DefaultBranch), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } @@ -58,7 +57,6 @@ func TestDefaultTags(t *testing.T) { func TestDefaultTagSuffix(t *testing.T) { var tests = []struct { Before string - CommitBranch string DefaultBranch string Suffix string After []string @@ -66,12 +64,10 @@ func TestDefaultTagSuffix(t *testing.T) { // without suffix { After: []string{"latest"}, - CommitBranch: "master", DefaultBranch: "", }, { Before: "refs/tags/v1.0.0", - CommitBranch: "master", DefaultBranch: "", After: []string{ "1", @@ -81,13 +77,11 @@ func TestDefaultTagSuffix(t *testing.T) { }, // with suffix { - CommitBranch: "master", DefaultBranch: "", Suffix: "linux-amd64", After: []string{"linux-amd64"}, }, { - CommitBranch: "master", DefaultBranch: "", Before: "refs/tags/v1.0.0", Suffix: "linux-amd64", @@ -98,13 +92,11 @@ func TestDefaultTagSuffix(t *testing.T) { }, }, { - CommitBranch: "master", DefaultBranch: "", Suffix: "nanoserver", After: []string{"nanoserver"}, }, { - CommitBranch: "master", DefaultBranch: "", Before: "refs/tags/v1.9.2", Suffix: "nanoserver", @@ -117,9 +109,31 @@ func TestDefaultTagSuffix(t *testing.T) { } for _, test := range tests { - got, want := DefaultTagSuffix(test.Before, test.Suffix, test.CommitBranch, test.DefaultBranch), test.After + got, want := DefaultTagSuffix(test.Before, test.Suffix, test.DefaultBranch), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } } } + +func Test_stripHeadPrefix(t *testing.T) { + type args struct { + ref string + } + tests := []struct { + args args + want string + }{ + { + args: args{ + ref: "refs/heads/master", + }, + want: "master", + }, + } + for _, tt := range tests { + if got := stripHeadPrefix(tt.args.ref); got != tt.want { + t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want) + } + } +} From ff05dfd6dcc3f8a4276b607c0e3f7c6c15f624e5 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 10:55:44 +0800 Subject: [PATCH 06/12] reverse the orderin Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 2 +- tags.go | 2 +- tags_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index a142cc4..b993bb9 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -248,8 +248,8 @@ func run(c *cli.Context) error { if c.Bool("tags.auto") { plugin.Build.Tags = docker.DefaultTagSuffix( c.String("commit.ref"), - c.String("tags.suffix"), c.String("repo.branch"), + c.String("tags.suffix"), ) } diff --git a/tags.go b/tags.go index d83821a..5f33e95 100644 --- a/tags.go +++ b/tags.go @@ -9,7 +9,7 @@ import ( // DefaultTagSuffix returns a set of default suggested tags // based on the commit ref with an attached suffix. -func DefaultTagSuffix(ref, suffix, defaultBranch string) []string { +func DefaultTagSuffix(ref, defaultBranch, suffix string) []string { tags := DefaultTags(ref, defaultBranch) if len(suffix) == 0 { return tags diff --git a/tags_test.go b/tags_test.go index 30732a5..709b6ba 100644 --- a/tags_test.go +++ b/tags_test.go @@ -109,7 +109,7 @@ func TestDefaultTagSuffix(t *testing.T) { } for _, test := range tests { - got, want := DefaultTagSuffix(test.Before, test.Suffix, test.DefaultBranch), test.After + got, want := DefaultTagSuffix(test.Before, test.DefaultBranch, test.Suffix), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } From c591da7e86eb36cded8d7839382d312350b87676 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 11:11:22 +0800 Subject: [PATCH 07/12] remove commit branch frone struct. Signed-off-by: Bo-Yi Wu --- docker.go | 1 - 1 file changed, 1 deletion(-) diff --git a/docker.go b/docker.go index 2f4728a..b3d0b77 100644 --- a/docker.go +++ b/docker.go @@ -49,7 +49,6 @@ type ( Compress bool // Docker build compress Repo string // Docker build repository LabelSchema []string // Label schema map - Branch string // Docker build branch DefaultBranch string // Docker latest branch } From 5fa26ebb5f1b751edbe972b16c3ab8757ff2eeff Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 13:24:35 +0800 Subject: [PATCH 08/12] UseDefaultTag for skip build if not default branch. Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 14 +++++-- docker.go | 18 +++++++++ docker_test.go | 25 ++++++++++++ tags.go | 19 ++------- tags_test.go | 85 ++++++++++++---------------------------- 5 files changed, 82 insertions(+), 79 deletions(-) create mode 100644 docker_test.go diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index b993bb9..0efd4d8 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -246,11 +246,19 @@ func run(c *cli.Context) error { } if c.Bool("tags.auto") { - plugin.Build.Tags = docker.DefaultTagSuffix( + if docker.UseDefaultTag( // return true if not default branch, or not tag c.String("commit.ref"), c.String("repo.branch"), - c.String("tags.suffix"), - ) + ) { + plugin.Build.Tags = docker.DefaultTagSuffix( + c.String("commit.ref"), + c.String("tags.suffix"), + ) + } else { + logrus.Printf("skipping automated docker build for %s", c.String("commit.ref")) + + return nil + } } return plugin.Exec() diff --git a/docker.go b/docker.go index b3d0b77..908268c 100644 --- a/docker.go +++ b/docker.go @@ -61,6 +61,24 @@ type ( } ) +func stripHeadPrefix(ref string) string { + ref = strings.TrimPrefix(ref, "refs/heads/") + return ref +} + +// UseDefaultTag for keep only default branch for latest tag +func UseDefaultTag(ref, defaultBranch string) bool { + if strings.HasPrefix(ref, "refs/tags/") { + return true + } + + if stripHeadPrefix(ref) == defaultBranch { + return true + } + + return false +} + // Exec executes the plugin step func (p Plugin) Exec() error { // start the Docker daemon server diff --git a/docker_test.go b/docker_test.go new file mode 100644 index 0000000..ffd1cf2 --- /dev/null +++ b/docker_test.go @@ -0,0 +1,25 @@ +package docker + +import "testing" + +func Test_stripHeadPrefix(t *testing.T) { + type args struct { + ref string + } + tests := []struct { + args args + want string + }{ + { + args: args{ + ref: "refs/heads/master", + }, + want: "master", + }, + } + for _, tt := range tests { + if got := stripHeadPrefix(tt.args.ref); got != tt.want { + t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want) + } + } +} diff --git a/tags.go b/tags.go index 5f33e95..5e1b36e 100644 --- a/tags.go +++ b/tags.go @@ -9,8 +9,8 @@ import ( // DefaultTagSuffix returns a set of default suggested tags // based on the commit ref with an attached suffix. -func DefaultTagSuffix(ref, defaultBranch, suffix string) []string { - tags := DefaultTags(ref, defaultBranch) +func DefaultTagSuffix(ref, suffix string) []string { + tags := DefaultTags(ref) if len(suffix) == 0 { return tags } @@ -26,18 +26,10 @@ func DefaultTagSuffix(ref, defaultBranch, suffix string) []string { // DefaultTags returns a set of default suggested tags based on // the commit ref. -func DefaultTags(ref, defaultBranch string) []string { - - if defaultBranch != "" && strings.HasPrefix(ref, "refs/heads/") { - if stripHeadPrefix(ref) != defaultBranch { - return []string{} - } - } - +func DefaultTags(ref string) []string { if !strings.HasPrefix(ref, "refs/tags/") { return []string{"latest"} } - v := stripTagPrefix(ref) version, err := semver.NewVersion(v) if err != nil { @@ -66,8 +58,3 @@ func stripTagPrefix(ref string) string { ref = strings.TrimPrefix(ref, "v") return ref } - -func stripHeadPrefix(ref string) string { - ref = strings.TrimPrefix(ref, "refs/heads/") - return ref -} diff --git a/tags_test.go b/tags_test.go index 709b6ba..9d356f8 100644 --- a/tags_test.go +++ b/tags_test.go @@ -25,29 +25,23 @@ func Test_stripTagPrefix(t *testing.T) { func TestDefaultTags(t *testing.T) { var tests = []struct { - Before string - DefaultBranch string - After []string + Before string + After []string }{ - {"", "master", []string{"latest"}}, - {"refs/heads/master", "master", []string{"latest"}}, - {"refs/tags/0.9.0", "master", []string{"0.9", "0.9.0"}}, - {"refs/tags/1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0-alpha.1", "master", []string{"1.0.0-alpha.1"}}, + {"", []string{"latest"}}, + {"refs/heads/master", []string{"latest"}}, + {"refs/tags/0.9.0", []string{"0.9", "0.9.0"}}, + {"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}}, // malformed or errors - {"refs/tags/x1.0.0", "master", []string{"latest"}}, - {"v1.0.0", "master", []string{"latest"}}, - - // defualt branch - {"refs/heads/master", "master", []string{"latest"}}, - {"refs/heads/test", "master", []string{}}, - {"refs/tags/v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/x1.0.0", []string{"latest"}}, + {"v1.0.0", []string{"latest"}}, } for _, test := range tests { - got, want := DefaultTags(test.Before, test.DefaultBranch), test.After + got, want := DefaultTags(test.Before), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } @@ -56,19 +50,16 @@ func TestDefaultTags(t *testing.T) { func TestDefaultTagSuffix(t *testing.T) { var tests = []struct { - Before string - DefaultBranch string - Suffix string - After []string + Before string + Suffix string + After []string }{ // without suffix { - After: []string{"latest"}, - DefaultBranch: "", + After: []string{"latest"}, }, { - Before: "refs/tags/v1.0.0", - DefaultBranch: "", + Before: "refs/tags/v1.0.0", After: []string{ "1", "1.0", @@ -77,14 +68,12 @@ func TestDefaultTagSuffix(t *testing.T) { }, // with suffix { - DefaultBranch: "", - Suffix: "linux-amd64", - After: []string{"linux-amd64"}, + Suffix: "linux-amd64", + After: []string{"linux-amd64"}, }, { - DefaultBranch: "", - Before: "refs/tags/v1.0.0", - Suffix: "linux-amd64", + Before: "refs/tags/v1.0.0", + Suffix: "linux-amd64", After: []string{ "1-linux-amd64", "1.0-linux-amd64", @@ -92,14 +81,12 @@ func TestDefaultTagSuffix(t *testing.T) { }, }, { - DefaultBranch: "", - Suffix: "nanoserver", - After: []string{"nanoserver"}, + Suffix: "nanoserver", + After: []string{"nanoserver"}, }, { - DefaultBranch: "", - Before: "refs/tags/v1.9.2", - Suffix: "nanoserver", + Before: "refs/tags/v1.9.2", + Suffix: "nanoserver", After: []string{ "1-nanoserver", "1.9-nanoserver", @@ -109,31 +96,9 @@ func TestDefaultTagSuffix(t *testing.T) { } for _, test := range tests { - got, want := DefaultTagSuffix(test.Before, test.DefaultBranch, test.Suffix), test.After + got, want := DefaultTagSuffix(test.Before, test.Suffix), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } } } - -func Test_stripHeadPrefix(t *testing.T) { - type args struct { - ref string - } - tests := []struct { - args args - want string - }{ - { - args: args{ - ref: "refs/heads/master", - }, - want: "master", - }, - } - for _, tt := range tests { - if got := stripHeadPrefix(tt.args.ref); got != tt.want { - t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want) - } - } -} From 13ce200771e345ede8f007d7993a1b18acf97ebf Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 13:26:07 +0800 Subject: [PATCH 09/12] remove DefaultBranch Signed-off-by: Bo-Yi Wu --- docker.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/docker.go b/docker.go index 908268c..30faeeb 100644 --- a/docker.go +++ b/docker.go @@ -37,19 +37,18 @@ type ( // Build defines Docker build parameters. Build struct { - Remote string // Git remote URL - Name string // Docker build using default named tag - Dockerfile string // Docker build Dockerfile - Context string // Docker build context - Tags []string // Docker build tags - Args []string // Docker build args - ArgsEnv []string // Docker build args from env - Squash bool // Docker build squash - Pull bool // Docker build pull - Compress bool // Docker build compress - Repo string // Docker build repository - LabelSchema []string // Label schema map - DefaultBranch string // Docker latest branch + Remote string // Git remote URL + Name string // Docker build using default named tag + Dockerfile string // Docker build Dockerfile + Context string // Docker build context + Tags []string // Docker build tags + Args []string // Docker build args + ArgsEnv []string // Docker build args from env + Squash bool // Docker build squash + Pull bool // Docker build pull + Compress bool // Docker build compress + Repo string // Docker build repository + LabelSchema []string // Label schema map } // Plugin defines the Docker plugin parameters. From ce53e219897f9bc2efae7054645cc3c51ef0fa34 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 13:30:33 +0800 Subject: [PATCH 10/12] add unit test. Signed-off-by: Bo-Yi Wu --- docker_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docker_test.go b/docker_test.go index ffd1cf2..7781593 100644 --- a/docker_test.go +++ b/docker_test.go @@ -23,3 +23,45 @@ func Test_stripHeadPrefix(t *testing.T) { } } } + +func TestUseDefaultTag(t *testing.T) { + type args struct { + ref string + defaultBranch string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "latest tag for default branch", + args: args{ + ref: "refs/heads/master", + defaultBranch: "master", + }, + want: true, + }, + { + name: "build from tags", + args: args{ + ref: "refs/tags/v1.0.0", + defaultBranch: "master", + }, + want: true, + }, + { + name: "skip build for not default branch", + args: args{ + ref: "refs/heads/develop", + defaultBranch: "master", + }, + want: false, + }, + } + for _, tt := range tests { + if got := UseDefaultTag(tt.args.ref, tt.args.defaultBranch); got != tt.want { + t.Errorf("%q. UseDefaultTag() = %v, want %v", tt.name, got, tt.want) + } + } +} From a5011aab392921b10e296437b0cd14d1d9d42fec Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 13:31:36 +0800 Subject: [PATCH 11/12] remove default branch Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 0efd4d8..3d19d0d 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -214,19 +214,18 @@ func run(c *cli.Context) error { Email: c.String("docker.email"), }, Build: docker.Build{ - Remote: c.String("remote.url"), - Name: c.String("commit.sha"), - DefaultBranch: c.String("repo.branch"), - Dockerfile: c.String("dockerfile"), - Context: c.String("context"), - Tags: c.StringSlice("tags"), - Args: c.StringSlice("args"), - ArgsEnv: c.StringSlice("args-from-env"), - Squash: c.Bool("squash"), - Pull: c.BoolT("pull-image"), - Compress: c.Bool("compress"), - Repo: c.String("repo"), - LabelSchema: c.StringSlice("label-schema"), + Remote: c.String("remote.url"), + Name: c.String("commit.sha"), + Dockerfile: c.String("dockerfile"), + Context: c.String("context"), + Tags: c.StringSlice("tags"), + Args: c.StringSlice("args"), + ArgsEnv: c.StringSlice("args-from-env"), + Squash: c.Bool("squash"), + Pull: c.BoolT("pull-image"), + Compress: c.Bool("compress"), + Repo: c.String("repo"), + LabelSchema: c.StringSlice("label-schema"), }, Daemon: docker.Daemon{ Registry: c.String("docker.registry"), From bb681102e98cda255c09574496bc58eb9692203f Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 13:33:18 +0800 Subject: [PATCH 12/12] update comment Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 3d19d0d..a7462b2 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -245,7 +245,7 @@ func run(c *cli.Context) error { } if c.Bool("tags.auto") { - if docker.UseDefaultTag( // return true if not default branch, or not tag + if docker.UseDefaultTag( // return true if tag event or default branch c.String("commit.ref"), c.String("repo.branch"), ) {