Merge pull request #160 from appleboy/defalut

keep only master branch for latest
This commit is contained in:
Brad Rydzewski 2017-11-09 13:17:00 -08:00 committed by GitHub
commit 55fd78d1cc
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 3 deletions

@ -192,6 +192,11 @@ func main() {
Usage: "docker email", Usage: "docker email",
EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL",
}, },
cli.StringFlag{
Name: "repo.branch",
Usage: "repository default branch",
EnvVar: "DRONE_REPO_BRANCH",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -240,10 +245,19 @@ func run(c *cli.Context) error {
} }
if c.Bool("tags.auto") { if c.Bool("tags.auto") {
if docker.UseDefaultTag( // return true if tag event or default branch
c.String("commit.ref"),
c.String("repo.branch"),
) {
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"),
) )
} else {
logrus.Printf("skipping automated docker build for %s", c.String("commit.ref"))
return nil
}
} }
return plugin.Exec() return plugin.Exec()

@ -60,6 +60,24 @@ type (
} }
) )
func stripHeadPrefix(ref string) string {
ref = strings.TrimPrefix(ref, "refs/heads/")
return ref
}
// UseDefaultTag for keep only default branch for latest tag
func UseDefaultTag(ref, defaultBranch string) bool {
if strings.HasPrefix(ref, "refs/tags/") {
return true
}
if stripHeadPrefix(ref) == defaultBranch {
return true
}
return false
}
// Exec executes the plugin step // Exec executes the plugin step
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
// start the Docker daemon server // start the Docker daemon server

67
docker_test.go Normal file

@ -0,0 +1,67 @@
package docker
import "testing"
func Test_stripHeadPrefix(t *testing.T) {
type args struct {
ref string
}
tests := []struct {
args args
want string
}{
{
args: args{
ref: "refs/heads/master",
},
want: "master",
},
}
for _, tt := range tests {
if got := stripHeadPrefix(tt.args.ref); got != tt.want {
t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want)
}
}
}
func TestUseDefaultTag(t *testing.T) {
type args struct {
ref string
defaultBranch string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "latest tag for default branch",
args: args{
ref: "refs/heads/master",
defaultBranch: "master",
},
want: true,
},
{
name: "build from tags",
args: args{
ref: "refs/tags/v1.0.0",
defaultBranch: "master",
},
want: true,
},
{
name: "skip build for not default branch",
args: args{
ref: "refs/heads/develop",
defaultBranch: "master",
},
want: false,
},
}
for _, tt := range tests {
if got := UseDefaultTag(tt.args.ref, tt.args.defaultBranch); got != tt.want {
t.Errorf("%q. UseDefaultTag() = %v, want %v", tt.name, got, tt.want)
}
}
}