Implement default tags logic #150
This commit is contained in:
parent
3727b33076
commit
6f5d6e2481
8
Gopkg.lock
generated
8
Gopkg.lock
generated
@ -13,6 +13,12 @@
|
||||
revision = "ee1f179877b2daf2aaabf71fa900773bf8842253"
|
||||
version = "v1.12.19"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/coreos/go-semver"
|
||||
packages = ["semver"]
|
||||
revision = "8ab6407b697782a06568d4b7f1db25550ec2e4c6"
|
||||
version = "v0.2.0"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/go-ini/ini"
|
||||
packages = ["."]
|
||||
@ -52,6 +58,6 @@
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "be0db8d07207fd70e97ba8ec8fc72defa40db74bd8438cf482be479ff67fa09b"
|
||||
inputs-digest = "d810a46da78466106fe9f994d288826e0c876cef7addebade091f62c1a1522a1"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
@ -37,3 +37,7 @@
|
||||
branch = "master"
|
||||
name = "github.com/urfave/cli"
|
||||
source = "https://github.com/bradrydzewski/cli.git"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/coreos/go-semver"
|
||||
version = "v0.2.0"
|
||||
|
@ -41,6 +41,11 @@ func main() {
|
||||
EnvVar: "DRONE_COMMIT_SHA",
|
||||
Value: "00000000",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "commit.ref",
|
||||
Usage: "git commit ref",
|
||||
EnvVar: "DRONE_COMMIT_REF",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "daemon.mirror",
|
||||
Usage: "docker daemon registry mirror",
|
||||
@ -121,6 +126,11 @@ func main() {
|
||||
EnvVar: "PLUGIN_TAG,PLUGIN_TAGS",
|
||||
FilePath: ".tags",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "tags.auto",
|
||||
Usage: "default build tags",
|
||||
EnvVar: "PLUGIN_DEFAULT_TAGS,PLUGIN_AUTO_TAG",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "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()
|
||||
}
|
||||
|
43
tags.go
Normal file
43
tags.go
Normal 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
49
tags_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user