UseDefaultTag for skip build if not default branch.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-11-09 13:24:35 +08:00
parent c591da7e86
commit 5fa26ebb5f
5 changed files with 82 additions and 79 deletions

View File

@ -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"),
) {
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()

View File

@ -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

25
docker_test.go Normal file
View File

@ -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)
}
}
}

19
tags.go
View File

@ -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
}

View File

@ -26,28 +26,22 @@ func Test_stripTagPrefix(t *testing.T) {
func TestDefaultTags(t *testing.T) {
var tests = []struct {
Before 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"}},
{"", []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)
}
@ -57,18 +51,15 @@ func TestDefaultTags(t *testing.T) {
func TestDefaultTagSuffix(t *testing.T) {
var tests = []struct {
Before string
DefaultBranch string
Suffix string
After []string
}{
// without suffix
{
After: []string{"latest"},
DefaultBranch: "",
},
{
Before: "refs/tags/v1.0.0",
DefaultBranch: "",
After: []string{
"1",
"1.0",
@ -77,12 +68,10 @@ func TestDefaultTagSuffix(t *testing.T) {
},
// with suffix
{
DefaultBranch: "",
Suffix: "linux-amd64",
After: []string{"linux-amd64"},
},
{
DefaultBranch: "",
Before: "refs/tags/v1.0.0",
Suffix: "linux-amd64",
After: []string{
@ -92,12 +81,10 @@ func TestDefaultTagSuffix(t *testing.T) {
},
},
{
DefaultBranch: "",
Suffix: "nanoserver",
After: []string{"nanoserver"},
},
{
DefaultBranch: "",
Before: "refs/tags/v1.9.2",
Suffix: "nanoserver",
After: []string{
@ -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)
}
}
}