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

ability to disable purge

This commit is contained in:
Brad Rydzewski 2017-11-09 13:28:19 -08:00
parent 55fd78d1cc
commit 224a31a416
5 changed files with 95 additions and 92 deletions

View File

@ -192,6 +192,11 @@ func main() {
Usage: "docker email", Usage: "docker email",
EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL",
}, },
cli.BoolTFlag{
Name: "docker.purge",
Usage: "docker should cleanup images",
EnvVar: "PLUGIN_PURGE",
},
cli.StringFlag{ cli.StringFlag{
Name: "repo.branch", Name: "repo.branch",
Usage: "repository default branch", Usage: "repository default branch",
@ -207,6 +212,7 @@ func main() {
func run(c *cli.Context) error { func run(c *cli.Context) error {
plugin := docker.Plugin{ plugin := docker.Plugin{
Dryrun: c.Bool("dry-run"), Dryrun: c.Bool("dry-run"),
Cleanup: c.Bool("purge"),
Login: docker.Login{ Login: docker.Login{
Registry: c.String("docker.registry"), Registry: c.String("docker.registry"),
Username: c.String("docker.username"), Username: c.String("docker.username"),
@ -255,7 +261,6 @@ func run(c *cli.Context) error {
) )
} else { } else {
logrus.Printf("skipping automated docker build for %s", c.String("commit.ref")) logrus.Printf("skipping automated docker build for %s", c.String("commit.ref"))
return nil return nil
} }
} }

View File

@ -57,27 +57,10 @@ type (
Build Build // Docker build configuration Build Build // Docker build configuration
Daemon Daemon // Docker daemon configuration Daemon Daemon // Docker daemon configuration
Dryrun bool // Docker push is skipped 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 // 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
@ -140,8 +123,10 @@ func (p Plugin) Exec() error {
} }
} }
if p.Cleanup {
cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi
cmds = append(cmds, commandPrune()) // docker system prune -f cmds = append(cmds, commandPrune()) // docker system prune -f
}
// execute all commands in batch mode. // execute all commands in batch mode.
for _, cmd := range cmds { for _, cmd := range cmds {

View File

@ -1,67 +1 @@
package docker 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)
}
}
}

15
tags.go
View File

@ -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 { func stripTagPrefix(ref string) string {
ref = strings.TrimPrefix(ref, "refs/tags/") ref = strings.TrimPrefix(ref, "refs/tags/")
ref = strings.TrimPrefix(ref, "v") ref = strings.TrimPrefix(ref, "v")

View File

@ -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)
}
}
}