From 224a31a416d65780d90fa59343d1a2ca0859effd Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 9 Nov 2017 13:28:19 -0800 Subject: [PATCH] ability to disable purge --- cmd/drone-docker/main.go | 9 ++++-- docker.go | 33 ++++++-------------- docker_test.go | 66 ---------------------------------------- tags.go | 15 +++++++++ tags_test.go | 64 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 92 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index a7462b2..d9132f2 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -192,6 +192,11 @@ func main() { Usage: "docker email", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, + cli.BoolTFlag{ + Name: "docker.purge", + Usage: "docker should cleanup images", + EnvVar: "PLUGIN_PURGE", + }, cli.StringFlag{ Name: "repo.branch", Usage: "repository default branch", @@ -206,7 +211,8 @@ func main() { func run(c *cli.Context) error { plugin := docker.Plugin{ - Dryrun: c.Bool("dry-run"), + Dryrun: c.Bool("dry-run"), + Cleanup: c.Bool("purge"), Login: docker.Login{ Registry: c.String("docker.registry"), Username: c.String("docker.username"), @@ -255,7 +261,6 @@ func run(c *cli.Context) error { ) } else { logrus.Printf("skipping automated docker build for %s", c.String("commit.ref")) - return nil } } diff --git a/docker.go b/docker.go index 30faeeb..229a1d0 100644 --- a/docker.go +++ b/docker.go @@ -53,31 +53,14 @@ type ( // Plugin defines the Docker plugin parameters. Plugin struct { - Login Login // Docker login configuration - Build Build // Docker build configuration - Daemon Daemon // Docker daemon configuration - Dryrun bool // Docker push is skipped + Login Login // Docker login configuration + Build Build // Docker build configuration + Daemon Daemon // Docker daemon configuration + Dryrun bool // Docker push is skipped + Cleanup bool // Docker purge is enabled } ) -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 func (p Plugin) Exec() error { // start the Docker daemon server @@ -140,8 +123,10 @@ func (p Plugin) Exec() error { } } - cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi - cmds = append(cmds, commandPrune()) // docker system prune -f + if p.Cleanup { + cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi + cmds = append(cmds, commandPrune()) // docker system prune -f + } // execute all commands in batch mode. for _, cmd := range cmds { diff --git a/docker_test.go b/docker_test.go index 7781593..1cdc3ff 100644 --- a/docker_test.go +++ b/docker_test.go @@ -1,67 +1 @@ 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) - } - } -} diff --git a/tags.go b/tags.go index 5e1b36e..99c7a83 100644 --- a/tags.go +++ b/tags.go @@ -53,6 +53,21 @@ func DefaultTags(ref string) []string { } } +// 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 +} + +func stripHeadPrefix(ref string) string { + return strings.TrimPrefix(ref, "refs/heads/") +} + func stripTagPrefix(ref string) string { ref = strings.TrimPrefix(ref, "refs/tags/") ref = strings.TrimPrefix(ref, "v") diff --git a/tags_test.go b/tags_test.go index 9d356f8..a68bfa5 100644 --- a/tags_test.go +++ b/tags_test.go @@ -102,3 +102,67 @@ func TestDefaultTagSuffix(t *testing.T) { } } } + +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) + } + } +}