@@ -3,6 +3,8 @@ package main
33import (
44 "context"
55 "encoding/base64"
6+ "fmt"
7+ "log"
68 "os"
79 "os/exec"
810 "path"
@@ -16,45 +18,80 @@ import (
1618 docker "github.com/drone-plugins/drone-docker"
1719)
1820
19- // gcr default username
20- var username = "_json_key"
21+ type Config struct {
22+ Repo string
23+ Registry string
24+ Password string
25+ WorkloadIdentity bool
26+ Username string
27+ RegistryType string
28+ }
2129
22- func main () {
23- // Load env-file if it exists first
30+ func loadConfig () Config {
31+ // Default username
32+ username := "_json_key"
33+
34+ // Load env-file if it exists
2435 if env := os .Getenv ("PLUGIN_ENV_FILE" ); env != "" {
25- godotenv .Load (env )
36+ if err := godotenv .Load (env ); err != nil {
37+ log .Fatalf ("Error loading .env file: %v" , err )
38+ }
2639 }
2740
28- var (
29- repo = getenv ("PLUGIN_REPO" )
30- registry = getenv ("PLUGIN_REGISTRY" )
31- password = getenv (
32- "PLUGIN_JSON_KEY" ,
33- "GCR_JSON_KEY" ,
34- "GOOGLE_CREDENTIALS" ,
35- "TOKEN" ,
36- )
37- workloadIdentity = parseBoolOrDefault (false , getenv ("PLUGIN_WORKLOAD_IDENTITY" ))
41+ location := getenv ("PLUGIN_LOCATION" )
42+ repo := getenv ("PLUGIN_REPO" )
43+
44+ password := getenv (
45+ "PLUGIN_JSON_KEY" ,
46+ "GCR_JSON_KEY" ,
47+ "GOOGLE_CREDENTIALS" ,
48+ "TOKEN" ,
3849 )
39- // set username and password
50+ workloadIdentity := parseBoolOrDefault ( false , getenv ( "PLUGIN_WORKLOAD_IDENTITY" ))
4051 username , password = setUsernameAndPassword (username , password , workloadIdentity )
41- // default registry value
52+
53+ registryType := getenv ("PLUGIN_REGISTRY_TYPE" )
54+ if registryType == "" {
55+ registryType = "GCR"
56+ }
57+
58+ registry := getenv ("PLUGIN_REGISTRY" )
4259 if registry == "" {
43- registry = "gcr.io"
60+ switch registryType {
61+ case "GCR" :
62+ registry = "gcr.io"
63+ case "GAR" :
64+ if location == "" {
65+ logrus .Fatalf ("Error: For REGISTRY_TYPE of GAR, LOCATION must be set" )
66+ }
67+ registry = fmt .Sprintf ("%s-docker.pkg.dev" , location )
68+ default :
69+ logrus .Fatalf ("Unsupported registry type: %s" , registryType )
70+ }
4471 }
4572
46- // must use the fully qualified repo name. If the
47- // repo name does not have the registry prefix we
48- // should prepend.
4973 if ! strings .HasPrefix (repo , registry ) {
5074 repo = path .Join (registry , repo )
5175 }
5276
53- os .Setenv ("PLUGIN_REPO" , repo )
54- os .Setenv ("PLUGIN_REGISTRY" , registry )
55- os .Setenv ("DOCKER_USERNAME" , username )
56- os .Setenv ("DOCKER_PASSWORD" , password )
57- os .Setenv ("PLUGIN_REGISTRY_TYPE" , "GCR" )
77+ return Config {
78+ Repo : repo ,
79+ Registry : registry ,
80+ Password : password ,
81+ WorkloadIdentity : workloadIdentity ,
82+ Username : username ,
83+ RegistryType : registryType ,
84+ }
85+ }
86+
87+ func main () {
88+ config := loadConfig ()
89+
90+ os .Setenv ("PLUGIN_REPO" , config .Repo )
91+ os .Setenv ("PLUGIN_REGISTRY" , config .Registry )
92+ os .Setenv ("DOCKER_USERNAME" , config .Username )
93+ os .Setenv ("DOCKER_PASSWORD" , config .Password )
94+ os .Setenv ("PLUGIN_REGISTRY_TYPE" , config .RegistryType )
5895
5996 // invoke the base docker plugin binary
6097 cmd := exec .Command (docker .GetDroneDockerExecCmd ())
0 commit comments