Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 0cc14f4

Browse files
authored
refactor: use parameter list for multiple registries (#309)
1 parent 6d13cf9 commit 0cc14f4

File tree

7 files changed

+68
-47
lines changed

7 files changed

+68
-47
lines changed

_docs/content/_index.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ steps:
9595
tags: latest
9696
```
9797

98-
**Multiple registries:**
98+
#### Push to multiple registries
9999

100100
```yaml
101101
kind: pipeline
@@ -105,26 +105,19 @@ steps:
105105
- name: docker
106106
image: thegeeklab/drone-docker-buildx:23
107107
privileged: true
108-
environment:
109-
DOCKER_REGISTRY_PASSWORD:
110-
from_secret: docker_registry_password
111-
GITHUB_REGISTRY_PASSWORD:
112-
from_secret: github_registry_password
113108
settings:
114-
repo:
109+
repo:
115110
- octocat/example
116111
- ghcr.io/octocat/example
117112
tags: latest
118-
registries: |
119-
registries:
120-
- username: "octocat"
121-
password: "$DOCKER_REGISTRY_PASSWORD"
122-
- registry: "ghcr.io"
123-
username: "octocat"
124-
password: "$GITHUB_REGISTRY_PASSWORD"
113+
registries:
114+
- username: octocat
115+
password: docker-password
116+
- registry: ghcr.io
117+
username: octocat
118+
password: ghrc-password
125119
```
126120

127-
128121
## Build
129122

130123
Build the binary with the following command:

_docs/data/data.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,6 @@ properties:
295295
required: false
296296

297297
- name: registries
298-
description: Credentials for multiple registries described in YAML format. Check out the Examples for more information.
299-
type: string
300-
required: false
298+
description: List of registry credentials. Check out the Examples for more information.
299+
type: list
300+
required: false

cmd/drone-docker-buildx/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
246246
Name: "docker.registry",
247247
EnvVars: []string{"PLUGIN_REGISTRY", "DOCKER_REGISTRY"},
248248
Usage: "docker registry to authenticate with",
249-
Value: "https://index.docker.io/v1/",
249+
Value: plugin.DefaultRegistry,
250250
Destination: &settings.Login.Registry,
251251
Category: category,
252252
},
@@ -332,7 +332,8 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
332332
Name: "docker.registries",
333333
EnvVars: []string{"PLUGIN_REGISTRIES"},
334334
Usage: "credentials for registries",
335-
Destination: &settings.Login.RegistriesYaml,
335+
Value: "[]",
336+
Destination: &settings.Login.RegistriesRaw,
336337
Category: category,
337338
},
338339
}

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ require (
66
github.com/coreos/go-semver v0.3.1
77
github.com/joho/godotenv v1.5.1
88
github.com/sirupsen/logrus v1.9.3
9+
github.com/stretchr/testify v1.7.0
910
github.com/thegeeklab/drone-plugin-lib/v2 v2.3.4
1011
github.com/urfave/cli/v2 v2.25.5
1112
golang.org/x/sys v0.11.0
1213
)
1314

15+
require (
16+
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/pmezard/go-difflib v1.0.0 // indirect
18+
)
19+
1420
require (
1521
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
1622
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1723
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
18-
gopkg.in/yaml.v3 v3.0.1
24+
gopkg.in/yaml.v3 v3.0.1 // indirect
1925
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr
2525
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2626
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
2727
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2829
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2930
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3031
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

plugin/impl.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package plugin
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -9,7 +10,6 @@ import (
910
"github.com/sirupsen/logrus"
1011
"github.com/urfave/cli/v2"
1112
"golang.org/x/sys/execabs"
12-
"gopkg.in/yaml.v3"
1313
)
1414

1515
// Daemon defines Docker daemon parameters.
@@ -33,8 +33,9 @@ type Daemon struct {
3333
// Login defines Docker login parameters.
3434
type Login struct {
3535
RegistryData
36-
Config string // Docker Auth Config
37-
RegistriesYaml string // Docker Auth with YAML config
36+
Config string
37+
Registries []RegistryData
38+
RegistriesRaw string
3839
}
3940

4041
type RegistryData struct {
@@ -44,10 +45,6 @@ type RegistryData struct {
4445
Email string // Docker registry email
4546
}
4647

47-
type RegistriesYaml struct {
48-
Registries []RegistryData `yaml:"registries"`
49-
}
50-
5148
// Build defines Docker build parameters.
5249
type Build struct {
5350
Ref string // Git commit ref
@@ -86,7 +83,10 @@ type Settings struct {
8683
Dryrun bool
8784
}
8885

89-
const strictFilePerm = 0o600
86+
const (
87+
strictFilePerm = 0o600
88+
DefaultRegistry = "https://index.docker.io/v1/"
89+
)
9090

9191
// Validate handles the settings validation of the plugin.
9292
func (p *Plugin) Validate() error {
@@ -118,6 +118,16 @@ func (p *Plugin) Validate() error {
118118
}
119119
}
120120

121+
if err := json.Unmarshal([]byte(p.settings.Login.RegistriesRaw), &p.settings.Login.Registries); err != nil {
122+
return fmt.Errorf("error unmarshal registries: %w", err)
123+
}
124+
125+
for i, registryData := range p.settings.Login.Registries {
126+
if registryData.Registry == "" {
127+
p.settings.Login.Registries[i].Registry = DefaultRegistry
128+
}
129+
}
130+
121131
return nil
122132
}
123133

@@ -185,25 +195,12 @@ func (p *Plugin) Execute() error {
185195
}
186196
}
187197

188-
if p.settings.Login.RegistriesYaml != "" {
189-
var t RegistriesYaml
198+
for _, registryData := range p.settings.Login.Registries {
199+
cmd := commandLogin(registryData)
190200

191-
err := yaml.Unmarshal([]byte(p.settings.Login.RegistriesYaml), &t)
201+
err := cmd.Run()
192202
if err != nil {
193-
return fmt.Errorf("error unmarshal registries: %w", err)
194-
}
195-
196-
for _, registryData := range t.Registries {
197-
if registryData.Registry == "" {
198-
registryData.Registry = "https://index.docker.io/v1/"
199-
}
200-
201-
cmd := commandLogin(registryData)
202-
203-
err := cmd.Run()
204-
if err != nil {
205-
return fmt.Errorf("error authenticating: %w", err)
206-
}
203+
return fmt.Errorf("error authenticating: %w", err)
207204
}
208205
}
209206

@@ -217,7 +214,7 @@ func (p *Plugin) Execute() error {
217214
switch {
218215
case p.settings.Login.Password != "":
219216
logrus.Info("Detected registry credentials")
220-
case p.settings.Login.RegistriesYaml != "":
217+
case len(p.settings.Login.Registries) > 0:
221218
logrus.Info("Detected multiple registry credentials")
222219
case p.settings.Login.Config != "":
223220
logrus.Info("Detected registry credentials file")

plugin/impl_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package plugin
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestRegistries(t *testing.T) {
11+
p := &Plugin{}
12+
p.settings.Login.RegistriesRaw = `[{"username": "docker_user", "password": "docker_password"}]`
13+
14+
assert.NoError(t, p.Validate())
15+
16+
fmt.Println(p.settings.Login.Registries[0].Password)
17+
18+
if assert.Len(t, p.settings.Login.Registries, 1) {
19+
assert.EqualValues(t, "docker_user", p.settings.Login.Registries[0].Username)
20+
assert.EqualValues(t, "docker_password", p.settings.Login.Registries[0].Password)
21+
assert.EqualValues(t, DefaultRegistry, p.settings.Login.Registries[0].Registry)
22+
}
23+
}

0 commit comments

Comments
 (0)