From c354cd6a8d15d492e29588dfe5c9137f8da9ed2a Mon Sep 17 00:00:00 2001 From: Eoin McAfee <83226740+eoinmcafee00@users.noreply.github.com> Date: Wed, 20 Sep 2023 14:17:12 +0100 Subject: [PATCH] ci-3011 - GAR support (#404) * adds support for GAR --- README.md | 21 ++++++++++ cmd/drone-gcr/main.go | 91 ++++++++++++++++++++++++++++++------------- 2 files changed, 85 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a94b421..275f7d8 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,26 @@ docker run --rm \ plugins/docker --dry-run ``` +### GAR (Google Artifact Registry) + +```yaml +kind: pipeline +name: default +type: docker + +steps: + - name: push-to-gar + image: plugins/gcr + pull: never + settings: + tag: latest + repo: project-id/repo/image-name + registry_type: GAR + location: us + json_key: + from_secret: gcr_json_key +``` + ## Developer Notes - When updating the base image, you will need to update for each architecture and OS. @@ -137,3 +157,4 @@ docker run -it --rm -v "$(pwd)":/usr/local/src/your-app githubchangeloggenerator ``` Create your pull request for the release. Get it merged then tag the release. + diff --git a/cmd/drone-gcr/main.go b/cmd/drone-gcr/main.go index a77b0d7..a695319 100644 --- a/cmd/drone-gcr/main.go +++ b/cmd/drone-gcr/main.go @@ -3,6 +3,8 @@ package main import ( "context" "encoding/base64" + "fmt" + "log" "os" "os/exec" "path" @@ -16,45 +18,80 @@ import ( docker "github.com/drone-plugins/drone-docker" ) -// gcr default username -var username = "_json_key" +type Config struct { + Repo string + Registry string + Password string + WorkloadIdentity bool + Username string + RegistryType string +} -func main() { - // Load env-file if it exists first +func loadConfig() Config { + // Default username + username := "_json_key" + + // Load env-file if it exists if env := os.Getenv("PLUGIN_ENV_FILE"); env != "" { - godotenv.Load(env) + if err := godotenv.Load(env); err != nil { + log.Fatalf("Error loading .env file: %v", err) + } } - var ( - repo = getenv("PLUGIN_REPO") - registry = getenv("PLUGIN_REGISTRY") - password = getenv( - "PLUGIN_JSON_KEY", - "GCR_JSON_KEY", - "GOOGLE_CREDENTIALS", - "TOKEN", - ) - workloadIdentity = parseBoolOrDefault(false, getenv("PLUGIN_WORKLOAD_IDENTITY")) + location := getenv("PLUGIN_LOCATION") + repo := getenv("PLUGIN_REPO") + + password := getenv( + "PLUGIN_JSON_KEY", + "GCR_JSON_KEY", + "GOOGLE_CREDENTIALS", + "TOKEN", ) - // set username and password + workloadIdentity := parseBoolOrDefault(false, getenv("PLUGIN_WORKLOAD_IDENTITY")) username, password = setUsernameAndPassword(username, password, workloadIdentity) - // default registry value - if registry == "" { - registry = "gcr.io" + + registryType := getenv("PLUGIN_REGISTRY_TYPE") + if registryType == "" { + registryType = "GCR" + } + + registry := getenv("PLUGIN_REGISTRY") + if registry == "" { + switch registryType { + case "GCR": + registry = "gcr.io" + case "GAR": + if location == "" { + logrus.Fatalf("Error: For REGISTRY_TYPE of GAR, LOCATION must be set") + } + registry = fmt.Sprintf("%s-docker.pkg.dev", location) + default: + logrus.Fatalf("Unsupported registry type: %s", registryType) + } } - // must use the fully qualified repo name. If the - // repo name does not have the registry prefix we - // should prepend. if !strings.HasPrefix(repo, registry) { repo = path.Join(registry, repo) } - os.Setenv("PLUGIN_REPO", repo) - os.Setenv("PLUGIN_REGISTRY", registry) - os.Setenv("DOCKER_USERNAME", username) - os.Setenv("DOCKER_PASSWORD", password) - os.Setenv("PLUGIN_REGISTRY_TYPE", "GCR") + return Config{ + Repo: repo, + Registry: registry, + Password: password, + WorkloadIdentity: workloadIdentity, + Username: username, + RegistryType: registryType, + } +} + +func main() { + config := loadConfig() + + os.Setenv("PLUGIN_REPO", config.Repo) + os.Setenv("PLUGIN_REGISTRY", config.Registry) + os.Setenv("DOCKER_USERNAME", config.Username) + os.Setenv("DOCKER_PASSWORD", config.Password) + os.Setenv("PLUGIN_REGISTRY_TYPE", config.RegistryType) // invoke the base docker plugin binary cmd := exec.Command(docker.GetDroneDockerExecCmd())