From f25b0139371c66bcaf57a8e0a3a6065cd68cbcba Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sun, 27 Aug 2017 12:57:23 -0700 Subject: [PATCH] enable fallback secret names [ci skip] --- cmd/drone-docker-gcr/main.go | 27 ++++++++++++++++++++++----- cmd/drone-docker-heroku/main.go | 31 ++++++++++++++++--------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/cmd/drone-docker-gcr/main.go b/cmd/drone-docker-gcr/main.go index 7342de3..b4bc5f8 100644 --- a/cmd/drone-docker-gcr/main.go +++ b/cmd/drone-docker-gcr/main.go @@ -8,12 +8,19 @@ import ( "strings" ) +// gcr default username +const username = "_json_key" + func main() { var ( - username = "_json_key" - password = os.Getenv("GCR_TOKEN") - registry = os.Getenv("PLUGIN_REGISTRY") - repo = os.Getenv("PLUGIN_REPO") + repo = getenv("PLUGIN_REPO") + registry = getenv("PLUGIN_REGISTRY") + password = getenv( + "PLUGIN_JSON_KEY", + "GCR_JSON_KEY", + "GOOGLE_CREDENTIALS", + "GOOGLE_APPLICATION_CREDENTIALS", + ) ) // decode the token if base64 encoded @@ -32,9 +39,9 @@ func main() { // should prepend. if !strings.HasPrefix(repo, registry) { repo = path.Join(registry, repo) - os.Setenv("PLUGIN_REPO", repo) } + os.Setenv("PLUGIN_REPO", repo) os.Setenv("PLUGIN_REGISTRY", registry) os.Setenv("DOCKER_USERNAME", username) os.Setenv("DOCKER_PASSWORD", password) @@ -48,3 +55,13 @@ func main() { os.Exit(1) } } + +func getenv(key ...string) (s string) { + for _, k := range key { + s = os.Getenv(k) + if s != "" { + return + } + } + return +} diff --git a/cmd/drone-docker-heroku/main.go b/cmd/drone-docker-heroku/main.go index 5d8a616..57a2ae8 100644 --- a/cmd/drone-docker-heroku/main.go +++ b/cmd/drone-docker-heroku/main.go @@ -9,22 +9,14 @@ import ( func main() { var ( registry = "registry.heroku.com" - process = os.Getenv("PLUGIN_PROCESS_TYPE") - app = os.Getenv("PLUGIN_APP") - email = os.Getenv("PLUGIN_EMAIL") - key = os.Getenv("PLUGIN_API_KEY") + process = getenv("PLUGIN_PROCESS_TYPE") + app = getenv("PLUGIN_APP") + email = getenv("PLUGIN_EMAIL", "HEROKU_EMAIL") + key = getenv("PLUGIN_API_KEY", "HEROKU_API_KEY") ) - // if the heroku email is provided as a named secret - // then we should use it. - if os.Getenv("HEROKU_EMAIL") != "" { - email = os.Getenv("HEROKU_EMAIL") - } - - // if the heroku api key is provided as a named secret - // then we should use it. - if os.Getenv("HEROKU_API_KEY") != "" { - key = os.Getenv("HEROKU_API_KEY") + if process == "" { + process = "web" } os.Setenv("PLUGIN_REGISTRY", registry) @@ -34,7 +26,6 @@ func main() { os.Setenv("DOCKER_USERNAME", email) os.Setenv("DOCKER_EMAIL", email) - // invoke the base docker plugin binary cmd := exec.Command("drone-docker") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -43,3 +34,13 @@ func main() { os.Exit(1) } } + +func getenv(key ...string) (s string) { + for _, k := range key { + s = os.Getenv(k) + if s != "" { + return + } + } + return +}