2015-05-15 05:12:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-08-23 19:44:50 +00:00
|
|
|
"fmt"
|
2015-05-15 05:12:50 +00:00
|
|
|
"os"
|
|
|
|
|
2016-08-22 19:49:28 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/joho/godotenv"
|
2016-07-22 18:13:39 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-08-27 19:27:28 +00:00
|
|
|
|
|
|
|
"github.com/drone-plugins/drone-docker"
|
2015-05-15 05:12:50 +00:00
|
|
|
)
|
|
|
|
|
2016-08-23 19:44:50 +00:00
|
|
|
var build = "0" // build number set at compile-time
|
2016-01-18 21:31:56 +00:00
|
|
|
|
2015-05-15 05:12:50 +00:00
|
|
|
func main() {
|
2016-10-27 21:42:45 +00:00
|
|
|
// Load env-file if it exists first
|
|
|
|
if env := os.Getenv("PLUGIN_ENV_FILE"); env != "" {
|
|
|
|
godotenv.Load(env)
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:17:16 +00:00
|
|
|
app := cli.NewApp()
|
2016-05-03 23:34:08 +00:00
|
|
|
app.Name = "docker plugin"
|
|
|
|
app.Usage = "docker plugin"
|
2016-05-03 23:17:16 +00:00
|
|
|
app.Action = run
|
2016-08-23 19:44:50 +00:00
|
|
|
app.Version = fmt.Sprintf("1.0.%s", build)
|
2016-05-03 23:17:16 +00:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "dry-run",
|
|
|
|
Usage: "dry run disables docker push",
|
|
|
|
EnvVar: "PLUGIN_DRY_RUN",
|
|
|
|
},
|
2017-07-17 19:51:02 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "remote.url",
|
|
|
|
Usage: "git remote url",
|
|
|
|
EnvVar: "DRONE_REMOTE_URL",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "commit.sha",
|
|
|
|
Usage: "git commit sha",
|
|
|
|
EnvVar: "DRONE_COMMIT_SHA",
|
2016-05-03 23:34:08 +00:00
|
|
|
Value: "00000000",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
2017-10-31 07:02:01 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "commit.ref",
|
|
|
|
Usage: "git commit ref",
|
|
|
|
EnvVar: "DRONE_COMMIT_REF",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "daemon.mirror",
|
|
|
|
Usage: "docker daemon registry mirror",
|
2016-05-20 19:59:51 +00:00
|
|
|
EnvVar: "PLUGIN_MIRROR",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "daemon.storage-driver",
|
|
|
|
Usage: "docker daemon storage driver",
|
|
|
|
EnvVar: "PLUGIN_STORAGE_DRIVER",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2016-05-03 23:34:08 +00:00
|
|
|
Name: "daemon.storage-path",
|
2016-05-03 23:17:16 +00:00
|
|
|
Usage: "docker daemon storage path",
|
2016-06-03 18:42:20 +00:00
|
|
|
Value: "/var/lib/docker",
|
2016-05-03 23:17:16 +00:00
|
|
|
EnvVar: "PLUGIN_STORAGE_PATH",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "daemon.bip",
|
|
|
|
Usage: "docker daemon bride ip address",
|
|
|
|
EnvVar: "PLUGIN_BIP",
|
|
|
|
},
|
2016-07-25 21:45:31 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "daemon.mtu",
|
|
|
|
Usage: "docker daemon custom mtu setting",
|
|
|
|
EnvVar: "PLUGIN_MTU",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "daemon.dns",
|
|
|
|
Usage: "docker daemon dns server",
|
2017-05-30 21:11:55 +00:00
|
|
|
EnvVar: "PLUGIN_CUSTOM_DNS",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
2017-08-25 15:25:49 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "daemon.dns-search",
|
|
|
|
Usage: "docker daemon dns search domains",
|
|
|
|
EnvVar: "PLUGIN_CUSTOM_DNS_SEARCH",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "daemon.insecure",
|
|
|
|
Usage: "docker daemon allows insecure registries",
|
|
|
|
EnvVar: "PLUGIN_INSECURE",
|
|
|
|
},
|
2016-10-10 18:24:05 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "daemon.ipv6",
|
|
|
|
Usage: "docker daemon IPv6 networking",
|
|
|
|
EnvVar: "PLUGIN_IPV6",
|
|
|
|
},
|
2017-01-19 18:17:43 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "daemon.experimental",
|
|
|
|
Usage: "docker daemon Experimental mode",
|
|
|
|
EnvVar: "PLUGIN_EXPERIMENTAL",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "daemon.debug",
|
|
|
|
Usage: "docker daemon executes in debug mode",
|
|
|
|
EnvVar: "PLUGIN_DEBUG,DOCKER_LAUNCH_DEBUG",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "daemon.off",
|
2017-08-28 15:23:28 +00:00
|
|
|
Usage: "don't start the docker daemon",
|
2016-05-03 23:17:16 +00:00
|
|
|
EnvVar: "PLUGIN_DAEMON_OFF",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "dockerfile",
|
|
|
|
Usage: "build dockerfile",
|
|
|
|
Value: "Dockerfile",
|
|
|
|
EnvVar: "PLUGIN_DOCKERFILE",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "context",
|
|
|
|
Usage: "build context",
|
|
|
|
Value: ".",
|
|
|
|
EnvVar: "PLUGIN_CONTEXT",
|
|
|
|
},
|
|
|
|
cli.StringSliceFlag{
|
2017-05-30 21:11:55 +00:00
|
|
|
Name: "tags",
|
|
|
|
Usage: "build tags",
|
|
|
|
Value: &cli.StringSlice{"latest"},
|
|
|
|
EnvVar: "PLUGIN_TAG,PLUGIN_TAGS",
|
2017-04-10 16:18:00 +00:00
|
|
|
FilePath: ".tags",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
2017-10-31 07:02:01 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "tags.auto",
|
|
|
|
Usage: "default build tags",
|
|
|
|
EnvVar: "PLUGIN_DEFAULT_TAGS,PLUGIN_AUTO_TAG",
|
|
|
|
},
|
2017-10-31 15:49:56 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tags.suffix",
|
|
|
|
Usage: "default build tags with suffix",
|
|
|
|
EnvVar: "PLUGIN_DEFAULT_SUFFIX,PLUGIN_AUTO_TAG_SUFFIX",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "args",
|
|
|
|
Usage: "build args",
|
2016-05-03 23:34:08 +00:00
|
|
|
EnvVar: "PLUGIN_BUILD_ARGS",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
2017-08-27 23:28:58 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "args-from-env",
|
|
|
|
Usage: "build args",
|
|
|
|
EnvVar: "PLUGIN_BUILD_ARGS_FROM_ENV",
|
|
|
|
},
|
2017-01-19 18:42:33 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "squash",
|
|
|
|
Usage: "squash the layers at build time",
|
|
|
|
EnvVar: "PLUGIN_SQUASH",
|
|
|
|
},
|
2017-02-17 16:18:43 +00:00
|
|
|
cli.BoolTFlag{
|
2017-02-20 11:40:35 +00:00
|
|
|
Name: "pull-image",
|
|
|
|
Usage: "force pull base image at build time",
|
|
|
|
EnvVar: "PLUGIN_PULL_IMAGE",
|
2017-02-17 16:18:43 +00:00
|
|
|
},
|
2017-01-27 18:04:55 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "compress",
|
|
|
|
Usage: "compress the build context using gzip",
|
|
|
|
EnvVar: "PLUGIN_COMPRESS",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Usage: "docker repository",
|
|
|
|
EnvVar: "PLUGIN_REPO",
|
|
|
|
},
|
2017-07-17 19:51:02 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "label-schema",
|
|
|
|
Usage: "label-schema labels",
|
|
|
|
EnvVar: "PLUGIN_LABEL_SCHEMA",
|
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "docker.registry",
|
2016-09-16 07:13:26 +00:00
|
|
|
Usage: "docker registry",
|
2017-08-27 19:27:28 +00:00
|
|
|
Value: "https://index.docker.io/v1/",
|
2017-03-16 19:17:34 +00:00
|
|
|
EnvVar: "PLUGIN_REGISTRY,DOCKER_REGISTRY",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "docker.username",
|
|
|
|
Usage: "docker username",
|
2017-03-16 19:17:34 +00:00
|
|
|
EnvVar: "PLUGIN_USERNAME,DOCKER_USERNAME",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "docker.password",
|
|
|
|
Usage: "docker password",
|
2017-03-16 19:17:34 +00:00
|
|
|
EnvVar: "PLUGIN_PASSWORD,DOCKER_PASSWORD",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "docker.email",
|
|
|
|
Usage: "docker email",
|
2017-03-16 19:17:34 +00:00
|
|
|
EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL",
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
2017-11-09 21:28:19 +00:00
|
|
|
cli.BoolTFlag{
|
|
|
|
Name: "docker.purge",
|
|
|
|
Usage: "docker should cleanup images",
|
|
|
|
EnvVar: "PLUGIN_PURGE",
|
|
|
|
},
|
2017-11-02 07:53:28 +00:00
|
|
|
cli.StringFlag{
|
2017-11-09 02:05:50 +00:00
|
|
|
Name: "repo.branch",
|
2017-11-06 07:37:19 +00:00
|
|
|
Usage: "repository default branch",
|
2017-11-03 01:49:07 +00:00
|
|
|
EnvVar: "DRONE_REPO_BRANCH",
|
2017-11-02 07:53:28 +00:00
|
|
|
},
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 19:49:28 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2016-05-03 23:17:16 +00:00
|
|
|
}
|
2015-10-28 00:53:51 +00:00
|
|
|
|
2016-08-22 19:49:28 +00:00
|
|
|
func run(c *cli.Context) error {
|
2017-08-27 19:27:28 +00:00
|
|
|
plugin := docker.Plugin{
|
2017-11-09 21:28:19 +00:00
|
|
|
Dryrun: c.Bool("dry-run"),
|
|
|
|
Cleanup: c.Bool("purge"),
|
2017-08-27 19:27:28 +00:00
|
|
|
Login: docker.Login{
|
2016-05-03 23:17:16 +00:00
|
|
|
Registry: c.String("docker.registry"),
|
|
|
|
Username: c.String("docker.username"),
|
|
|
|
Password: c.String("docker.password"),
|
|
|
|
Email: c.String("docker.email"),
|
|
|
|
},
|
2017-08-27 19:27:28 +00:00
|
|
|
Build: docker.Build{
|
2017-11-09 05:31:36 +00:00
|
|
|
Remote: c.String("remote.url"),
|
|
|
|
Name: c.String("commit.sha"),
|
|
|
|
Dockerfile: c.String("dockerfile"),
|
|
|
|
Context: c.String("context"),
|
|
|
|
Tags: c.StringSlice("tags"),
|
|
|
|
Args: c.StringSlice("args"),
|
|
|
|
ArgsEnv: c.StringSlice("args-from-env"),
|
|
|
|
Squash: c.Bool("squash"),
|
|
|
|
Pull: c.BoolT("pull-image"),
|
|
|
|
Compress: c.Bool("compress"),
|
|
|
|
Repo: c.String("repo"),
|
|
|
|
LabelSchema: c.StringSlice("label-schema"),
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
2017-08-27 19:27:28 +00:00
|
|
|
Daemon: docker.Daemon{
|
2016-05-03 23:17:16 +00:00
|
|
|
Registry: c.String("docker.registry"),
|
|
|
|
Mirror: c.String("daemon.mirror"),
|
|
|
|
StorageDriver: c.String("daemon.storage-driver"),
|
|
|
|
StoragePath: c.String("daemon.storage-path"),
|
|
|
|
Insecure: c.Bool("daemon.insecure"),
|
|
|
|
Disabled: c.Bool("daemon.off"),
|
2016-10-10 18:24:05 +00:00
|
|
|
IPv6: c.Bool("daemon.ipv6"),
|
2016-05-03 23:34:08 +00:00
|
|
|
Debug: c.Bool("daemon.debug"),
|
2016-05-03 23:17:16 +00:00
|
|
|
Bip: c.String("daemon.bip"),
|
|
|
|
DNS: c.StringSlice("daemon.dns"),
|
2017-08-25 15:25:49 +00:00
|
|
|
DNSSearch: c.StringSlice("daemon.dns-search"),
|
2016-07-26 10:09:00 +00:00
|
|
|
MTU: c.String("daemon.mtu"),
|
2017-01-19 18:17:43 +00:00
|
|
|
Experimental: c.Bool("daemon.experimental"),
|
2016-05-03 23:17:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-10-31 07:02:01 +00:00
|
|
|
if c.Bool("tags.auto") {
|
2017-11-09 05:33:18 +00:00
|
|
|
if docker.UseDefaultTag( // return true if tag event or default branch
|
2017-10-31 07:02:01 +00:00
|
|
|
c.String("commit.ref"),
|
2017-11-09 02:05:50 +00:00
|
|
|
c.String("repo.branch"),
|
2017-11-09 05:24:35 +00:00
|
|
|
) {
|
|
|
|
plugin.Build.Tags = docker.DefaultTagSuffix(
|
|
|
|
c.String("commit.ref"),
|
|
|
|
c.String("tags.suffix"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
logrus.Printf("skipping automated docker build for %s", c.String("commit.ref"))
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-31 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 19:49:28 +00:00
|
|
|
return plugin.Exec()
|
2015-05-15 05:12:50 +00:00
|
|
|
}
|