mirror of
https://github.com/harness-community/drone-helm-chart-container-registry.git
synced 2026-06-04 18:24:12 +08:00
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"helm.sh/helm/v3/pkg/action"
|
|
"helm.sh/helm/v3/pkg/cli"
|
|
"helm.sh/helm/v3/pkg/downloader"
|
|
"helm.sh/helm/v3/pkg/registry"
|
|
)
|
|
|
|
func main() {
|
|
registryUrl := os.Getenv("PLUGIN_REGISTRY_URL")
|
|
username := os.Getenv("PLUGIN_REGISTRY_USERNAME")
|
|
token := os.Getenv("PLUGIN_REGISTRY_PASSWORD")
|
|
chartPath := os.Getenv("PLUGIN_CHART_PATH")
|
|
namespace := os.Getenv("PLUGIN_REGISTRY_NAMESPACE")
|
|
|
|
if (registryUrl == "") || (username == "") || (token == "") || (namespace == "") || (chartPath == "") {
|
|
fmt.Println("Missing required environment variables")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// package chart
|
|
helmClient := action.NewPackage()
|
|
helmClient.DependencyUpdate = true
|
|
helmClient.Destination = chartPath
|
|
|
|
downloadManager := &downloader.Manager{
|
|
Out: os.Stdout,
|
|
ChartPath: chartPath,
|
|
Debug: true,
|
|
}
|
|
|
|
if err := downloadManager.Build(); err != nil {
|
|
fmt.Printf("Failed to retrieve chart in %s", chartPath)
|
|
os.Exit(1)
|
|
}
|
|
|
|
packageRun, err := helmClient.Run(chartPath, nil)
|
|
if err != nil {
|
|
fmt.Printf("Failed to package chart in %s", chartPath)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("Successfully packaged chart in %s\n", chartPath)
|
|
|
|
opts := []registry.ClientOption{
|
|
registry.ClientOptWriter(os.Stdout),
|
|
}
|
|
|
|
registryClient, err := registry.NewClient(opts...)
|
|
if err != nil {
|
|
fmt.Println("Failed to create registry client")
|
|
os.Exit(1)
|
|
}
|
|
|
|
cfg := new(action.Configuration)
|
|
cfg.RegistryClient = registryClient
|
|
|
|
action.NewRegistryLogin(cfg).Run(
|
|
os.Stdout,
|
|
registryUrl,
|
|
username,
|
|
token,
|
|
)
|
|
|
|
// Push the chart
|
|
client := action.NewPushWithOpts(action.WithPushConfig(cfg))
|
|
|
|
settings := new(cli.EnvSettings)
|
|
client.Settings = settings
|
|
|
|
ociURL := "oci://" + registryUrl + "/" + namespace
|
|
|
|
_, err = client.Run(packageRun, ociURL)
|
|
if err != nil {
|
|
fmt.Println("Failed to push chart")
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("Successfully pushed chart to %s", ociURL)
|
|
}
|