Merge remote-tracking branch 'origin'

This commit is contained in:
Brad Rydzewski 2017-04-04 00:40:59 +09:00
commit 7d9dd5cac7
3 changed files with 34 additions and 20 deletions

View File

@ -7,7 +7,7 @@
Drone plugin can be used to build and publish Docker images to a container Drone plugin can be used to build and publish Docker images to a container
registry. For the usage information and a listing of the available options registry. For the usage information and a listing of the available options
please take a look at [the docs](DOCS.md). please take a look at [the docs](http://plugins.drone.io/drone-plugins/drone-docker/).
## Build ## Build

20
main.go
View File

@ -118,6 +118,16 @@ func main() {
Usage: "squash the layers at build time", Usage: "squash the layers at build time",
EnvVar: "PLUGIN_SQUASH", EnvVar: "PLUGIN_SQUASH",
}, },
cli.BoolTFlag{
Name: "pull-image",
Usage: "force pull base image at build time",
EnvVar: "PLUGIN_PULL_IMAGE",
},
cli.BoolFlag{
Name: "compress",
Usage: "compress the build context using gzip",
EnvVar: "PLUGIN_COMPRESS",
},
cli.StringFlag{ cli.StringFlag{
Name: "repo", Name: "repo",
Usage: "docker repository", Usage: "docker repository",
@ -127,22 +137,22 @@ func main() {
Name: "docker.registry", Name: "docker.registry",
Usage: "docker registry", Usage: "docker registry",
Value: defaultRegistry, Value: defaultRegistry,
EnvVar: "DOCKER_REGISTRY,PLUGIN_REGISTRY", EnvVar: "PLUGIN_REGISTRY,DOCKER_REGISTRY",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "docker.username", Name: "docker.username",
Usage: "docker username", Usage: "docker username",
EnvVar: "DOCKER_USERNAME,PLUGIN_USERNAME", EnvVar: "PLUGIN_USERNAME,DOCKER_USERNAME",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "docker.password", Name: "docker.password",
Usage: "docker password", Usage: "docker password",
EnvVar: "DOCKER_PASSWORD,PLUGIN_PASSWORD", EnvVar: "PLUGIN_PASSWORD,DOCKER_PASSWORD",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "docker.email", Name: "docker.email",
Usage: "docker email", Usage: "docker email",
EnvVar: "DOCKER_EMAIL,PLUGIN_EMAIL", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL",
}, },
} }
@ -167,6 +177,8 @@ func run(c *cli.Context) error {
Tags: c.StringSlice("tags"), Tags: c.StringSlice("tags"),
Args: c.StringSlice("args"), Args: c.StringSlice("args"),
Squash: c.Bool("squash"), Squash: c.Bool("squash"),
Pull: c.BoolT("pull-image"),
Compress: c.Bool("compress"),
Repo: c.String("repo"), Repo: c.String("repo"),
}, },
Daemon: Daemon{ Daemon: Daemon{

View File

@ -47,6 +47,8 @@ type (
Tags []string // Docker build tags Tags []string // Docker build tags
Args []string // Docker build args Args []string // Docker build args
Squash bool // Docker build squash Squash bool // Docker build squash
Pull bool // Docker build pull
Compress bool // Docker build compress
Repo string // Docker build repository Repo string // Docker build repository
} }
@ -61,17 +63,6 @@ type (
// Exec executes the plugin step // Exec executes the plugin step
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
// TODO execute code remove dangling images
// this is problematic because we are running docker in scratch which does
// not have bash, so we need to hack something together
// docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi
/*
cmd = exec.Command("docker", "images", "-q", "-f", "dangling=true")
cmd = exec.Command("docker", append([]string{"rmi"}, images...)...)
*/
// start the Docker daemon server // start the Docker daemon server
if !p.Daemon.Disabled { if !p.Daemon.Disabled {
cmd := commandDaemon(p.Daemon) cmd := commandDaemon(p.Daemon)
@ -121,6 +112,7 @@ func (p Plugin) Exec() error {
var cmds []*exec.Cmd var cmds []*exec.Cmd
cmds = append(cmds, commandVersion()) // docker version cmds = append(cmds, commandVersion()) // docker version
cmds = append(cmds, commandInfo()) // docker info cmds = append(cmds, commandInfo()) // docker info
cmds = append(cmds, commandDockerPrune()) // cleanup docker
cmds = append(cmds, commandBuild(p.Build)) // docker build cmds = append(cmds, commandBuild(p.Build)) // docker build
for _, tag := range p.Build.Tags { for _, tag := range p.Build.Tags {
@ -147,6 +139,7 @@ func (p Plugin) Exec() error {
} }
const dockerExe = "/usr/local/bin/docker" const dockerExe = "/usr/local/bin/docker"
const dockerdExe = "/usr/local/bin/dockerd"
// helper function to create the docker login command. // helper function to create the docker login command.
func commandLogin(login Login) *exec.Cmd { func commandLogin(login Login) *exec.Cmd {
@ -185,7 +178,6 @@ func commandInfo() *exec.Cmd {
func commandBuild(build Build) *exec.Cmd { func commandBuild(build Build) *exec.Cmd {
args := []string{ args := []string{
"build", "build",
"--pull=true",
"--rm=true", "--rm=true",
"-f", build.Dockerfile, "-f", build.Dockerfile,
"-t", build.Name, "-t", build.Name,
@ -195,6 +187,12 @@ func commandBuild(build Build) *exec.Cmd {
if build.Squash { if build.Squash {
args = append(args, "--squash") args = append(args, "--squash")
} }
if build.Compress {
args = append(args, "--compress")
}
if build.Pull {
args = append(args, "--pull=true")
}
for _, arg := range build.Args { for _, arg := range build.Args {
args = append(args, "--build-arg", arg) args = append(args, "--build-arg", arg)
} }
@ -264,7 +262,7 @@ func commandPush(build Build, tag string) *exec.Cmd {
// helper function to create the docker daemon command. // helper function to create the docker daemon command.
func commandDaemon(daemon Daemon) *exec.Cmd { func commandDaemon(daemon Daemon) *exec.Cmd {
args := []string{"daemon", "-g", daemon.StoragePath} args := []string{"-g", daemon.StoragePath}
if daemon.StorageDriver != "" { if daemon.StorageDriver != "" {
args = append(args, "-s", daemon.StorageDriver) args = append(args, "-s", daemon.StorageDriver)
@ -290,7 +288,11 @@ func commandDaemon(daemon Daemon) *exec.Cmd {
if daemon.Experimental { if daemon.Experimental {
args = append(args, "--experimental") args = append(args, "--experimental")
} }
return exec.Command(dockerExe, args...) return exec.Command(dockerdExe, args...)
}
func commandDockerPrune() *exec.Cmd {
return exec.Command(dockerExe, "system", "prune", "-f")
} }
// trace writes each command to stdout with the command wrapped in an xml // trace writes each command to stdout with the command wrapped in an xml