woodpecker-docker-buildx/plugin/login.go
6543 25ad50a579 Add support for multible registrys (#23)
if normal settings `registry`, `username`... is set

that's used as default, else first logins entry is used.

```yml
settings:
  logins:
    - registry: https://codeberg.org
      username: "6543"
      password: geheim
    - registry: https://index.docker.io/v1/
      username: a6543
      password: anders_geheim
```

close #18

Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/23
Reviewed-by: anbraten <anbraten@noreply.codeberg.org>
2022-10-08 16:02:02 +02:00

37 lines
737 B
Go

package plugin
import (
"fmt"
"os/exec"
)
// login to the registrys
func (p *Plugin) Login() error {
registrys := make(map[string]bool)
for _, login := range p.settings.Logins {
if !registrys[login.Registry] && !login.anonymous() {
// only log into a registry once
registrys[login.Registry] = true
cmd := commandLogin(login)
err := cmd.Run()
if err != nil {
return fmt.Errorf("error authenticating: %s", err)
}
}
}
return nil
}
// helper function to create the docker login command.
func commandLogin(login Login) *exec.Cmd {
if login.Email != "" {
return commandLoginEmail(login)
}
return exec.Command(
dockerExe, "login",
"-u", login.Username,
"-p", login.Password,
login.Registry,
)
}