add defualt tag suffix

This commit is contained in:
Brad Rydzewski 2017-10-31 08:49:56 -07:00
parent 6f5d6e2481
commit 88ae029815
3 changed files with 81 additions and 3 deletions

View File

@ -131,6 +131,11 @@ func main() {
Usage: "default build tags",
EnvVar: "PLUGIN_DEFAULT_TAGS,PLUGIN_AUTO_TAG",
},
cli.StringFlag{
Name: "tags.suffix",
Usage: "default build tags with suffix",
EnvVar: "PLUGIN_DEFAULT_SUFFIX,PLUGIN_AUTO_TAG_SUFFIX",
},
cli.StringSliceFlag{
Name: "args",
Usage: "build args",
@ -235,8 +240,9 @@ func run(c *cli.Context) error {
}
if c.Bool("tags.auto") {
plugin.Build.Tags = docker.DefaultTags(
plugin.Build.Tags = docker.DefaultTagSuffix(
c.String("commit.ref"),
c.String("tags.suffix"),
)
}

19
tags.go
View File

@ -7,7 +7,24 @@ import (
"github.com/coreos/go-semver/semver"
)
// Default tags returns a set of default suggested tags based on
// 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)
if len(suffix) == 0 {
return tags
}
for i, tag := range tags {
if tag == "latest" {
tags[i] = suffix
} else {
tags[i] = fmt.Sprintf("%s-%s", tag, suffix)
}
}
return tags
}
// DefaultTags returns a set of default suggested tags based on
// the commit ref.
func DefaultTags(ref string) []string {
if !strings.HasPrefix(ref, "refs/tags/") {

View File

@ -23,7 +23,7 @@ func Test_stripTagPrefix(t *testing.T) {
}
}
func Test_defaultTags(t *testing.T) {
func TestDefaultTags(t *testing.T) {
var tests = []struct {
Before string
After []string
@ -47,3 +47,58 @@ func Test_defaultTags(t *testing.T) {
}
}
}
func TestDefaultTagSuffix(t *testing.T) {
var tests = []struct {
Before string
Suffix string
After []string
}{
// without suffix
{
After: []string{"latest"},
},
{
Before: "refs/tags/v1.0.0",
After: []string{
"1",
"1.0",
"1.0.0",
},
},
// with suffix
{
Suffix: "linux-amd64",
After: []string{"linux-amd64"},
},
{
Before: "refs/tags/v1.0.0",
Suffix: "linux-amd64",
After: []string{
"1-linux-amd64",
"1.0-linux-amd64",
"1.0.0-linux-amd64",
},
},
{
Suffix: "nanoserver",
After: []string{"nanoserver"},
},
{
Before: "refs/tags/v1.9.2",
Suffix: "nanoserver",
After: []string{
"1-nanoserver",
"1.9-nanoserver",
"1.9.2-nanoserver",
},
},
}
for _, test := range tests {
got, want := DefaultTagSuffix(test.Before, test.Suffix), test.After
if !reflect.DeepEqual(got, want) {
t.Errorf("Got tag %v, want %v", got, want)
}
}
}