From 18c4e995d32b6a1fc7a6db3aaa2e623af2879fd5 Mon Sep 17 00:00:00 2001 From: Andrea Cervesato Date: Tue, 24 Mar 2020 19:20:52 +0100 Subject: [PATCH] Added AuthConfig to login to multiple registry --- cmd/drone-docker/main.go | 14 ++++++++++---- docker.go | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 946d994..2cbb5ce 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -208,6 +208,11 @@ func main() { Usage: "docker email", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, + cli.StringFlag{ + Name: "docker.authconfig", + Usage: "docker json authconfig content", + EnvVar: "PLUGIN_AUTHCONFIG,DOCKER_AUTHCONFIG", + }, cli.BoolTFlag{ Name: "docker.purge", Usage: "docker should cleanup images", @@ -240,10 +245,11 @@ func run(c *cli.Context) error { Dryrun: c.Bool("dry-run"), Cleanup: c.BoolT("docker.purge"), Login: docker.Login{ - Registry: c.String("docker.registry"), - Username: c.String("docker.username"), - Password: c.String("docker.password"), - Email: c.String("docker.email"), + Registry: c.String("docker.registry"), + Username: c.String("docker.username"), + Password: c.String("docker.password"), + Email: c.String("docker.email"), + AuthConfig: c.String("docker.authconfig"), }, Build: docker.Build{ Remote: c.String("remote.url"), diff --git a/docker.go b/docker.go index be23616..6209650 100644 --- a/docker.go +++ b/docker.go @@ -28,10 +28,11 @@ type ( // Login defines Docker login parameters. Login struct { - Registry string // Docker registry address - Username string // Docker registry username - Password string // Docker registry password - Email string // Docker registry email + Registry string // Docker registry address + Username string // Docker registry username + Password string // Docker registry password + Email string // Docker registry email + AuthConfig string // Docker Auth Config } // Build defines Docker build parameters. @@ -83,6 +84,35 @@ func (p Plugin) Exec() error { time.Sleep(time.Second * 1) } + // Create Auth Config File + if p.Login.AuthConfig != "" { + fmt.Println("AuthConfig provided.") + err_mkdir := os.MkdirAll("/root/.docker", 0600) + if err_mkdir != nil { + fmt.Println("Error creating root's docker auth config directory: ") + fmt.Println(err_mkdir) + } + cfile, err_create := os.Create("/root/.docker/config.json") + if err_create != nil { + fmt.Println("Error creating root's docker auth config file: ") + fmt.Println(err_create) + cfile.Close() + } + err_chmod := os.Chmod("/root/.docker/config.json", 0600) + if err_chmod != nil { + fmt.Println("Error setting permissions on root's docker auth config file: ") + fmt.Println(err_chmod) + } + _, err_str := cfile.WriteString(p.Login.AuthConfig) + if err_str != nil { + fmt.Println("Error filling root's docker auth config file: ") + fmt.Println(err_str) + } else { + cfile.Close() + } + + } + // login to the Docker registry if p.Login.Password != "" { cmd := commandLogin(p.Login) @@ -93,7 +123,7 @@ func (p Plugin) Exec() error { } else { fmt.Println("Registry credentials not provided. Guest mode enabled.") } - + if p.Build.Squash && !p.Daemon.Experimental { fmt.Println("Squash build flag is only available when Docker deamon is started with experimental flag. Ignoring...") p.Build.Squash = false