mirror of
https://github.com/thegeeklab/wp-docker-buildx.git
synced 2024-11-24 12:50:41 +00:00
refactor: use parameter list for multiple registries (#309)
This commit is contained in:
parent
6d13cf910f
commit
0cc14f4114
@ -95,7 +95,7 @@ steps:
|
|||||||
tags: latest
|
tags: latest
|
||||||
```
|
```
|
||||||
|
|
||||||
**Multiple registries:**
|
#### Push to multiple registries
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
@ -105,26 +105,19 @@ steps:
|
|||||||
- name: docker
|
- name: docker
|
||||||
image: thegeeklab/drone-docker-buildx:23
|
image: thegeeklab/drone-docker-buildx:23
|
||||||
privileged: true
|
privileged: true
|
||||||
environment:
|
|
||||||
DOCKER_REGISTRY_PASSWORD:
|
|
||||||
from_secret: docker_registry_password
|
|
||||||
GITHUB_REGISTRY_PASSWORD:
|
|
||||||
from_secret: github_registry_password
|
|
||||||
settings:
|
settings:
|
||||||
repo:
|
repo:
|
||||||
- octocat/example
|
- octocat/example
|
||||||
- ghcr.io/octocat/example
|
- ghcr.io/octocat/example
|
||||||
tags: latest
|
tags: latest
|
||||||
registries: |
|
registries:
|
||||||
registries:
|
- username: octocat
|
||||||
- username: "octocat"
|
password: docker-password
|
||||||
password: "$DOCKER_REGISTRY_PASSWORD"
|
- registry: ghcr.io
|
||||||
- registry: "ghcr.io"
|
username: octocat
|
||||||
username: "octocat"
|
password: ghrc-password
|
||||||
password: "$GITHUB_REGISTRY_PASSWORD"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|
||||||
Build the binary with the following command:
|
Build the binary with the following command:
|
||||||
|
@ -295,6 +295,6 @@ properties:
|
|||||||
required: false
|
required: false
|
||||||
|
|
||||||
- name: registries
|
- name: registries
|
||||||
description: Credentials for multiple registries described in YAML format. Check out the Examples for more information.
|
description: List of registry credentials. Check out the Examples for more information.
|
||||||
type: string
|
type: list
|
||||||
required: false
|
required: false
|
||||||
|
@ -246,7 +246,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
|
|||||||
Name: "docker.registry",
|
Name: "docker.registry",
|
||||||
EnvVars: []string{"PLUGIN_REGISTRY", "DOCKER_REGISTRY"},
|
EnvVars: []string{"PLUGIN_REGISTRY", "DOCKER_REGISTRY"},
|
||||||
Usage: "docker registry to authenticate with",
|
Usage: "docker registry to authenticate with",
|
||||||
Value: "https://index.docker.io/v1/",
|
Value: plugin.DefaultRegistry,
|
||||||
Destination: &settings.Login.Registry,
|
Destination: &settings.Login.Registry,
|
||||||
Category: category,
|
Category: category,
|
||||||
},
|
},
|
||||||
@ -332,7 +332,8 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
|
|||||||
Name: "docker.registries",
|
Name: "docker.registries",
|
||||||
EnvVars: []string{"PLUGIN_REGISTRIES"},
|
EnvVars: []string{"PLUGIN_REGISTRIES"},
|
||||||
Usage: "credentials for registries",
|
Usage: "credentials for registries",
|
||||||
Destination: &settings.Login.RegistriesYaml,
|
Value: "[]",
|
||||||
|
Destination: &settings.Login.RegistriesRaw,
|
||||||
Category: category,
|
Category: category,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
8
go.mod
8
go.mod
@ -6,14 +6,20 @@ require (
|
|||||||
github.com/coreos/go-semver v0.3.1
|
github.com/coreos/go-semver v0.3.1
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/sirupsen/logrus v1.9.3
|
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/thegeeklab/drone-plugin-lib/v2 v2.3.4
|
||||||
github.com/urfave/cli/v2 v2.25.5
|
github.com/urfave/cli/v2 v2.25.5
|
||||||
golang.org/x/sys v0.11.0
|
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 (
|
require (
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
1
go.sum
1
go.sum
@ -25,6 +25,7 @@ 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.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 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
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/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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -9,7 +10,6 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/sys/execabs"
|
"golang.org/x/sys/execabs"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Daemon defines Docker daemon parameters.
|
// Daemon defines Docker daemon parameters.
|
||||||
@ -33,8 +33,9 @@ type Daemon struct {
|
|||||||
// Login defines Docker login parameters.
|
// Login defines Docker login parameters.
|
||||||
type Login struct {
|
type Login struct {
|
||||||
RegistryData
|
RegistryData
|
||||||
Config string // Docker Auth Config
|
Config string
|
||||||
RegistriesYaml string // Docker Auth with YAML config
|
Registries []RegistryData
|
||||||
|
RegistriesRaw string
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegistryData struct {
|
type RegistryData struct {
|
||||||
@ -44,10 +45,6 @@ type RegistryData struct {
|
|||||||
Email string // Docker registry email
|
Email string // Docker registry email
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegistriesYaml struct {
|
|
||||||
Registries []RegistryData `yaml:"registries"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build defines Docker build parameters.
|
// Build defines Docker build parameters.
|
||||||
type Build struct {
|
type Build struct {
|
||||||
Ref string // Git commit ref
|
Ref string // Git commit ref
|
||||||
@ -86,7 +83,10 @@ type Settings struct {
|
|||||||
Dryrun bool
|
Dryrun bool
|
||||||
}
|
}
|
||||||
|
|
||||||
const strictFilePerm = 0o600
|
const (
|
||||||
|
strictFilePerm = 0o600
|
||||||
|
DefaultRegistry = "https://index.docker.io/v1/"
|
||||||
|
)
|
||||||
|
|
||||||
// Validate handles the settings validation of the plugin.
|
// Validate handles the settings validation of the plugin.
|
||||||
func (p *Plugin) Validate() error {
|
func (p *Plugin) Validate() error {
|
||||||
@ -118,6 +118,16 @@ 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,25 +195,12 @@ func (p *Plugin) Execute() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.settings.Login.RegistriesYaml != "" {
|
for _, registryData := range p.settings.Login.Registries {
|
||||||
var t RegistriesYaml
|
cmd := commandLogin(registryData)
|
||||||
|
|
||||||
err := yaml.Unmarshal([]byte(p.settings.Login.RegistriesYaml), &t)
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error unmarshal registries: %w", err)
|
return fmt.Errorf("error authenticating: %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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,7 +214,7 @@ func (p *Plugin) Execute() error {
|
|||||||
switch {
|
switch {
|
||||||
case p.settings.Login.Password != "":
|
case p.settings.Login.Password != "":
|
||||||
logrus.Info("Detected registry credentials")
|
logrus.Info("Detected registry credentials")
|
||||||
case p.settings.Login.RegistriesYaml != "":
|
case len(p.settings.Login.Registries) > 0:
|
||||||
logrus.Info("Detected multiple registry credentials")
|
logrus.Info("Detected multiple registry credentials")
|
||||||
case p.settings.Login.Config != "":
|
case p.settings.Login.Config != "":
|
||||||
logrus.Info("Detected registry credentials file")
|
logrus.Info("Detected registry credentials file")
|
||||||
|
23
plugin/impl_test.go
Normal file
23
plugin/impl_test.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user