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

keep only master branch for latest

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-11-02 15:53:28 +08:00
parent 88ae029815
commit d8ba908538
4 changed files with 98 additions and 52 deletions

View File

@ -192,6 +192,17 @@ func main() {
Usage: "docker email", Usage: "docker email",
EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL",
}, },
cli.StringFlag{
Name: "commit.branch",
Value: "master",
Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH",
},
cli.StringFlag{
Name: "default.branch",
Usage: "defualt latest branch",
EnvVar: "PLUGIN_DEFALUT_BRANCH",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -209,18 +220,20 @@ func run(c *cli.Context) error {
Email: c.String("docker.email"), Email: c.String("docker.email"),
}, },
Build: docker.Build{ Build: docker.Build{
Remote: c.String("remote.url"), Remote: c.String("remote.url"),
Name: c.String("commit.sha"), Name: c.String("commit.sha"),
Dockerfile: c.String("dockerfile"), Branch: c.String("commit.branch"),
Context: c.String("context"), DefaultBranch: c.String("default.branch"),
Tags: c.StringSlice("tags"), Dockerfile: c.String("dockerfile"),
Args: c.StringSlice("args"), Context: c.String("context"),
ArgsEnv: c.StringSlice("args-from-env"), Tags: c.StringSlice("tags"),
Squash: c.Bool("squash"), Args: c.StringSlice("args"),
Pull: c.BoolT("pull-image"), ArgsEnv: c.StringSlice("args-from-env"),
Compress: c.Bool("compress"), Squash: c.Bool("squash"),
Repo: c.String("repo"), Pull: c.BoolT("pull-image"),
LabelSchema: c.StringSlice("label-schema"), Compress: c.Bool("compress"),
Repo: c.String("repo"),
LabelSchema: c.StringSlice("label-schema"),
}, },
Daemon: docker.Daemon{ Daemon: docker.Daemon{
Registry: c.String("docker.registry"), Registry: c.String("docker.registry"),
@ -243,6 +256,8 @@ func run(c *cli.Context) error {
plugin.Build.Tags = docker.DefaultTagSuffix( plugin.Build.Tags = docker.DefaultTagSuffix(
c.String("commit.ref"), c.String("commit.ref"),
c.String("tags.suffix"), c.String("tags.suffix"),
c.String("commit.branch"),
c.String("default.branch"),
) )
} }

View File

@ -37,18 +37,20 @@ type (
// Build defines Docker build parameters. // Build defines Docker build parameters.
Build struct { Build struct {
Remote string // Git remote URL Remote string // Git remote URL
Name string // Docker build using default named tag Name string // Docker build using default named tag
Dockerfile string // Docker build Dockerfile Dockerfile string // Docker build Dockerfile
Context string // Docker build context Context string // Docker build context
Tags []string // Docker build tags Tags []string // Docker build tags
Args []string // Docker build args Args []string // Docker build args
ArgsEnv []string // Docker build args from env ArgsEnv []string // Docker build args from env
Squash bool // Docker build squash Squash bool // Docker build squash
Pull bool // Docker build pull Pull bool // Docker build pull
Compress bool // Docker build compress Compress bool // Docker build compress
Repo string // Docker build repository Repo string // Docker build repository
LabelSchema []string // Label schema map LabelSchema []string // Label schema map
Branch string // Docker build branch
DefaultBranch string // Docker latest branch
} }
// Plugin defines the Docker plugin parameters. // Plugin defines the Docker plugin parameters.

14
tags.go
View File

@ -9,8 +9,8 @@ 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, commitBranch, defaultBranch string) []string {
tags := DefaultTags(ref) tags := DefaultTags(ref, commitBranch, defaultBranch)
if len(suffix) == 0 { if len(suffix) == 0 {
return tags return tags
} }
@ -26,10 +26,18 @@ func DefaultTagSuffix(ref, suffix 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, commitBranch, defaultBranch string) []string {
if defaultBranch != "" &&
commitBranch != defaultBranch &&
!strings.HasPrefix(ref, "refs/tags/") {
return []string{}
}
if !strings.HasPrefix(ref, "refs/tags/") { if !strings.HasPrefix(ref, "refs/tags/") {
return []string{"latest"} return []string{"latest"}
} }
v := stripTagPrefix(ref) v := stripTagPrefix(ref)
version, err := semver.NewVersion(v) version, err := semver.NewVersion(v)
if err != nil { if err != nil {

View File

@ -25,23 +25,30 @@ func Test_stripTagPrefix(t *testing.T) {
func TestDefaultTags(t *testing.T) { func TestDefaultTags(t *testing.T) {
var tests = []struct { var tests = []struct {
Before string Before string
After []string CommitBranch string
DefaultBranch string
After []string
}{ }{
{"", []string{"latest"}}, {"", "master", "", []string{"latest"}},
{"refs/heads/master", []string{"latest"}}, {"refs/heads/master", "master", "", []string{"latest"}},
{"refs/tags/0.9.0", []string{"0.9", "0.9.0"}}, {"refs/tags/0.9.0", "master", "", []string{"0.9", "0.9.0"}},
{"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}}, {"refs/tags/1.0.0", "master", "", []string{"1", "1.0", "1.0.0"}},
{"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}}, {"refs/tags/v1.0.0", "master", "", []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", "master", "", []string{"1.0.0-alpha.1"}},
// malformed or errors // malformed or errors
{"refs/tags/x1.0.0", []string{"latest"}}, {"refs/tags/x1.0.0", "master", "", []string{"latest"}},
{"v1.0.0", []string{"latest"}}, {"v1.0.0", "master", "", []string{"latest"}},
// defualt branch
{"refs/heads/master", "master", "master", []string{"latest"}},
{"refs/heads/test", "test", "master", []string{}},
{"refs/tags/v1.0.0", "v1.0.0", "master", []string{"1", "1.0", "1.0.0"}},
} }
for _, test := range tests { for _, test := range tests {
got, want := DefaultTags(test.Before), test.After got, want := DefaultTags(test.Before, test.CommitBranch, test.DefaultBranch), 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)
} }
@ -50,16 +57,22 @@ func TestDefaultTags(t *testing.T) {
func TestDefaultTagSuffix(t *testing.T) { func TestDefaultTagSuffix(t *testing.T) {
var tests = []struct { var tests = []struct {
Before string Before string
Suffix string CommitBranch string
After []string DefaultBranch string
Suffix string
After []string
}{ }{
// without suffix // without suffix
{ {
After: []string{"latest"}, After: []string{"latest"},
CommitBranch: "master",
DefaultBranch: "",
}, },
{ {
Before: "refs/tags/v1.0.0", Before: "refs/tags/v1.0.0",
CommitBranch: "master",
DefaultBranch: "",
After: []string{ After: []string{
"1", "1",
"1.0", "1.0",
@ -68,12 +81,16 @@ func TestDefaultTagSuffix(t *testing.T) {
}, },
// with suffix // with suffix
{ {
Suffix: "linux-amd64", CommitBranch: "master",
After: []string{"linux-amd64"}, DefaultBranch: "",
Suffix: "linux-amd64",
After: []string{"linux-amd64"},
}, },
{ {
Before: "refs/tags/v1.0.0", CommitBranch: "master",
Suffix: "linux-amd64", DefaultBranch: "",
Before: "refs/tags/v1.0.0",
Suffix: "linux-amd64",
After: []string{ After: []string{
"1-linux-amd64", "1-linux-amd64",
"1.0-linux-amd64", "1.0-linux-amd64",
@ -81,12 +98,16 @@ func TestDefaultTagSuffix(t *testing.T) {
}, },
}, },
{ {
Suffix: "nanoserver", CommitBranch: "master",
After: []string{"nanoserver"}, DefaultBranch: "",
Suffix: "nanoserver",
After: []string{"nanoserver"},
}, },
{ {
Before: "refs/tags/v1.9.2", CommitBranch: "master",
Suffix: "nanoserver", DefaultBranch: "",
Before: "refs/tags/v1.9.2",
Suffix: "nanoserver",
After: []string{ After: []string{
"1-nanoserver", "1-nanoserver",
"1.9-nanoserver", "1.9-nanoserver",
@ -96,7 +117,7 @@ func TestDefaultTagSuffix(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
got, want := DefaultTagSuffix(test.Before, test.Suffix), test.After got, want := DefaultTagSuffix(test.Before, test.Suffix, test.CommitBranch, test.DefaultBranch), 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)
} }