mirror of
https://github.com/thegeeklab/drone-docker-buildx.git
synced 2024-11-04 20:10:41 +00:00
192 lines
4.6 KiB
Go
192 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/codegangsta/cli"
|
|
_ "github.com/joho/godotenv/autoload"
|
|
)
|
|
|
|
// build number set at compile-time
|
|
var version string
|
|
|
|
// default docker registry
|
|
const defaultRegistry = "https://index.docker.io/v1/"
|
|
|
|
func main() {
|
|
app := cli.NewApp()
|
|
app.Name = "docker plugin"
|
|
app.Usage = "docker plugin"
|
|
app.Action = run
|
|
app.Version = version
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
Name: "dry-run",
|
|
Usage: "dry run disables docker push",
|
|
EnvVar: "PLUGIN_DRY_RUN",
|
|
},
|
|
|
|
cli.StringFlag{
|
|
Name: "commit.sha",
|
|
Usage: "git commit sha",
|
|
EnvVar: "DRONE_COMMIT_SHA",
|
|
Value: "00000000",
|
|
},
|
|
|
|
// daemon parameters
|
|
cli.StringFlag{
|
|
Name: "daemon.mirror",
|
|
Usage: "docker daemon registry mirror",
|
|
EnvVar: "PLUGIN_REGISTRY",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "daemon.storage-driver",
|
|
Usage: "docker daemon storage driver",
|
|
EnvVar: "PLUGIN_STORAGE_DRIVER",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "daemon.storage-path",
|
|
Usage: "docker daemon storage path",
|
|
Value: "/tmp/docker",
|
|
EnvVar: "PLUGIN_STORAGE_PATH",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "daemon.bip",
|
|
Usage: "docker daemon bride ip address",
|
|
EnvVar: "PLUGIN_BIP",
|
|
},
|
|
cli.StringSliceFlag{
|
|
Name: "daemon.dns",
|
|
Usage: "docker daemon dns server",
|
|
EnvVar: "PLUGIN_DNS",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "daemon.insecure",
|
|
Usage: "docker daemon allows insecure registries",
|
|
EnvVar: "PLUGIN_INSECURE",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "daemon.debug",
|
|
Usage: "docker daemon executes in debug mode",
|
|
EnvVar: "PLUGIN_DEBUG,DOCKER_LAUNCH_DEBUG",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "daemon.off",
|
|
Usage: "docker daemon executes in debug mode",
|
|
EnvVar: "PLUGIN_DAEMON_OFF",
|
|
},
|
|
|
|
// build parameters
|
|
|
|
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{
|
|
Name: "tags",
|
|
Usage: "build tags",
|
|
Value: &cli.StringSlice{"latest"},
|
|
EnvVar: "PLUGIN_TAG,PLUGIN_TAGS",
|
|
},
|
|
cli.StringSliceFlag{
|
|
Name: "args",
|
|
Usage: "build args",
|
|
EnvVar: "PLUGIN_BUILD_ARGS",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "repo",
|
|
Usage: "docker repository",
|
|
EnvVar: "PLUGIN_REPO",
|
|
},
|
|
|
|
// secret variables
|
|
cli.StringFlag{
|
|
Name: "docker.registry",
|
|
Usage: "docker username",
|
|
Value: defaultRegistry,
|
|
EnvVar: "DOCKER_REGISTRY,PLUGIN_REGISTRY",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "docker.username",
|
|
Usage: "docker username",
|
|
EnvVar: "DOCKER_USERNAME,PLUGIN_USERNAME",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "docker.password",
|
|
Usage: "docker password",
|
|
EnvVar: "DOCKER_PASSWORD,PLUGIN_PASSWORD",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "docker.email",
|
|
Usage: "docker email",
|
|
EnvVar: "DOCKER_EMAIL,PLUGIN_EMAIL",
|
|
},
|
|
}
|
|
|
|
app.Run(os.Args)
|
|
}
|
|
|
|
func run(c *cli.Context) {
|
|
plugin := Plugin{
|
|
Dryrun: c.Bool("dry-run"),
|
|
Login: Login{
|
|
Registry: c.String("docker.registry"),
|
|
Username: c.String("docker.username"),
|
|
Password: c.String("docker.password"),
|
|
Email: c.String("docker.email"),
|
|
},
|
|
Build: Build{
|
|
Name: c.String("commit.sha"),
|
|
Dockerfile: c.String("dockerfile"),
|
|
Context: c.String("context"),
|
|
Tags: c.StringSlice("tags"),
|
|
Args: c.StringSlice("args"),
|
|
Repo: c.String("repo"),
|
|
},
|
|
Daemon: Daemon{
|
|
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"),
|
|
Debug: c.Bool("daemon.debug"),
|
|
Bip: c.String("daemon.bip"),
|
|
DNS: c.StringSlice("daemon.dns"),
|
|
},
|
|
}
|
|
|
|
// this code attempts to normalize the repository name by appending the fully
|
|
// qualified registry name if otherwise omitted.
|
|
if plugin.Login.Registry != defaultRegistry &&
|
|
strings.HasPrefix(plugin.Build.Repo, defaultRegistry) {
|
|
plugin.Build.Repo = plugin.Login.Registry + "/" + plugin.Build.Repo
|
|
}
|
|
|
|
if err := plugin.Exec(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 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...)...)
|
|
*/
|