From 88ae029815889bfa4d8d7ee96967d31a678716fe Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 31 Oct 2017 08:49:56 -0700 Subject: [PATCH] add defualt tag suffix --- cmd/drone-docker/main.go | 8 +++++- tags.go | 19 +++++++++++++- tags_test.go | 57 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 5fe75f7..3a1247f 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -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"), ) } diff --git a/tags.go b/tags.go index d9efde7..5e1b36e 100644 --- a/tags.go +++ b/tags.go @@ -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/") { diff --git a/tags_test.go b/tags_test.go index 4625db7..9d356f8 100644 --- a/tags_test.go +++ b/tags_test.go @@ -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) + } + } +}