diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 946d994..875b5b6 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -7,7 +7,7 @@ import ( "github.com/sirupsen/logrus" "github.com/urfave/cli" - "github.com/drone-plugins/drone-docker" + docker "github.com/drone-plugins/drone-docker" ) var ( @@ -208,6 +208,11 @@ func main() { Usage: "docker email", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, + cli.StringFlag{ + Name: "docker.config", + Usage: "docker json dockerconfig content", + EnvVar: "PLUGIN_CONFIG", + }, cli.BoolTFlag{ Name: "docker.purge", Usage: "docker should cleanup images", @@ -244,6 +249,7 @@ func run(c *cli.Context) error { Username: c.String("docker.username"), Password: c.String("docker.password"), Email: c.String("docker.email"), + Config: c.String("docker.config"), }, Build: docker.Build{ Remote: c.String("remote.url"), diff --git a/daemon.go b/daemon.go index 5bf1119..5976cd1 100644 --- a/daemon.go +++ b/daemon.go @@ -9,6 +9,7 @@ import ( const dockerExe = "/usr/local/bin/docker" const dockerdExe = "/usr/local/bin/dockerd" +const dockerHome = "/root/.docker/" func (p Plugin) startDaemon() { cmd := commandDaemon(p.Daemon) @@ -23,4 +24,4 @@ func (p Plugin) startDaemon() { trace(cmd) cmd.Run() }() -} \ No newline at end of file +} diff --git a/daemon_win.go b/daemon_win.go index 062cf5e..77e0543 100644 --- a/daemon_win.go +++ b/daemon_win.go @@ -4,6 +4,7 @@ package docker const dockerExe = "C:\\bin\\docker.exe" const dockerdExe = "" +const dockerHome = "C:\\ProgramData\\docker\\" func (p Plugin) startDaemon() { // this is a no-op on windows diff --git a/docker.go b/docker.go index be23616..e94250b 100644 --- a/docker.go +++ b/docker.go @@ -2,8 +2,10 @@ package docker import ( "fmt" + "io/ioutil" "os" "os/exec" + "path/filepath" "strings" "time" ) @@ -32,6 +34,7 @@ type ( Username string // Docker registry username Password string // Docker registry password Email string // Docker registry email + Config string // Docker Auth Config } // Build defines Docker build parameters. @@ -83,6 +86,17 @@ func (p Plugin) Exec() error { time.Sleep(time.Second * 1) } + // Create Auth Config File + if p.Login.Config != "" { + os.MkdirAll(dockerHome, 0600) + + path := filepath.Join(dockerHome, "config.json") + err := ioutil.WriteFile(path, []byte(p.Login.Config), 0600) + if err != nil { + return fmt.Errorf("Error writeing config.json: %s", err) + } + } + // login to the Docker registry if p.Login.Password != "" { cmd := commandLogin(p.Login) @@ -90,8 +104,15 @@ func (p Plugin) Exec() error { if err != nil { return fmt.Errorf("Error authenticating: %s", err) } - } else { - fmt.Println("Registry credentials not provided. Guest mode enabled.") + } + + switch { + case p.Login.Password != "": + fmt.Println("Detected registry credentials") + case p.Login.Config != "": + fmt.Println("Detected registry credentials file") + default: + fmt.Println("Registry credentials or Docker config not provided. Guest mode enabled.") } if p.Build.Squash && !p.Daemon.Experimental {