Skip to content

Commit

Permalink
Implement default tags logic drone-plugins#150
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Oct 31, 2017
1 parent 3727b33 commit 6f5d6e2
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
[[constraint]]
branch = "master"
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"
16 changes: 16 additions & 0 deletions cmd/drone-docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 changes: 43 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions tags_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 6f5d6e2

Please sign in to comment.