mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-04 18:23:50 +08:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bebe65a6c2 | |||
| 0a4b18dbcb | |||
| d381ac6700 | |||
| 39f3398dfe | |||
| 59e09c14de | |||
| 5e7bcabe6a | |||
| 0a35538489 | |||
| 2172c5b7cb | |||
| 9388a47a4c |
+1
-1
@@ -4,7 +4,7 @@ name: default
|
||||
|
||||
steps:
|
||||
- name: build
|
||||
image: golang
|
||||
image: golang:1.17.2
|
||||
commands:
|
||||
- go test ./...
|
||||
- sh scripts/build.sh
|
||||
|
||||
@@ -51,3 +51,35 @@ docker run --rm \
|
||||
-w /drone \
|
||||
plugins/kaniko:linux-amd64
|
||||
```
|
||||
|
||||
### Automatic Tagging
|
||||
|
||||
With auto tagging enabled, semantic versions can be passed to PLUGIN_TAGS directly for expansion:
|
||||
|
||||
```console
|
||||
docker run --rm \
|
||||
-e PLUGIN_TAGS=v1.2.3,latest \
|
||||
-e PLUGIN_AUTO_TAG=true \
|
||||
-v $(pwd):/drone \
|
||||
-w /drone \
|
||||
plugins/kaniko:linux-amd64
|
||||
```
|
||||
would both be equivalent to
|
||||
|
||||
```
|
||||
PLUGIN_TAGS=1,1.2,1.2.3,latest
|
||||
```
|
||||
|
||||
This allows for passing `$DRONE_TAG` directly as a tag for repos that use [semver](https://semver.org) tags.
|
||||
|
||||
To avoid confusion between repo tags and image tags, `PLUGIN_AUTO_TAG` also recognizes a semantic version
|
||||
without the `v` prefix. As such, the following is also equivalent to the above:
|
||||
|
||||
```console
|
||||
docker run --rm \
|
||||
-e PLUGIN_TAGS=1.2.3,latest \
|
||||
-e PLUGIN_AUTO_TAG=true \
|
||||
-v $(pwd):/drone \
|
||||
-w /drone \
|
||||
plugins/kaniko:linux-amd64
|
||||
```
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/pkg/errors"
|
||||
@@ -12,7 +13,7 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
|
||||
kaniko "github.com/drone/drone-kaniko"
|
||||
"github.com/drone/drone-kaniko/cmd/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -20,8 +21,9 @@ const (
|
||||
dockerPath string = "/kaniko/.docker"
|
||||
dockerConfigPath string = "/kaniko/.docker/config.json"
|
||||
|
||||
v1Registry string = "https://index.docker.io/v1/" // Default registry
|
||||
v2Registry string = "https://index.docker.io/v2/" // v2 registry is not supported
|
||||
v1RegistryURL string = "https://index.docker.io/v1/" // Default registry
|
||||
v2RegistryURL string = "https://index.docker.io/v2/" // v2 registry is not supported
|
||||
v2HubRegistryURL string = "https://registry.hub.docker.com/v2/"
|
||||
|
||||
defaultDigestFile string = "/kaniko/digest-file"
|
||||
)
|
||||
@@ -63,6 +65,11 @@ func main() {
|
||||
EnvVar: "PLUGIN_TAGS",
|
||||
FilePath: ".tags",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "auto_tag",
|
||||
Usage: "enable for semver tagging",
|
||||
EnvVar: "PLUGIN_AUTO_TAG",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "args",
|
||||
Usage: "build args",
|
||||
@@ -86,7 +93,7 @@ func main() {
|
||||
cli.StringFlag{
|
||||
Name: "registry",
|
||||
Usage: "docker registry",
|
||||
Value: v1Registry,
|
||||
Value: v1RegistryURL,
|
||||
EnvVar: "PLUGIN_REGISTRY",
|
||||
},
|
||||
cli.StringFlag{
|
||||
@@ -139,6 +146,11 @@ func main() {
|
||||
Usage: "Set this flag with value as oneof <panic|fatal|error|warn|info|debug|trace> to set the logging level for kaniko. Defaults to info.",
|
||||
EnvVar: "PLUGIN_VERBOSITY",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "platform",
|
||||
Usage: "Allows to build with another default platform than the host, similarly to docker build --platform",
|
||||
EnvVar: "PLUGIN_PLATFORM",
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
@@ -162,6 +174,7 @@ func run(c *cli.Context) error {
|
||||
Dockerfile: c.String("dockerfile"),
|
||||
Context: c.String("context"),
|
||||
Tags: c.StringSlice("tags"),
|
||||
AutoTag: c.Bool("auto_tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
Target: c.String("target"),
|
||||
Repo: c.String("repo"),
|
||||
@@ -174,10 +187,11 @@ func run(c *cli.Context) error {
|
||||
DigestFile: defaultDigestFile,
|
||||
NoPush: noPush,
|
||||
Verbosity: c.String("verbosity"),
|
||||
Platform: c.String("platform"),
|
||||
},
|
||||
Artifact: kaniko.Artifact{
|
||||
Tags: c.StringSlice("tags"),
|
||||
Repo: c.String("repo"),
|
||||
Repo: buildRepo(c.String("registry"), c.String("repo")),
|
||||
Registry: c.String("registry"),
|
||||
ArtifactFile: c.String("artifact-file"),
|
||||
RegistryType: artifact.Docker,
|
||||
@@ -198,10 +212,10 @@ func createDockerCfgFile(username, password, registry string) error {
|
||||
return fmt.Errorf("Registry must be specified")
|
||||
}
|
||||
|
||||
if registry == v2Registry {
|
||||
if registry == v2RegistryURL || registry == v2HubRegistryURL {
|
||||
fmt.Println("Docker v2 registry is not supported in kaniko. Refer issue: https://github.com/GoogleContainerTools/kaniko/issues/1209")
|
||||
fmt.Printf("Using v1 registry instead: %s\n", v1Registry)
|
||||
registry = v1Registry
|
||||
fmt.Printf("Using v1 registry instead: %s\n", v1RegistryURL)
|
||||
registry = v1RegistryURL
|
||||
}
|
||||
|
||||
err := os.MkdirAll(dockerPath, 0600)
|
||||
@@ -218,3 +232,17 @@ func createDockerCfgFile(username, password, registry string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildRepo(registry, repo string) string {
|
||||
if registry == "" {
|
||||
// No custom registry, just return the repo name
|
||||
return repo
|
||||
}
|
||||
if strings.HasPrefix(repo, registry + "/") {
|
||||
// Repo already includes the registry prefix
|
||||
// For backward compatibility, we won't add the prefix again.
|
||||
return repo
|
||||
}
|
||||
// Prefix the repo with the registry
|
||||
return registry + "/" + repo
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_buildRepo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
registry string
|
||||
repo string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "dockerhub",
|
||||
repo: "golang",
|
||||
want: "golang",
|
||||
},
|
||||
{
|
||||
name: "internal",
|
||||
registry: "artifactory.example.com",
|
||||
repo: "service",
|
||||
want: "artifactory.example.com/service",
|
||||
},
|
||||
{
|
||||
name: "backward_compatibility",
|
||||
registry: "artifactory.example.com",
|
||||
repo: "artifactory.example.com/service",
|
||||
want: "artifactory.example.com/service",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := buildRepo(tt.registry, tt.repo); got != tt.want {
|
||||
t.Errorf("buildRepo(%q, %q) = %v, want %v", tt.registry, tt.repo, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+13
-1
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
|
||||
"github.com/aws/smithy-go"
|
||||
kaniko "github.com/drone/drone-kaniko"
|
||||
"github.com/drone/drone-kaniko/cmd/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/pkg/errors"
|
||||
@@ -78,6 +78,11 @@ func main() {
|
||||
EnvVar: "PLUGIN_TAGS",
|
||||
FilePath: ".tags",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "auto_tag",
|
||||
Usage: "enable for semver tagging",
|
||||
EnvVar: "PLUGIN_AUTO_TAG",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "args",
|
||||
Usage: "build args",
|
||||
@@ -169,6 +174,11 @@ func main() {
|
||||
Usage: "Set this flag with value as oneof <panic|fatal|error|warn|info|debug|trace> to set the logging level for kaniko. Defaults to info.",
|
||||
EnvVar: "PLUGIN_VERBOSITY",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "platform",
|
||||
Usage: "Allows to build with another default platform than the host, similarly to docker build --platform",
|
||||
EnvVar: "PLUGIN_PLATFORM",
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
@@ -235,6 +245,7 @@ func run(c *cli.Context) error {
|
||||
Dockerfile: c.String("dockerfile"),
|
||||
Context: c.String("context"),
|
||||
Tags: c.StringSlice("tags"),
|
||||
AutoTag: c.Bool("auto_tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
Target: c.String("target"),
|
||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
||||
@@ -246,6 +257,7 @@ func run(c *cli.Context) error {
|
||||
DigestFile: defaultDigestFile,
|
||||
NoPush: noPush,
|
||||
Verbosity: c.String("verbosity"),
|
||||
Platform: c.String("platform"),
|
||||
},
|
||||
Artifact: kaniko.Artifact{
|
||||
Tags: c.StringSlice("tags"),
|
||||
|
||||
+17
-7
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
|
||||
kaniko "github.com/drone/drone-kaniko"
|
||||
"github.com/drone/drone-kaniko/cmd/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -59,6 +59,11 @@ func main() {
|
||||
EnvVar: "PLUGIN_TAGS",
|
||||
FilePath: ".tags",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "auto_tag",
|
||||
Usage: "enable for semver tagging",
|
||||
EnvVar: "PLUGIN_AUTO_TAG",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "args",
|
||||
Usage: "build args",
|
||||
@@ -125,6 +130,11 @@ func main() {
|
||||
Usage: "Set this flag as --verbosity=<panic|fatal|error|warn|info|debug|trace> to set the logging level for kaniko. Defaults to info.",
|
||||
EnvVar: "PLUGIN_VERBOSITY",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "platform",
|
||||
Usage: "Allows to build with another default platform than the host, similarly to docker build --platform",
|
||||
EnvVar: "PLUGIN_PLATFORM",
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
@@ -136,8 +146,10 @@ func run(c *cli.Context) error {
|
||||
noPush := c.Bool("no-push")
|
||||
jsonKey := c.String("json-key")
|
||||
|
||||
// only setup auth when pushing or credentials are defined
|
||||
if !noPush || jsonKey != "" {
|
||||
// JSON key may not be set in the following cases:
|
||||
// 1. Image does not need to be pushed to GCR.
|
||||
// 2. Workload identity is set on GKE in which pod will inherit the credentials via service account.
|
||||
if jsonKey != "" {
|
||||
if err := setupGCRAuth(jsonKey); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -148,6 +160,7 @@ func run(c *cli.Context) error {
|
||||
Dockerfile: c.String("dockerfile"),
|
||||
Context: c.String("context"),
|
||||
Tags: c.StringSlice("tags"),
|
||||
AutoTag: c.Bool("auto_tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
Target: c.String("target"),
|
||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
||||
@@ -159,6 +172,7 @@ func run(c *cli.Context) error {
|
||||
DigestFile: defaultDigestFile,
|
||||
NoPush: noPush,
|
||||
Verbosity: c.String("verbosity"),
|
||||
Platform: c.String("platform"),
|
||||
},
|
||||
Artifact: kaniko.Artifact{
|
||||
Tags: c.StringSlice("tags"),
|
||||
@@ -172,10 +186,6 @@ func run(c *cli.Context) error {
|
||||
}
|
||||
|
||||
func setupGCRAuth(jsonKey string) error {
|
||||
if jsonKey == "" {
|
||||
return fmt.Errorf("GCR JSON key must be specified")
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(gcrKeyPath, []byte(jsonKey), 0644)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to write GCR JSON key")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM gcr.io/kaniko-project/executor:v1.5.2
|
||||
FROM gcr.io/kaniko-project/executor:v1.6.0
|
||||
|
||||
ADD release/linux/amd64/kaniko-docker /kaniko/
|
||||
ENTRYPOINT ["/kaniko/kaniko-docker"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM gcr.io/kaniko-project/executor:arm64-v1.5.2
|
||||
FROM gcr.io/kaniko-project/executor:arm64-v1.6.0
|
||||
|
||||
ENV HOME /root
|
||||
ENV USER root
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM gcr.io/kaniko-project/executor:v1.5.2
|
||||
FROM gcr.io/kaniko-project/executor:v1.6.0
|
||||
|
||||
ADD release/linux/amd64/kaniko-ecr /kaniko/
|
||||
ENTRYPOINT ["/kaniko/kaniko-ecr"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM gcr.io/kaniko-project/executor:arm64-v1.5.2
|
||||
FROM gcr.io/kaniko-project/executor:arm64-v1.6.0
|
||||
|
||||
ENV HOME /root
|
||||
ENV USER root
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM gcr.io/kaniko-project/executor:v1.5.2
|
||||
FROM gcr.io/kaniko-project/executor:v1.6.0
|
||||
|
||||
ADD release/linux/amd64/kaniko-gcr /kaniko/
|
||||
ENTRYPOINT ["/kaniko/kaniko-gcr"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM gcr.io/kaniko-project/executor:arm64-v1.5.2
|
||||
FROM gcr.io/kaniko-project/executor:arm64-v1.6.0
|
||||
|
||||
ENV HOME /root
|
||||
ENV USER root
|
||||
|
||||
@@ -6,10 +6,28 @@ require (
|
||||
github.com/aws/aws-sdk-go-v2/service/ecr v1.4.3
|
||||
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.4.3
|
||||
github.com/aws/smithy-go v1.7.0
|
||||
github.com/google/go-cmp v0.5.6
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/sirupsen/logrus v1.3.0
|
||||
github.com/urfave/cli v1.22.2
|
||||
golang.org/x/mod v0.4.2
|
||||
)
|
||||
|
||||
go 1.13
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.3.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.4.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.2.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.2.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.3.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.6.2 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
|
||||
)
|
||||
|
||||
go 1.17
|
||||
|
||||
@@ -53,13 +53,25 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
|
||||
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
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/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
|
||||
@@ -7,7 +7,8 @@ import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/drone/drone-kaniko/cmd/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
"golang.org/x/mod/semver"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -16,6 +17,7 @@ type (
|
||||
Dockerfile string // Docker build Dockerfile
|
||||
Context string // Docker build context
|
||||
Tags []string // Docker build tags
|
||||
AutoTag bool // Set this to create semver-tagged labels
|
||||
Args []string // Docker build args
|
||||
Target string // Docker build target
|
||||
Repo string // Docker build repository
|
||||
@@ -28,7 +30,9 @@ type (
|
||||
DigestFile string // Digest file location
|
||||
NoPush bool // Set this flag if you only want to build the image, without pushing to a registry
|
||||
Verbosity string // Log level
|
||||
Platform string // Allows to build with another default platform than the host, similarly to docker build --platform
|
||||
}
|
||||
|
||||
// Artifact defines content of artifact file
|
||||
Artifact struct {
|
||||
Tags []string // Docker artifact tags
|
||||
@@ -45,6 +49,47 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// 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)),
|
||||
}
|
||||
}
|
||||
|
||||
// Exec executes the plugin step
|
||||
func (p Plugin) Exec() error {
|
||||
if !p.Build.NoPush && p.Build.Repo == "" {
|
||||
@@ -63,7 +108,9 @@ func (p Plugin) Exec() error {
|
||||
// Set the destination repository
|
||||
if !p.Build.NoPush {
|
||||
for _, tag := range p.Build.Tags {
|
||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--destination=%s:%s", p.Build.Repo, tag))
|
||||
for _, label := range p.Build.labelsForTag(tag) {
|
||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--destination=%s:%s", p.Build.Repo, label))
|
||||
}
|
||||
}
|
||||
}
|
||||
// Set the build arguments
|
||||
@@ -96,7 +143,7 @@ func (p Plugin) Exec() error {
|
||||
}
|
||||
|
||||
if p.Build.CacheTTL != 0 {
|
||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-ttl=%d", p.Build.CacheTTL))
|
||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-ttl=%dh", p.Build.CacheTTL))
|
||||
}
|
||||
|
||||
if p.Build.DigestFile != "" {
|
||||
@@ -111,6 +158,10 @@ func (p Plugin) Exec() error {
|
||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--verbosity=%s", p.Build.Verbosity))
|
||||
}
|
||||
|
||||
if p.Build.Platform != "" {
|
||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--customPlatform=%s", p.Build.Platform))
|
||||
}
|
||||
|
||||
cmd := exec.Command("/kaniko/executor", cmdArgs...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
@@ -1 +1,74 @@
|
||||
package kaniko
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestBuild_labelsForTag(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tag string
|
||||
autoTags []string
|
||||
}{
|
||||
{
|
||||
name: "semver",
|
||||
tag: "v1.2.3",
|
||||
autoTags: []string{"1", "1.2", "1.2.3"},
|
||||
},
|
||||
{
|
||||
name: "no_patch",
|
||||
tag: "v1.2",
|
||||
autoTags: []string{"1", "1.2", "1.2.0"},
|
||||
},
|
||||
{
|
||||
name: "only_major",
|
||||
tag: "v1",
|
||||
autoTags: []string{"1", "1.0", "1.0.0"},
|
||||
},
|
||||
{
|
||||
name: "full_with_build",
|
||||
tag: "v1.2.3+build-info",
|
||||
autoTags: []string{"1+build-info", "1.2+build-info", "1.2.3+build-info"},
|
||||
},
|
||||
{
|
||||
name: "build_with_underscores",
|
||||
tag: "v1.2.3+linux_amd64",
|
||||
autoTags: []string{"1+linux-amd64", "1.2+linux-amd64", "1.2.3+linux-amd64"},
|
||||
},
|
||||
{
|
||||
name: "prerelease",
|
||||
tag: "v1.2.3-rc1",
|
||||
autoTags: []string{"1.2.3-rc1"},
|
||||
},
|
||||
{
|
||||
name: "prerelease_with_build",
|
||||
tag: "v1.2.3-rc1+bld",
|
||||
autoTags: []string{"1.2.3-rc1+bld"},
|
||||
},
|
||||
{
|
||||
name: "invalid_build",
|
||||
tag: "v1+bld", // can only include build detail with all three elements
|
||||
autoTags: []string{"v1+bld"},
|
||||
},
|
||||
{
|
||||
name: "accidental_non_semver",
|
||||
tag: "1.2.3",
|
||||
autoTags: []string{"1", "1.2", "1.2.3"},
|
||||
},
|
||||
{
|
||||
name: "non_semver",
|
||||
tag: "latest",
|
||||
autoTags: []string{"latest"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tags := Build{AutoTag: true}.labelsForTag(tt.tag)
|
||||
if got, want := tags, tt.autoTags; !cmp.Equal(got, want) {
|
||||
t.Errorf("tagsFor(%q) = %q, want %q", tt.tag, got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user