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

Implement default tags logic #150

This commit is contained in:
Brad Rydzewski 2017-10-31 00:02:01 -07:00
parent 3727b33076
commit 6f5d6e2481
5 changed files with 120 additions and 2 deletions

8
Gopkg.lock generated
View File

@ -13,6 +13,12 @@
revision = "ee1f179877b2daf2aaabf71fa900773bf8842253" revision = "ee1f179877b2daf2aaabf71fa900773bf8842253"
version = "v1.12.19" version = "v1.12.19"
[[projects]]
name = "github.com/coreos/go-semver"
packages = ["semver"]
revision = "8ab6407b697782a06568d4b7f1db25550ec2e4c6"
version = "v0.2.0"
[[projects]] [[projects]]
name = "github.com/go-ini/ini" name = "github.com/go-ini/ini"
packages = ["."] packages = ["."]
@ -52,6 +58,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "be0db8d07207fd70e97ba8ec8fc72defa40db74bd8438cf482be479ff67fa09b" inputs-digest = "d810a46da78466106fe9f994d288826e0c876cef7addebade091f62c1a1522a1"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -37,3 +37,7 @@
branch = "master" branch = "master"
name = "github.com/urfave/cli" name = "github.com/urfave/cli"
source = "https://github.com/bradrydzewski/cli.git" source = "https://github.com/bradrydzewski/cli.git"
[[constraint]]
name = "github.com/coreos/go-semver"
version = "v0.2.0"

View File

@ -41,6 +41,11 @@ func main() {
EnvVar: "DRONE_COMMIT_SHA", EnvVar: "DRONE_COMMIT_SHA",
Value: "00000000", Value: "00000000",
}, },
cli.StringFlag{
Name: "commit.ref",
Usage: "git commit ref",
EnvVar: "DRONE_COMMIT_REF",
},
cli.StringFlag{ cli.StringFlag{
Name: "daemon.mirror", Name: "daemon.mirror",
Usage: "docker daemon registry mirror", Usage: "docker daemon registry mirror",
@ -121,6 +126,11 @@ func main() {
EnvVar: "PLUGIN_TAG,PLUGIN_TAGS", EnvVar: "PLUGIN_TAG,PLUGIN_TAGS",
FilePath: ".tags", FilePath: ".tags",
}, },
cli.BoolFlag{
Name: "tags.auto",
Usage: "default build tags",
EnvVar: "PLUGIN_DEFAULT_TAGS,PLUGIN_AUTO_TAG",
},
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "args", Name: "args",
Usage: "build args", Usage: "build args",
@ -224,5 +234,11 @@ func run(c *cli.Context) error {
}, },
} }
if c.Bool("tags.auto") {
plugin.Build.Tags = docker.DefaultTags(
c.String("commit.ref"),
)
}
return plugin.Exec() return plugin.Exec()
} }

43
tags.go Normal file
View File

@ -0,0 +1,43 @@
package docker
import (
"fmt"
"strings"
"github.com/coreos/go-semver/semver"
)
// Default tags returns a set of default suggested tags based on
// the commit ref.
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 {
return []string{"latest"}
}
if version.PreRelease != "" || version.Metadata != "" {
return []string{
version.String(),
}
}
if version.Major == 0 {
return []string{
fmt.Sprintf("%d.%d", version.Major, version.Minor),
fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch),
}
}
return []string{
fmt.Sprint(version.Major),
fmt.Sprintf("%d.%d", version.Major, version.Minor),
fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch),
}
}
func stripTagPrefix(ref string) string {
ref = strings.TrimPrefix(ref, "refs/tags/")
ref = strings.TrimPrefix(ref, "v")
return ref
}

49
tags_test.go Normal file
View File

@ -0,0 +1,49 @@
package docker
import (
"reflect"
"testing"
)
func Test_stripTagPrefix(t *testing.T) {
var tests = []struct {
Before string
After string
}{
{"refs/tags/1.0.0", "1.0.0"},
{"refs/tags/v1.0.0", "1.0.0"},
{"v1.0.0", "1.0.0"},
}
for _, test := range tests {
got, want := stripTagPrefix(test.Before), test.After
if got != want {
t.Errorf("Got tag %s, want %s", got, want)
}
}
}
func Test_defaultTags(t *testing.T) {
var tests = []struct {
Before 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"}},
// malformed or errors
{"refs/tags/x1.0.0", []string{"latest"}},
{"v1.0.0", []string{"latest"}},
}
for _, test := range tests {
got, want := DefaultTags(test.Before), test.After
if !reflect.DeepEqual(got, want) {
t.Errorf("Got tag %v, want %v", got, want)
}
}
}