Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

89 changes: 63 additions & 26 deletions cmd/drone-gcr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"encoding/base64"
"fmt"
"log"
"os"
"os/exec"
"path"
Expand All @@ -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

registryType := getenv("PLUGIN_REGISTRY_TYPE")
if registryType == "" {
registryType = "GCR"
}

registry := getenv("PLUGIN_REGISTRY")
if registry == "" {
registry = "gcr.io"
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())
Expand Down