ci-3011 - GAR support (#404)

* adds support for GAR
This commit is contained in:
Eoin McAfee
2023-09-20 14:17:12 +01:00
committed by GitHub
parent e55012b8de
commit c354cd6a8d
2 changed files with 85 additions and 27 deletions
+21
View File
@@ -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.
+64 -27
View File
@@ -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())