From 601a872acc9ecf4593e63f42c20ca1ad375829aa Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 11 Aug 2023 10:03:20 +0200 Subject: [PATCH] Revert "refactor: use parameter list for multiple registries (#309)" This reverts commit 0cc14f4114d8d8aefeff36df53f74e5ba5449973. --- _docs/content/_index.md | 23 ++++++++++----- _docs/data/data.yaml | 6 ++-- cmd/drone-docker-buildx/config.go | 5 ++-- go.mod | 8 +---- go.sum | 1 - plugin/impl.go | 49 ++++++++++++++++--------------- plugin/impl_test.go | 23 --------------- 7 files changed, 47 insertions(+), 68 deletions(-) delete mode 100644 plugin/impl_test.go diff --git a/_docs/content/_index.md b/_docs/content/_index.md index d340bc0..d9ecb25 100644 --- a/_docs/content/_index.md +++ b/_docs/content/_index.md @@ -95,7 +95,7 @@ steps: tags: latest ``` -#### Push to multiple registries +**Multiple registries:** ```yaml kind: pipeline @@ -105,19 +105,26 @@ steps: - name: docker image: thegeeklab/drone-docker-buildx:23 privileged: true + environment: + DOCKER_REGISTRY_PASSWORD: + from_secret: docker_registry_password + GITHUB_REGISTRY_PASSWORD: + from_secret: github_registry_password settings: - repo: + repo: - octocat/example - ghcr.io/octocat/example tags: latest - registries: - - username: octocat - password: docker-password - - registry: ghcr.io - username: octocat - password: ghrc-password + registries: | + registries: + - username: "octocat" + password: "$DOCKER_REGISTRY_PASSWORD" + - registry: "ghcr.io" + username: "octocat" + password: "$GITHUB_REGISTRY_PASSWORD" ``` + ## Build Build the binary with the following command: diff --git a/_docs/data/data.yaml b/_docs/data/data.yaml index de32ada..fea75e6 100644 --- a/_docs/data/data.yaml +++ b/_docs/data/data.yaml @@ -295,6 +295,6 @@ properties: required: false - name: registries - description: List of registry credentials. Check out the Examples for more information. - type: list - required: false + description: Credentials for multiple registries described in YAML format. Check out the Examples for more information. + type: string + required: false \ No newline at end of file diff --git a/cmd/drone-docker-buildx/config.go b/cmd/drone-docker-buildx/config.go index 4a7950c..afb3d8d 100644 --- a/cmd/drone-docker-buildx/config.go +++ b/cmd/drone-docker-buildx/config.go @@ -246,7 +246,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Name: "docker.registry", EnvVars: []string{"PLUGIN_REGISTRY", "DOCKER_REGISTRY"}, Usage: "docker registry to authenticate with", - Value: plugin.DefaultRegistry, + Value: "https://index.docker.io/v1/", Destination: &settings.Login.Registry, Category: category, }, @@ -332,8 +332,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Name: "docker.registries", EnvVars: []string{"PLUGIN_REGISTRIES"}, Usage: "credentials for registries", - Value: "[]", - Destination: &settings.Login.RegistriesRaw, + Destination: &settings.Login.RegistriesYaml, Category: category, }, } diff --git a/go.mod b/go.mod index 2d6ca08..f643a76 100644 --- a/go.mod +++ b/go.mod @@ -6,20 +6,14 @@ require ( github.com/coreos/go-semver v0.3.1 github.com/joho/godotenv v1.5.1 github.com/sirupsen/logrus v1.9.3 - github.com/stretchr/testify v1.7.0 github.com/thegeeklab/drone-plugin-lib/v2 v2.3.4 github.com/urfave/cli/v2 v2.25.5 golang.org/x/sys v0.11.0 ) -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect -) - require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index aff082e..4bb7288 100644 --- a/go.sum +++ b/go.sum @@ -25,7 +25,6 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/plugin/impl.go b/plugin/impl.go index 57cb805..ca82cf7 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -1,7 +1,6 @@ package plugin import ( - "encoding/json" "fmt" "os" "path/filepath" @@ -10,6 +9,7 @@ import ( "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" "golang.org/x/sys/execabs" + "gopkg.in/yaml.v3" ) // Daemon defines Docker daemon parameters. @@ -33,9 +33,8 @@ type Daemon struct { // Login defines Docker login parameters. type Login struct { RegistryData - Config string - Registries []RegistryData - RegistriesRaw string + Config string // Docker Auth Config + RegistriesYaml string // Docker Auth with YAML config } type RegistryData struct { @@ -45,6 +44,10 @@ type RegistryData struct { Email string // Docker registry email } +type RegistriesYaml struct { + Registries []RegistryData `yaml:"registries"` +} + // Build defines Docker build parameters. type Build struct { Ref string // Git commit ref @@ -83,10 +86,7 @@ type Settings struct { Dryrun bool } -const ( - strictFilePerm = 0o600 - DefaultRegistry = "https://index.docker.io/v1/" -) +const strictFilePerm = 0o600 // Validate handles the settings validation of the plugin. func (p *Plugin) Validate() error { @@ -118,16 +118,6 @@ func (p *Plugin) Validate() error { } } - if err := json.Unmarshal([]byte(p.settings.Login.RegistriesRaw), &p.settings.Login.Registries); err != nil { - return fmt.Errorf("error unmarshal registries: %w", err) - } - - for i, registryData := range p.settings.Login.Registries { - if registryData.Registry == "" { - p.settings.Login.Registries[i].Registry = DefaultRegistry - } - } - return nil } @@ -195,12 +185,25 @@ func (p *Plugin) Execute() error { } } - for _, registryData := range p.settings.Login.Registries { - cmd := commandLogin(registryData) + if p.settings.Login.RegistriesYaml != "" { + var t RegistriesYaml - err := cmd.Run() + err := yaml.Unmarshal([]byte(p.settings.Login.RegistriesYaml), &t) if err != nil { - return fmt.Errorf("error authenticating: %w", err) + return fmt.Errorf("error unmarshal registries: %w", err) + } + + for _, registryData := range t.Registries { + if registryData.Registry == "" { + registryData.Registry = "https://index.docker.io/v1/" + } + + cmd := commandLogin(registryData) + + err := cmd.Run() + if err != nil { + return fmt.Errorf("error authenticating: %w", err) + } } } @@ -214,7 +217,7 @@ func (p *Plugin) Execute() error { switch { case p.settings.Login.Password != "": logrus.Info("Detected registry credentials") - case len(p.settings.Login.Registries) > 0: + case p.settings.Login.RegistriesYaml != "": logrus.Info("Detected multiple registry credentials") case p.settings.Login.Config != "": logrus.Info("Detected registry credentials file") diff --git a/plugin/impl_test.go b/plugin/impl_test.go deleted file mode 100644 index 8026140..0000000 --- a/plugin/impl_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package plugin - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestRegistries(t *testing.T) { - p := &Plugin{} - p.settings.Login.RegistriesRaw = `[{"username": "docker_user", "password": "docker_password"}]` - - assert.NoError(t, p.Validate()) - - fmt.Println(p.settings.Login.Registries[0].Password) - - if assert.Len(t, p.settings.Login.Registries, 1) { - assert.EqualValues(t, "docker_user", p.settings.Login.Registries[0].Username) - assert.EqualValues(t, "docker_password", p.settings.Login.Registries[0].Password) - assert.EqualValues(t, DefaultRegistry, p.settings.Login.Registries[0].Registry) - } -}