2020-11-16 19:11:59 +01:00
|
|
|
package kaniko
|
2020-11-16 18:37:13 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-29 12:05:57 +02:00
|
|
|
"io/ioutil"
|
2020-11-16 18:37:13 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2021-04-29 12:05:57 +02:00
|
|
|
|
2021-10-18 13:36:53 +02:00
|
|
|
"github.com/drone/drone-kaniko/pkg/artifact"
|
2021-10-17 20:35:18 +02:00
|
|
|
"golang.org/x/mod/semver"
|
2020-11-16 18:37:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Build defines Docker build parameters.
|
|
|
|
Build struct {
|
2021-02-18 09:19:21 +01:00
|
|
|
Dockerfile string // Docker build Dockerfile
|
|
|
|
Context string // Docker build context
|
|
|
|
Tags []string // Docker build tags
|
2021-10-17 20:35:18 +02:00
|
|
|
AutoTag bool // Set this to create semver-tagged labels
|
2021-02-18 09:19:21 +01:00
|
|
|
Args []string // Docker build args
|
|
|
|
Target string // Docker build target
|
|
|
|
Repo string // Docker build repository
|
|
|
|
Labels []string // Label map
|
|
|
|
SkipTlsVerify bool // Docker skip tls certificate verify for registry
|
2021-03-02 16:45:44 +01:00
|
|
|
SnapshotMode string // Kaniko snapshot mode
|
2021-04-07 20:35:53 +02:00
|
|
|
EnableCache bool // Whether to enable kaniko cache
|
|
|
|
CacheRepo string // Remote repository that will be used to store cached layers
|
|
|
|
CacheTTL int // Cache timeout in hours
|
2021-04-29 12:05:57 +02:00
|
|
|
DigestFile string // Digest file location
|
2021-04-29 17:23:43 +02:00
|
|
|
NoPush bool // Set this flag if you only want to build the image, without pushing to a registry
|
2021-08-20 13:08:35 +02:00
|
|
|
Verbosity string // Log level
|
2021-12-01 12:40:28 +01:00
|
|
|
Platform string // Allows to build with another default platform than the host, similarly to docker build --platform
|
2021-04-29 12:05:57 +02:00
|
|
|
}
|
2021-10-17 20:35:18 +02:00
|
|
|
|
2021-04-29 12:05:57 +02:00
|
|
|
// Artifact defines content of artifact file
|
|
|
|
Artifact struct {
|
|
|
|
Tags []string // Docker artifact tags
|
|
|
|
Repo string // Docker artifact repository
|
|
|
|
Registry string // Docker artifact registry
|
|
|
|
RegistryType artifact.RegistryTypeEnum // Rocker artifact registry type
|
|
|
|
ArtifactFile string // Artifact file location
|
2020-11-16 18:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plugin defines the Docker plugin parameters.
|
|
|
|
Plugin struct {
|
2021-04-29 12:05:57 +02:00
|
|
|
Build Build // Docker build configuration
|
|
|
|
Artifact Artifact // Artifact file content
|
2020-11-16 18:37:13 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-17 20:35:18 +02:00
|
|
|
// labelsForTag returns the labels to use for the given tag, subject to the value of AutoTag.
|
|
|
|
//
|
|
|
|
// Build information (e.g. +linux_amd64) is carried through to all labels.
|
|
|
|
// Pre-release information (e.g. -rc1) suppresses major and major+minor auto-labels.
|
|
|
|
func (b Build) labelsForTag(tag string) (labels []string) {
|
|
|
|
// We strip "v" off of the beginning of semantic versions, as they are not used in docker tags
|
|
|
|
const VersionPrefix = "v"
|
|
|
|
|
|
|
|
// Semantic Versions don't allow underscores, so replace them with dashes.
|
|
|
|
// https://semver.org/
|
|
|
|
semverTag := strings.ReplaceAll(tag, "_", "-")
|
|
|
|
|
|
|
|
// Allow tags of the form "1.2.3" as well as "v1.2.3" to avoid confusion.
|
|
|
|
if withV := VersionPrefix + semverTag; !semver.IsValid(semverTag) && semver.IsValid(withV) {
|
|
|
|
semverTag = withV
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass through tags if auto-tag is not set, or if the tag is not a semantic version
|
|
|
|
if !b.AutoTag || !semver.IsValid(semverTag) {
|
|
|
|
return []string{tag}
|
|
|
|
}
|
|
|
|
tag = semverTag
|
|
|
|
|
|
|
|
// If the version is pre-release, only the full release should be tagged, not the major/minor versions.
|
|
|
|
if semver.Prerelease(tag) != "" {
|
|
|
|
return []string{
|
|
|
|
strings.TrimPrefix(tag, VersionPrefix),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagFor carries any build information from the semantic version through to major and minor tags.
|
|
|
|
labelFor := func(base string) string {
|
|
|
|
return strings.TrimPrefix(base, VersionPrefix) + semver.Build(tag)
|
|
|
|
}
|
|
|
|
return []string{
|
|
|
|
labelFor(semver.Major(tag)),
|
|
|
|
labelFor(semver.MajorMinor(tag)),
|
|
|
|
labelFor(semver.Canonical(tag)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:37:13 +01:00
|
|
|
// Exec executes the plugin step
|
|
|
|
func (p Plugin) Exec() error {
|
2021-09-01 08:15:50 +02:00
|
|
|
if !p.Build.NoPush && p.Build.Repo == "" {
|
2020-11-16 18:37:13 +01:00
|
|
|
return fmt.Errorf("repository name to publish image must be specified")
|
|
|
|
}
|
|
|
|
|
2021-01-29 10:09:06 +01:00
|
|
|
if _, err := os.Stat(p.Build.Dockerfile); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("dockerfile does not exist at path: %s", p.Build.Dockerfile)
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:37:13 +01:00
|
|
|
cmdArgs := []string{
|
|
|
|
fmt.Sprintf("--dockerfile=%s", p.Build.Dockerfile),
|
|
|
|
fmt.Sprintf("--context=dir://%s", p.Build.Context),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the destination repository
|
2021-09-01 08:15:50 +02:00
|
|
|
if !p.Build.NoPush {
|
|
|
|
for _, tag := range p.Build.Tags {
|
2021-10-17 20:35:18 +02:00
|
|
|
for _, label := range p.Build.labelsForTag(tag) {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--destination=%s:%s", p.Build.Repo, label))
|
|
|
|
}
|
2021-09-01 08:15:50 +02:00
|
|
|
}
|
2020-11-16 18:37:13 +01:00
|
|
|
}
|
|
|
|
// Set the build arguments
|
|
|
|
for _, arg := range p.Build.Args {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--build-arg=%s", arg))
|
|
|
|
}
|
|
|
|
// Set the labels
|
|
|
|
for _, label := range p.Build.Labels {
|
2021-01-18 16:42:53 +01:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--label=%s", label))
|
2020-11-16 18:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.Build.Target != "" {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--target=%s", p.Build.Target))
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:19:21 +01:00
|
|
|
if p.Build.SkipTlsVerify {
|
2021-09-01 08:15:50 +02:00
|
|
|
cmdArgs = append(cmdArgs, "--skip-tls-verify=true")
|
2021-02-18 09:19:21 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 16:45:44 +01:00
|
|
|
if p.Build.SnapshotMode != "" {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--snapshotMode=%s", p.Build.SnapshotMode))
|
|
|
|
}
|
|
|
|
|
2021-09-01 08:15:50 +02:00
|
|
|
if p.Build.EnableCache {
|
|
|
|
cmdArgs = append(cmdArgs, "--cache=true")
|
2021-04-07 20:35:53 +02:00
|
|
|
|
2021-08-20 13:08:35 +02:00
|
|
|
if p.Build.CacheRepo != "" {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-repo=%s", p.Build.CacheRepo))
|
|
|
|
}
|
2021-04-07 20:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.Build.CacheTTL != 0 {
|
2022-02-07 14:43:29 +01:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-ttl=%dh", p.Build.CacheTTL))
|
2021-04-07 20:35:53 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 12:05:57 +02:00
|
|
|
if p.Build.DigestFile != "" {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--digest-file=%s", p.Build.DigestFile))
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:23:43 +02:00
|
|
|
if p.Build.NoPush {
|
2021-09-01 08:15:50 +02:00
|
|
|
cmdArgs = append(cmdArgs, "--no-push")
|
2021-04-29 17:23:43 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:08:35 +02:00
|
|
|
if p.Build.Verbosity != "" {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--verbosity=%s", p.Build.Verbosity))
|
|
|
|
}
|
|
|
|
|
2021-12-01 12:40:28 +01:00
|
|
|
if p.Build.Platform != "" {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--customPlatform=%s", p.Build.Platform))
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:37:13 +01:00
|
|
|
cmd := exec.Command("/kaniko/executor", cmdArgs...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
trace(cmd)
|
|
|
|
|
|
|
|
err := cmd.Run()
|
2021-04-29 12:05:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Build.DigestFile != "" && p.Artifact.ArtifactFile != "" {
|
|
|
|
content, err := ioutil.ReadFile(p.Build.DigestFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to read digest file contents at path: %s with error: %s\n", p.Build.DigestFile, err)
|
|
|
|
}
|
|
|
|
err = artifact.WritePluginArtifactFile(p.Artifact.RegistryType, p.Artifact.ArtifactFile, p.Artifact.Registry, p.Artifact.Repo, string(content), p.Artifact.Tags)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to write plugin artifact file at path: %s with error: %s\n", p.Artifact.ArtifactFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-11-16 18:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// trace writes each command to stdout with the command wrapped in an xml
|
|
|
|
// tag so that it can be extracted and displayed in the logs.
|
|
|
|
func trace(cmd *exec.Cmd) {
|
|
|
|
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
|
|
|
|
}
|