0
0
mirror of https://github.com/thegeeklab/wp-docker-buildx.git synced 2024-11-22 20:20:39 +00:00

Merge pull request #279 from drone-plugins/feature/semver-fail

semver parsing fails pipeline step
This commit is contained in:
Brad Rydzewski 2020-06-08 14:57:13 -04:00 committed by GitHub
commit d913d6e7dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 17 deletions

View File

@ -298,10 +298,15 @@ func run(c *cli.Context) error {
c.String("commit.ref"), c.String("commit.ref"),
c.String("repo.branch"), c.String("repo.branch"),
) { ) {
plugin.Build.Tags = docker.DefaultTagSuffix( tag, err := docker.DefaultTagSuffix(
c.String("commit.ref"), c.String("commit.ref"),
c.String("tags.suffix"), c.String("tags.suffix"),
) )
if err != nil {
logrus.Printf("cannot build docker image for %s, invalid semantic version", c.String("commit.ref"))
return err
}
plugin.Build.Tags = tag
} else { } else {
logrus.Printf("skipping automated docker build for %s", c.String("commit.ref")) logrus.Printf("skipping automated docker build for %s", c.String("commit.ref"))
return nil return nil

23
tags.go
View File

@ -9,10 +9,13 @@ import (
// DefaultTagSuffix returns a set of default suggested tags // DefaultTagSuffix returns a set of default suggested tags
// based on the commit ref with an attached suffix. // based on the commit ref with an attached suffix.
func DefaultTagSuffix(ref, suffix string) []string { func DefaultTagSuffix(ref, suffix string) ([]string, error) {
tags := DefaultTags(ref) tags, err := DefaultTags(ref)
if err != nil {
return nil, err
}
if len(suffix) == 0 { if len(suffix) == 0 {
return tags return tags, nil
} }
for i, tag := range tags { for i, tag := range tags {
if tag == "latest" { if tag == "latest" {
@ -21,7 +24,7 @@ func DefaultTagSuffix(ref, suffix string) []string {
tags[i] = fmt.Sprintf("%s-%s", tag, suffix) tags[i] = fmt.Sprintf("%s-%s", tag, suffix)
} }
} }
return tags return tags, nil
} }
func splitOff(input string, delim string) string { func splitOff(input string, delim string) string {
@ -36,19 +39,19 @@ func splitOff(input string, delim string) string {
// DefaultTags returns a set of default suggested tags based on // DefaultTags returns a set of default suggested tags based on
// the commit ref. // the commit ref.
func DefaultTags(ref string) []string { func DefaultTags(ref string) ([]string, error) {
if !strings.HasPrefix(ref, "refs/tags/") { if !strings.HasPrefix(ref, "refs/tags/") {
return []string{"latest"} return []string{"latest"}, nil
} }
v := stripTagPrefix(ref) v := stripTagPrefix(ref)
version, err := semver.NewVersion(v) version, err := semver.NewVersion(v)
if err != nil { if err != nil {
return []string{"latest"} return []string{"latest"}, err
} }
if version.PreRelease != "" || version.Metadata != "" { if version.PreRelease != "" || version.Metadata != "" {
return []string{ return []string{
version.String(), version.String(),
} }, nil
} }
v = stripTagPrefix(ref) v = stripTagPrefix(ref)
@ -59,13 +62,13 @@ func DefaultTags(ref string) []string {
return []string{ return []string{
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor), fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch), fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
} }, nil
} }
return []string{ return []string{
fmt.Sprintf("%0*d", len(dotParts[0]), version.Major), fmt.Sprintf("%0*d", len(dotParts[0]), version.Major),
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor), fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch), fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
} }, nil
} }
// UseDefaultTag for keep only default branch for latest tag // UseDefaultTag for keep only default branch for latest tag

View File

@ -34,20 +34,35 @@ func TestDefaultTags(t *testing.T) {
{"refs/tags/1.0.0", []string{"1", "1.0", "1.0.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", []string{"1", "1.0", "1.0.0"}},
{"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}}, {"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}},
// malformed or errors
{"refs/tags/x1.0.0", []string{"latest"}},
{"v1.0.0", []string{"latest"}},
} }
for _, test := range tests { for _, test := range tests {
got, want := DefaultTags(test.Before), test.After tags, err := DefaultTags(test.Before)
if err != nil {
t.Error(err)
continue
}
got, want := tags, test.After
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("Got tag %v, want %v", got, want) t.Errorf("Got tag %v, want %v", got, want)
} }
} }
} }
func TestDefaultTagsError(t *testing.T) {
var tests = []string{
"refs/tags/x1.0.0",
"refs/tags/20190203",
}
for _, test := range tests {
_, err := DefaultTags(test)
if err == nil {
t.Errorf("Expect tag error for %s", test)
}
}
}
func TestDefaultTagSuffix(t *testing.T) { func TestDefaultTagSuffix(t *testing.T) {
var tests = []struct { var tests = []struct {
Before string Before string
@ -105,7 +120,12 @@ func TestDefaultTagSuffix(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
got, want := DefaultTagSuffix(test.Before, test.Suffix), test.After tag, err := DefaultTagSuffix(test.Before, test.Suffix)
if err != nil {
t.Error(err)
continue
}
got, want := tag, test.After
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("Got tag %v, want %v", got, want) t.Errorf("Got tag %v, want %v", got, want)
} }