2021-01-11 20:54:49 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2023-08-10 21:27:49 +00:00
|
|
|
"encoding/json"
|
2021-01-11 20:54:49 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
2023-02-08 09:13:28 +00:00
|
|
|
"golang.org/x/sys/execabs"
|
2021-01-11 20:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Daemon defines Docker daemon parameters.
|
|
|
|
type Daemon struct {
|
2021-07-08 07:00:51 +00:00
|
|
|
Registry string // Docker registry
|
|
|
|
Mirror string // Docker registry mirror
|
|
|
|
Insecure bool // Docker daemon enable insecure registries
|
|
|
|
StorageDriver string // Docker daemon storage driver
|
|
|
|
StoragePath string // Docker daemon storage path
|
|
|
|
Disabled bool // DOcker daemon is disabled (already running)
|
|
|
|
Debug bool // Docker daemon started in debug mode
|
|
|
|
Bip string // Docker daemon network bridge IP address
|
|
|
|
DNS cli.StringSlice // Docker daemon dns server
|
|
|
|
DNSSearch cli.StringSlice // Docker daemon dns search domain
|
|
|
|
MTU string // Docker daemon mtu setting
|
|
|
|
IPv6 bool // Docker daemon IPv6 networking
|
|
|
|
Experimental bool // Docker daemon enable experimental mode
|
2021-07-25 12:28:33 +00:00
|
|
|
BuildkitConfig string // Docker buildkit config
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login defines Docker login parameters.
|
|
|
|
type Login struct {
|
2023-08-09 09:35:58 +00:00
|
|
|
RegistryData
|
2023-08-10 21:27:49 +00:00
|
|
|
Config string
|
|
|
|
Registries []RegistryData
|
|
|
|
RegistriesRaw string
|
2023-08-09 09:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RegistryData struct {
|
2021-01-11 20:54:49 +00:00
|
|
|
Registry string // Docker registry address
|
|
|
|
Username string // Docker registry username
|
|
|
|
Password string // Docker registry password
|
|
|
|
Email string // Docker registry email
|
2023-08-09 09:35:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
// Build defines Docker build parameters.
|
|
|
|
type Build struct {
|
2022-07-24 10:48:02 +00:00
|
|
|
Ref string // Git commit ref
|
|
|
|
Branch string // Git repository branch
|
|
|
|
Dockerfile string // Docker build Dockerfile
|
|
|
|
Context string // Docker build context
|
|
|
|
TagsAuto bool // Docker build auto tag
|
|
|
|
TagsSuffix string // Docker build tags with suffix
|
|
|
|
Tags cli.StringSlice // Docker build tags
|
2022-10-09 12:05:05 +00:00
|
|
|
ExtraTags cli.StringSlice // Docker build tags including registry
|
2022-07-24 10:48:02 +00:00
|
|
|
Platforms cli.StringSlice // Docker build target platforms
|
|
|
|
Args cli.StringSlice // Docker build args
|
|
|
|
ArgsEnv cli.StringSlice // Docker build args from env
|
|
|
|
Target string // Docker build target
|
|
|
|
Pull bool // Docker build pull
|
2022-10-31 19:10:18 +00:00
|
|
|
CacheFrom []string // Docker build cache-from
|
2022-08-08 11:36:23 +00:00
|
|
|
CacheTo string // Docker build cache-to
|
2022-07-24 10:48:02 +00:00
|
|
|
Compress bool // Docker build compress
|
2023-08-09 09:35:58 +00:00
|
|
|
Repo cli.StringSlice // Docker build repositories
|
2022-07-24 10:48:02 +00:00
|
|
|
NoCache bool // Docker build no-cache
|
|
|
|
AddHost cli.StringSlice // Docker build add-host
|
|
|
|
Quiet bool // Docker build quiet
|
|
|
|
Output string // Docker build output folder
|
|
|
|
NamedContext cli.StringSlice // Docker build named context
|
2022-09-20 19:35:29 +00:00
|
|
|
Labels cli.StringSlice // Docker build labels
|
2023-01-12 10:28:22 +00:00
|
|
|
Provenance string // Docker build provenance attestation
|
2023-03-08 14:51:24 +00:00
|
|
|
SBOM string // Docker build sbom attestation
|
2023-06-29 19:16:20 +00:00
|
|
|
Secrets []string // Docker build secrets
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Settings for the Plugin.
|
|
|
|
type Settings struct {
|
2022-11-01 13:04:49 +00:00
|
|
|
Daemon Daemon
|
|
|
|
Login Login
|
|
|
|
Build Build
|
|
|
|
Dryrun bool
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 21:27:49 +00:00
|
|
|
const (
|
|
|
|
strictFilePerm = 0o600
|
|
|
|
DefaultRegistry = "https://index.docker.io/v1/"
|
|
|
|
)
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
// Validate handles the settings validation of the plugin.
|
|
|
|
func (p *Plugin) Validate() error {
|
2021-01-11 21:38:17 +00:00
|
|
|
p.settings.Build.Branch = p.pipeline.Repo.Branch
|
|
|
|
p.settings.Build.Ref = p.pipeline.Commit.Ref
|
2021-01-11 20:54:49 +00:00
|
|
|
p.settings.Daemon.Registry = p.settings.Login.Registry
|
|
|
|
|
|
|
|
if p.settings.Build.TagsAuto {
|
|
|
|
// return true if tag event or default branch
|
|
|
|
if UseDefaultTag(
|
|
|
|
p.settings.Build.Ref,
|
|
|
|
p.settings.Build.Branch,
|
|
|
|
) {
|
|
|
|
tag, err := DefaultTagSuffix(
|
|
|
|
p.settings.Build.Ref,
|
|
|
|
p.settings.Build.TagsSuffix,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-06-02 13:00:33 +00:00
|
|
|
logrus.Infof("cannot generate tags from %s, invalid semantic version", p.settings.Build.Ref)
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
p.settings.Build.Tags = *cli.NewStringSlice(tag...)
|
|
|
|
} else {
|
2022-06-02 13:00:33 +00:00
|
|
|
logrus.Infof("skip auto-tagging for %s, not on default branch or tag", p.settings.Build.Ref)
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 21:27:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute provides the implementation of the plugin.
|
2023-03-24 13:04:29 +00:00
|
|
|
//
|
|
|
|
//nolint:gocognit
|
2021-01-11 20:54:49 +00:00
|
|
|
func (p *Plugin) Execute() error {
|
|
|
|
// start the Docker daemon server
|
2023-03-24 13:04:29 +00:00
|
|
|
//nolint: nestif
|
2021-01-11 20:54:49 +00:00
|
|
|
if !p.settings.Daemon.Disabled {
|
2023-03-24 13:04:29 +00:00
|
|
|
// If no custom DNS value set start internal DNS server
|
|
|
|
if len(p.settings.Daemon.DNS.Value()) == 0 {
|
|
|
|
ip, err := getContainerIP()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("error detecting IP address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip != "" {
|
|
|
|
logrus.Debugf("discovered IP address: %v", ip)
|
|
|
|
p.startCoredns()
|
|
|
|
|
|
|
|
if err := p.settings.Daemon.DNS.Set(ip); err != nil {
|
|
|
|
return fmt.Errorf("error setting daemon dns: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
p.startDaemon()
|
|
|
|
}
|
|
|
|
|
|
|
|
// poll the docker daemon until it is started. This ensures the daemon is
|
|
|
|
// ready to accept connections before we proceed.
|
|
|
|
for i := 0; i < 15; i++ {
|
|
|
|
cmd := commandInfo()
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Auth Config File
|
|
|
|
if p.settings.Login.Config != "" {
|
2023-02-08 09:13:28 +00:00
|
|
|
if err := os.MkdirAll(dockerHome, strictFilePerm); err != nil {
|
|
|
|
return fmt.Errorf("failed to create docker home: %w", err)
|
2022-04-25 10:51:42 +00:00
|
|
|
}
|
2021-01-11 20:54:49 +00:00
|
|
|
|
|
|
|
path := filepath.Join(dockerHome, "config.json")
|
2023-02-08 09:13:28 +00:00
|
|
|
|
|
|
|
err := os.WriteFile(path, []byte(p.settings.Login.Config), strictFilePerm)
|
2021-01-11 20:54:49 +00:00
|
|
|
if err != nil {
|
2023-02-08 09:13:28 +00:00
|
|
|
return fmt.Errorf("error writing config.json: %w", err)
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// login to the Docker registry
|
|
|
|
if p.settings.Login.Password != "" {
|
2023-08-09 09:35:58 +00:00
|
|
|
cmd := commandLogin(p.settings.Login.RegistryData)
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2023-02-08 09:13:28 +00:00
|
|
|
return fmt.Errorf("error authenticating: %w", err)
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 21:27:49 +00:00
|
|
|
for _, registryData := range p.settings.Login.Registries {
|
|
|
|
cmd := commandLogin(registryData)
|
2023-08-09 09:35:58 +00:00
|
|
|
|
2023-08-10 21:27:49 +00:00
|
|
|
err := cmd.Run()
|
2023-08-09 09:35:58 +00:00
|
|
|
if err != nil {
|
2023-08-10 21:27:49 +00:00
|
|
|
return fmt.Errorf("error authenticating: %w", err)
|
2023-08-09 09:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 12:28:33 +00:00
|
|
|
if p.settings.Daemon.BuildkitConfig != "" {
|
2023-02-08 09:13:28 +00:00
|
|
|
err := os.WriteFile(buildkitConfig, []byte(p.settings.Daemon.BuildkitConfig), strictFilePerm)
|
2021-07-25 12:28:33 +00:00
|
|
|
if err != nil {
|
2023-02-08 09:13:28 +00:00
|
|
|
return fmt.Errorf("error writing buildkit.toml: %w", err)
|
2021-07-25 12:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
switch {
|
|
|
|
case p.settings.Login.Password != "":
|
2023-02-08 09:13:28 +00:00
|
|
|
logrus.Info("Detected registry credentials")
|
2023-08-10 21:27:49 +00:00
|
|
|
case len(p.settings.Login.Registries) > 0:
|
2023-08-09 09:35:58 +00:00
|
|
|
logrus.Info("Detected multiple registry credentials")
|
2021-01-11 20:54:49 +00:00
|
|
|
case p.settings.Login.Config != "":
|
2023-02-08 09:13:28 +00:00
|
|
|
logrus.Info("Detected registry credentials file")
|
2021-01-11 20:54:49 +00:00
|
|
|
default:
|
2023-02-08 09:13:28 +00:00
|
|
|
logrus.Info("Registry credentials or Docker config not provided. Guest mode enabled.")
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add proxy build args
|
|
|
|
addProxyBuildArgs(&p.settings.Build)
|
|
|
|
|
2023-02-08 09:13:28 +00:00
|
|
|
var cmds []*execabs.Cmd
|
2021-01-11 20:54:49 +00:00
|
|
|
cmds = append(cmds, commandVersion()) // docker version
|
|
|
|
cmds = append(cmds, commandInfo()) // docker info
|
2021-07-08 07:00:51 +00:00
|
|
|
cmds = append(cmds, commandBuilder(p.settings.Daemon))
|
2021-01-11 20:54:49 +00:00
|
|
|
cmds = append(cmds, commandBuildx())
|
|
|
|
|
2021-07-02 16:58:55 +00:00
|
|
|
cmds = append(cmds, commandBuild(p.settings.Build, p.settings.Dryrun)) // docker build
|
2021-01-11 20:54:49 +00:00
|
|
|
|
|
|
|
// execute all commands in batch mode.
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
trace(cmd)
|
|
|
|
|
|
|
|
err := cmd.Run()
|
2022-08-08 11:36:23 +00:00
|
|
|
if err != nil {
|
2021-01-11 20:54:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|