Skip to content

Commit c354cd6

Browse files
authored
ci-3011 - GAR support (#404)
* adds support for GAR
1 parent e55012b commit c354cd6

File tree

2 files changed

+84
-26
lines changed

2 files changed

+84
-26
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ docker run --rm \
113113
plugins/docker --dry-run
114114
```
115115

116+
### GAR (Google Artifact Registry)
117+
118+
```yaml
119+
kind: pipeline
120+
name: default
121+
type: docker
122+
123+
steps:
124+
- name: push-to-gar
125+
image: plugins/gcr
126+
pull: never
127+
settings:
128+
tag: latest
129+
repo: project-id/repo/image-name
130+
registry_type: GAR
131+
location: us
132+
json_key:
133+
from_secret: gcr_json_key
134+
```
135+
116136
## Developer Notes
117137
118138
- 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
137157
```
138158

139159
Create your pull request for the release. Get it merged then tag the release.
160+

cmd/drone-gcr/main.go

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
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

Comments
 (0)