mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-14 05:12:26 +08:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ffce84d742 | |||
| 7c6c33d174 | |||
| a11da49965 | |||
| fae9271dc7 | |||
| 8bd45524d4 | |||
| 427f9abb99 | |||
| dbdf13277d | |||
| c38eb07dea | |||
| de43f3afb6 | |||
| bebe65a6c2 | |||
| 0a4b18dbcb | |||
| d381ac6700 | |||
| 39f3398dfe | |||
| 59e09c14de | |||
| 5e7bcabe6a | |||
| 0a35538489 |
+106
-2
@@ -4,7 +4,7 @@ name: default
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: build
|
- name: build
|
||||||
image: golang:1.17.2
|
image: golang:1.17.7
|
||||||
commands:
|
commands:
|
||||||
- go test ./...
|
- go test ./...
|
||||||
- sh scripts/build.sh
|
- sh scripts/build.sh
|
||||||
@@ -60,6 +60,56 @@ steps:
|
|||||||
exclude:
|
exclude:
|
||||||
- pull_request
|
- pull_request
|
||||||
|
|
||||||
|
- name: docker-kaniko-v1-8
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
repo: plugins/kaniko
|
||||||
|
auto_tag: true
|
||||||
|
auto_tag_suffix: linux-amd64-kaniko1.8.0
|
||||||
|
daemon_off: false
|
||||||
|
dockerfile: docker/docker/Dockerfile.linux.amd64.kaniko1.8.0
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
exclude:
|
||||||
|
- pull_request
|
||||||
|
|
||||||
|
- name: gcr-kaniko-v1-8
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
repo: plugins/kaniko-gcr
|
||||||
|
auto_tag: true
|
||||||
|
auto_tag_suffix: linux-amd64-kaniko1.8.0
|
||||||
|
daemon_off: false
|
||||||
|
dockerfile: docker/gcr/Dockerfile.linux.amd64.kaniko1.8.0
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
exclude:
|
||||||
|
- pull_request
|
||||||
|
|
||||||
|
- name: ecr-kaniko-v1-8
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
repo: plugins/kaniko-ecr
|
||||||
|
auto_tag: true
|
||||||
|
auto_tag_suffix: linux-amd64-kaniko1.8.0
|
||||||
|
daemon_off: false
|
||||||
|
dockerfile: docker/ecr/Dockerfile.linux.amd64.kaniko1.8.0
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
exclude:
|
||||||
|
- pull_request
|
||||||
---
|
---
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
type: docker
|
type: docker
|
||||||
@@ -112,4 +162,58 @@ trigger:
|
|||||||
- "refs/tags/**"
|
- "refs/tags/**"
|
||||||
|
|
||||||
depends_on:
|
depends_on:
|
||||||
- default
|
- default
|
||||||
|
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: notifications-docker-kaniko1-8
|
||||||
|
|
||||||
|
platform:
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: manifest-docker
|
||||||
|
pull: always
|
||||||
|
image: plugins/manifest
|
||||||
|
settings:
|
||||||
|
auto_tag: true
|
||||||
|
ignore_missing: true
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
spec: docker/docker/manifest-kaniko1.8.0.tmpl
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
|
||||||
|
- name: manifest-gcr
|
||||||
|
pull: always
|
||||||
|
image: plugins/manifest
|
||||||
|
settings:
|
||||||
|
auto_tag: true
|
||||||
|
ignore_missing: true
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
spec: docker/gcr/manifest-kaniko1.8.0.tmpl
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
|
||||||
|
- name: manifest-ecr
|
||||||
|
pull: always
|
||||||
|
image: plugins/manifest
|
||||||
|
settings:
|
||||||
|
auto_tag: true
|
||||||
|
ignore_missing: true
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
spec: docker/ecr/manifest-kaniko1.8.0.tmpl
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
ref:
|
||||||
|
- refs/heads/main
|
||||||
|
- "refs/tags/**"
|
||||||
|
|
||||||
|
depends_on:
|
||||||
|
- default
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ docker build \
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
### Manual Tagging
|
||||||
|
|
||||||
```console
|
```console
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
@@ -51,3 +52,79 @@ docker run --rm \
|
|||||||
-w /drone \
|
-w /drone \
|
||||||
plugins/kaniko:linux-amd64
|
plugins/kaniko:linux-amd64
|
||||||
```
|
```
|
||||||
|
|
||||||
|
With expanded tagging enabled, semantic versions can be passed to PLUGIN_TAGS directly for expansion.
|
||||||
|
|
||||||
|
**Note**: this feature only works for build labels. Artifact labels are not supported.
|
||||||
|
|
||||||
|
```console
|
||||||
|
docker run --rm \
|
||||||
|
-e PLUGIN_TAGS=v1.2.3,latest \
|
||||||
|
-e PLUGIN_EXPAND_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_EXPAND_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_EXPAND_TAG=true \
|
||||||
|
-v $(pwd):/drone \
|
||||||
|
-w /drone \
|
||||||
|
plugins/kaniko:linux-amd64
|
||||||
|
```
|
||||||
|
|
||||||
|
### Auto Tagging
|
||||||
|
The [auto tag feature](https://plugins.drone.io/drone-plugins/drone-docker) of docker plugin is also supported.
|
||||||
|
|
||||||
|
When auto tagging is enabled, if any of the case is matched below, a docker build will be pushed with auto generated tags. Otherwise the docker build will be skipped.
|
||||||
|
|
||||||
|
**Note**: this feature only works for build labels. Artifact labels are not supported.
|
||||||
|
|
||||||
|
#### Git Tag Push:
|
||||||
|
|
||||||
|
```console
|
||||||
|
docker run --rm \
|
||||||
|
-e DRONE_COMMIT_REF=refs/tags/v1.2.3 \
|
||||||
|
-e PLUGIN_REPO=foo/bar \
|
||||||
|
-e PLUGIN_USERNAME=foo \
|
||||||
|
-e PLUGIN_PASSWORD=bar \
|
||||||
|
-e PLUGIN_AUTO_TAG=true \
|
||||||
|
-v $(pwd):/drone \
|
||||||
|
-w /drone \
|
||||||
|
plugins/kaniko:linux-amd64
|
||||||
|
```
|
||||||
|
|
||||||
|
Tags to push:
|
||||||
|
- 1.2.3
|
||||||
|
- 1.2
|
||||||
|
- 1
|
||||||
|
|
||||||
|
#### Git Commit Push in default branch:
|
||||||
|
|
||||||
|
```console
|
||||||
|
docker run --rm \
|
||||||
|
-e DRONE_COMMIT_REF=refs/heads/master \
|
||||||
|
-e DRONE_REPO_BRANCH=main \
|
||||||
|
-e PLUGIN_REPO=foo/bar \
|
||||||
|
-e PLUGIN_USERNAME=foo \
|
||||||
|
-e PLUGIN_PASSWORD=bar \
|
||||||
|
-e PLUGIN_AUTO_TAG=true \
|
||||||
|
-v $(pwd):/drone \
|
||||||
|
-w /drone \
|
||||||
|
plugins/kaniko:linux-amd64
|
||||||
|
```
|
||||||
|
|
||||||
|
Tags to push:
|
||||||
|
- latest
|
||||||
|
|||||||
+76
-17
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -12,7 +13,7 @@ import (
|
|||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
|
||||||
kaniko "github.com/drone/drone-kaniko"
|
kaniko "github.com/drone/drone-kaniko"
|
||||||
"github.com/drone/drone-kaniko/cmd/artifact"
|
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -57,6 +58,16 @@ func main() {
|
|||||||
Value: ".",
|
Value: ".",
|
||||||
EnvVar: "PLUGIN_CONTEXT",
|
EnvVar: "PLUGIN_CONTEXT",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "drone-commit-ref",
|
||||||
|
Usage: "git commit ref passed by Drone",
|
||||||
|
EnvVar: "DRONE_COMMIT_REF",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "drone-repo-branch",
|
||||||
|
Usage: "git repository default branch passed by Drone",
|
||||||
|
EnvVar: "DRONE_REPO_BRANCH",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "tags",
|
Name: "tags",
|
||||||
Usage: "build tags",
|
Usage: "build tags",
|
||||||
@@ -64,6 +75,21 @@ func main() {
|
|||||||
EnvVar: "PLUGIN_TAGS",
|
EnvVar: "PLUGIN_TAGS",
|
||||||
FilePath: ".tags",
|
FilePath: ".tags",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "expand-tag",
|
||||||
|
Usage: "enable for semver tagging",
|
||||||
|
EnvVar: "PLUGIN_EXPAND_TAG",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "auto-tag",
|
||||||
|
Usage: "enable auto generation of build tags",
|
||||||
|
EnvVar: "PLUGIN_AUTO_TAG",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "auto-tag-suffix",
|
||||||
|
Usage: "the suffix of auto build tags",
|
||||||
|
EnvVar: "PLUGIN_AUTO_TAG_SUFFIX",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "args",
|
Name: "args",
|
||||||
Usage: "build args",
|
Usage: "build args",
|
||||||
@@ -90,6 +116,11 @@ func main() {
|
|||||||
Value: v1RegistryURL,
|
Value: v1RegistryURL,
|
||||||
EnvVar: "PLUGIN_REGISTRY",
|
EnvVar: "PLUGIN_REGISTRY",
|
||||||
},
|
},
|
||||||
|
cli.StringSliceFlag{
|
||||||
|
Name: "registry-mirrors",
|
||||||
|
Usage: "docker registry mirrors",
|
||||||
|
EnvVar: "PLUGIN_REGISTRY_MIRRORS",
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "username",
|
Name: "username",
|
||||||
Usage: "docker username",
|
Usage: "docker username",
|
||||||
@@ -140,6 +171,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.",
|
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",
|
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 {
|
if err := app.Run(os.Args); err != nil {
|
||||||
@@ -160,25 +196,32 @@ func run(c *cli.Context) error {
|
|||||||
|
|
||||||
plugin := kaniko.Plugin{
|
plugin := kaniko.Plugin{
|
||||||
Build: kaniko.Build{
|
Build: kaniko.Build{
|
||||||
Dockerfile: c.String("dockerfile"),
|
DroneCommitRef: c.String("drone-commit-ref"),
|
||||||
Context: c.String("context"),
|
DroneRepoBranch: c.String("drone-repo-branch"),
|
||||||
Tags: c.StringSlice("tags"),
|
Dockerfile: c.String("dockerfile"),
|
||||||
Args: c.StringSlice("args"),
|
Context: c.String("context"),
|
||||||
Target: c.String("target"),
|
Tags: c.StringSlice("tags"),
|
||||||
Repo: c.String("repo"),
|
AutoTag: c.Bool("auto-tag"),
|
||||||
Labels: c.StringSlice("custom-labels"),
|
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||||
SkipTlsVerify: c.Bool("skip-tls-verify"),
|
ExpandTag: c.Bool("expand-tag"),
|
||||||
SnapshotMode: c.String("snapshot-mode"),
|
Args: c.StringSlice("args"),
|
||||||
EnableCache: c.Bool("enable-cache"),
|
Target: c.String("target"),
|
||||||
CacheRepo: c.String("cache-repo"),
|
Repo: buildRepo(c.String("registry"), c.String("repo")),
|
||||||
CacheTTL: c.Int("cache-ttl"),
|
Mirrors: c.StringSlice("registry-mirrors"),
|
||||||
DigestFile: defaultDigestFile,
|
Labels: c.StringSlice("custom-labels"),
|
||||||
NoPush: noPush,
|
SkipTlsVerify: c.Bool("skip-tls-verify"),
|
||||||
Verbosity: c.String("verbosity"),
|
SnapshotMode: c.String("snapshot-mode"),
|
||||||
|
EnableCache: c.Bool("enable-cache"),
|
||||||
|
CacheRepo: buildRepo(c.String("registry"), c.String("cache-repo")),
|
||||||
|
CacheTTL: c.Int("cache-ttl"),
|
||||||
|
DigestFile: defaultDigestFile,
|
||||||
|
NoPush: noPush,
|
||||||
|
Verbosity: c.String("verbosity"),
|
||||||
|
Platform: c.String("platform"),
|
||||||
},
|
},
|
||||||
Artifact: kaniko.Artifact{
|
Artifact: kaniko.Artifact{
|
||||||
Tags: c.StringSlice("tags"),
|
Tags: c.StringSlice("tags"),
|
||||||
Repo: c.String("repo"),
|
Repo: buildRepo(c.String("registry"), c.String("repo")),
|
||||||
Registry: c.String("registry"),
|
Registry: c.String("registry"),
|
||||||
ArtifactFile: c.String("artifact-file"),
|
ArtifactFile: c.String("artifact-file"),
|
||||||
RegistryType: artifact.Docker,
|
RegistryType: artifact.Docker,
|
||||||
@@ -219,3 +262,19 @@ func createDockerCfgFile(username, password, registry string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildRepo(registry, repo string) string {
|
||||||
|
if registry == "" {
|
||||||
|
// No custom registry, just return the repo name
|
||||||
|
return repo
|
||||||
|
}
|
||||||
|
// Trim off trailing slash to prevent double slash when combining with repo
|
||||||
|
registry = strings.TrimSuffix(registry, "/")
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+57
-15
@@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
|
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
|
||||||
"github.com/aws/smithy-go"
|
"github.com/aws/smithy-go"
|
||||||
kaniko "github.com/drone/drone-kaniko"
|
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/drone/drone-kaniko/pkg/docker"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -71,6 +71,16 @@ func main() {
|
|||||||
Value: ".",
|
Value: ".",
|
||||||
EnvVar: "PLUGIN_CONTEXT",
|
EnvVar: "PLUGIN_CONTEXT",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "drone-commit-ref",
|
||||||
|
Usage: "git commit ref passed by Drone",
|
||||||
|
EnvVar: "DRONE_COMMIT_REF",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "drone-repo-branch",
|
||||||
|
Usage: "git repository default branch passed by Drone",
|
||||||
|
EnvVar: "DRONE_REPO_BRANCH",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "tags",
|
Name: "tags",
|
||||||
Usage: "build tags",
|
Usage: "build tags",
|
||||||
@@ -78,6 +88,21 @@ func main() {
|
|||||||
EnvVar: "PLUGIN_TAGS",
|
EnvVar: "PLUGIN_TAGS",
|
||||||
FilePath: ".tags",
|
FilePath: ".tags",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "expand-tag",
|
||||||
|
Usage: "enable for semver tagging",
|
||||||
|
EnvVar: "PLUGIN_EXPAND_TAG",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "auto-tag",
|
||||||
|
Usage: "enable auto generation of build tags",
|
||||||
|
EnvVar: "PLUGIN_AUTO_TAG",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "auto-tag-suffix",
|
||||||
|
Usage: "the suffix of auto build tags",
|
||||||
|
EnvVar: "PLUGIN_AUTO_TAG_SUFFIX",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "args",
|
Name: "args",
|
||||||
Usage: "build args",
|
Usage: "build args",
|
||||||
@@ -114,6 +139,11 @@ func main() {
|
|||||||
Usage: "ECR registry",
|
Usage: "ECR registry",
|
||||||
EnvVar: "PLUGIN_REGISTRY",
|
EnvVar: "PLUGIN_REGISTRY",
|
||||||
},
|
},
|
||||||
|
cli.StringSliceFlag{
|
||||||
|
Name: "registry-mirrors",
|
||||||
|
Usage: "docker registry mirrors",
|
||||||
|
EnvVar: "PLUGIN_REGISTRY_MIRRORS",
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "access-key",
|
Name: "access-key",
|
||||||
Usage: "ECR access key",
|
Usage: "ECR access key",
|
||||||
@@ -169,6 +199,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.",
|
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",
|
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 {
|
if err := app.Run(os.Args); err != nil {
|
||||||
@@ -232,20 +267,27 @@ func run(c *cli.Context) error {
|
|||||||
|
|
||||||
plugin := kaniko.Plugin{
|
plugin := kaniko.Plugin{
|
||||||
Build: kaniko.Build{
|
Build: kaniko.Build{
|
||||||
Dockerfile: c.String("dockerfile"),
|
DroneCommitRef: c.String("drone-commit-ref"),
|
||||||
Context: c.String("context"),
|
DroneRepoBranch: c.String("drone-repo-branch"),
|
||||||
Tags: c.StringSlice("tags"),
|
Dockerfile: c.String("dockerfile"),
|
||||||
Args: c.StringSlice("args"),
|
Context: c.String("context"),
|
||||||
Target: c.String("target"),
|
Tags: c.StringSlice("tags"),
|
||||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
AutoTag: c.Bool("auto-tag"),
|
||||||
Labels: c.StringSlice("custom-labels"),
|
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||||
SnapshotMode: c.String("snapshot-mode"),
|
ExpandTag: c.Bool("expand-tag"),
|
||||||
EnableCache: c.Bool("enable-cache"),
|
Args: c.StringSlice("args"),
|
||||||
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
|
Target: c.String("target"),
|
||||||
CacheTTL: c.Int("cache-ttl"),
|
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
||||||
DigestFile: defaultDigestFile,
|
Mirrors: c.StringSlice("registry-mirrors"),
|
||||||
NoPush: noPush,
|
Labels: c.StringSlice("custom-labels"),
|
||||||
Verbosity: c.String("verbosity"),
|
SnapshotMode: c.String("snapshot-mode"),
|
||||||
|
EnableCache: c.Bool("enable-cache"),
|
||||||
|
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
|
||||||
|
CacheTTL: c.Int("cache-ttl"),
|
||||||
|
DigestFile: defaultDigestFile,
|
||||||
|
NoPush: noPush,
|
||||||
|
Verbosity: c.String("verbosity"),
|
||||||
|
Platform: c.String("platform"),
|
||||||
},
|
},
|
||||||
Artifact: kaniko.Artifact{
|
Artifact: kaniko.Artifact{
|
||||||
Tags: c.StringSlice("tags"),
|
Tags: c.StringSlice("tags"),
|
||||||
|
|||||||
+61
-21
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
|
||||||
kaniko "github.com/drone/drone-kaniko"
|
kaniko "github.com/drone/drone-kaniko"
|
||||||
"github.com/drone/drone-kaniko/cmd/artifact"
|
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -52,6 +52,16 @@ func main() {
|
|||||||
Value: ".",
|
Value: ".",
|
||||||
EnvVar: "PLUGIN_CONTEXT",
|
EnvVar: "PLUGIN_CONTEXT",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "drone-commit-ref",
|
||||||
|
Usage: "git commit ref passed by Drone",
|
||||||
|
EnvVar: "DRONE_COMMIT_REF",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "drone-repo-branch",
|
||||||
|
Usage: "git repository default branch passed by Drone",
|
||||||
|
EnvVar: "DRONE_REPO_BRANCH",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "tags",
|
Name: "tags",
|
||||||
Usage: "build tags",
|
Usage: "build tags",
|
||||||
@@ -59,6 +69,21 @@ func main() {
|
|||||||
EnvVar: "PLUGIN_TAGS",
|
EnvVar: "PLUGIN_TAGS",
|
||||||
FilePath: ".tags",
|
FilePath: ".tags",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "expand-tag",
|
||||||
|
Usage: "enable for semver tagging",
|
||||||
|
EnvVar: "PLUGIN_EXPAND_TAG",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "auto-tag",
|
||||||
|
Usage: "enable auto generation of build tags",
|
||||||
|
EnvVar: "PLUGIN_AUTO_TAG",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "auto-tag-suffix",
|
||||||
|
Usage: "the suffix of auto build tags",
|
||||||
|
EnvVar: "PLUGIN_AUTO_TAG_SUFFIX",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "args",
|
Name: "args",
|
||||||
Usage: "build args",
|
Usage: "build args",
|
||||||
@@ -85,6 +110,11 @@ func main() {
|
|||||||
Value: "gcr.io",
|
Value: "gcr.io",
|
||||||
EnvVar: "PLUGIN_REGISTRY",
|
EnvVar: "PLUGIN_REGISTRY",
|
||||||
},
|
},
|
||||||
|
cli.StringSliceFlag{
|
||||||
|
Name: "registry-mirrors",
|
||||||
|
Usage: "docker registry mirrors",
|
||||||
|
EnvVar: "PLUGIN_REGISTRY_MIRRORS",
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "json-key",
|
Name: "json-key",
|
||||||
Usage: "docker username",
|
Usage: "docker username",
|
||||||
@@ -125,6 +155,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.",
|
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",
|
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 {
|
if err := app.Run(os.Args); err != nil {
|
||||||
@@ -136,8 +171,10 @@ func run(c *cli.Context) error {
|
|||||||
noPush := c.Bool("no-push")
|
noPush := c.Bool("no-push")
|
||||||
jsonKey := c.String("json-key")
|
jsonKey := c.String("json-key")
|
||||||
|
|
||||||
// only setup auth when pushing or credentials are defined
|
// JSON key may not be set in the following cases:
|
||||||
if !noPush || jsonKey != "" {
|
// 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 {
|
if err := setupGCRAuth(jsonKey); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -145,20 +182,27 @@ func run(c *cli.Context) error {
|
|||||||
|
|
||||||
plugin := kaniko.Plugin{
|
plugin := kaniko.Plugin{
|
||||||
Build: kaniko.Build{
|
Build: kaniko.Build{
|
||||||
Dockerfile: c.String("dockerfile"),
|
DroneCommitRef: c.String("drone-commit-ref"),
|
||||||
Context: c.String("context"),
|
DroneRepoBranch: c.String("drone-repo-branch"),
|
||||||
Tags: c.StringSlice("tags"),
|
Dockerfile: c.String("dockerfile"),
|
||||||
Args: c.StringSlice("args"),
|
Context: c.String("context"),
|
||||||
Target: c.String("target"),
|
Tags: c.StringSlice("tags"),
|
||||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
AutoTag: c.Bool("auto-tag"),
|
||||||
Labels: c.StringSlice("custom-labels"),
|
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||||
SnapshotMode: c.String("snapshot-mode"),
|
ExpandTag: c.Bool("expand-tag"),
|
||||||
EnableCache: c.Bool("enable-cache"),
|
Args: c.StringSlice("args"),
|
||||||
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
|
Target: c.String("target"),
|
||||||
CacheTTL: c.Int("cache-ttl"),
|
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
||||||
DigestFile: defaultDigestFile,
|
Mirrors: c.StringSlice("registry-mirrors"),
|
||||||
NoPush: noPush,
|
Labels: c.StringSlice("custom-labels"),
|
||||||
Verbosity: c.String("verbosity"),
|
SnapshotMode: c.String("snapshot-mode"),
|
||||||
|
EnableCache: c.Bool("enable-cache"),
|
||||||
|
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
|
||||||
|
CacheTTL: c.Int("cache-ttl"),
|
||||||
|
DigestFile: defaultDigestFile,
|
||||||
|
NoPush: noPush,
|
||||||
|
Verbosity: c.String("verbosity"),
|
||||||
|
Platform: c.String("platform"),
|
||||||
},
|
},
|
||||||
Artifact: kaniko.Artifact{
|
Artifact: kaniko.Artifact{
|
||||||
Tags: c.StringSlice("tags"),
|
Tags: c.StringSlice("tags"),
|
||||||
@@ -172,10 +216,6 @@ func run(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setupGCRAuth(jsonKey string) error {
|
func setupGCRAuth(jsonKey string) error {
|
||||||
if jsonKey == "" {
|
|
||||||
return fmt.Errorf("GCR JSON key must be specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(gcrKeyPath, []byte(jsonKey), 0644)
|
err := ioutil.WriteFile(gcrKeyPath, []byte(jsonKey), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to write GCR JSON key")
|
return errors.Wrap(err, "failed to write GCR JSON key")
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
FROM gcr.io/kaniko-project/executor:v1.8.0
|
||||||
|
|
||||||
|
ADD release/linux/amd64/kaniko-docker /kaniko/
|
||||||
|
ENTRYPOINT ["/kaniko/kaniko-docker"]
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
image: plugins/kaniko:{{#if build.tag}}{{trimPrefix "v" build.tag}}-kaniko1.8.0{{else}}latest-kaniko1.8.0{{/if}}
|
||||||
|
{{#if build.tags}}
|
||||||
|
tags:
|
||||||
|
{{#each build.tags}}
|
||||||
|
- {{this}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
manifests:
|
||||||
|
-
|
||||||
|
image: plugins/kaniko:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64-kaniko1.8.0
|
||||||
|
platform:
|
||||||
|
architecture: amd64
|
||||||
|
os: linux
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
FROM gcr.io/kaniko-project/executor:v1.8.0
|
||||||
|
|
||||||
|
ADD release/linux/amd64/kaniko-ecr /kaniko/
|
||||||
|
ENTRYPOINT ["/kaniko/kaniko-ecr"]
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
image: plugins/kaniko-ecr:{{#if build.tag}}{{trimPrefix "v" build.tag}}-kaniko1.8.0{{else}}latest-kaniko1.8.0{{/if}}
|
||||||
|
{{#if build.tags}}
|
||||||
|
tags:
|
||||||
|
{{#each build.tags}}
|
||||||
|
- {{this}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
manifests:
|
||||||
|
-
|
||||||
|
image: plugins/kaniko-ecr:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64-kaniko1.8.0
|
||||||
|
platform:
|
||||||
|
architecture: amd64
|
||||||
|
os: linux
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
FROM gcr.io/kaniko-project/executor:v1.8.0
|
||||||
|
|
||||||
|
ADD release/linux/amd64/kaniko-gcr /kaniko/
|
||||||
|
ENTRYPOINT ["/kaniko/kaniko-gcr"]
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
image: plugins/kaniko-gcr:{{#if build.tag}}{{trimPrefix "v" build.tag}}-kaniko1.8.0{{else}}latest-kaniko1.8.0{{/if}}
|
||||||
|
{{#if build.tags}}
|
||||||
|
tags:
|
||||||
|
{{#each build.tags}}
|
||||||
|
- {{this}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
manifests:
|
||||||
|
-
|
||||||
|
image: plugins/kaniko-gcr:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64-kaniko1.8.0
|
||||||
|
platform:
|
||||||
|
architecture: amd64
|
||||||
|
os: linux
|
||||||
@@ -6,10 +6,13 @@ require (
|
|||||||
github.com/aws/aws-sdk-go-v2/service/ecr v1.4.3
|
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/aws-sdk-go-v2/service/ecrpublic v1.4.3
|
||||||
github.com/aws/smithy-go v1.7.0
|
github.com/aws/smithy-go v1.7.0
|
||||||
|
github.com/coreos/go-semver v0.3.0
|
||||||
|
github.com/google/go-cmp v0.5.6
|
||||||
github.com/joho/godotenv v1.3.0
|
github.com/joho/godotenv v1.3.0
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/sirupsen/logrus v1.3.0
|
github.com/sirupsen/logrus v1.3.0
|
||||||
github.com/urfave/cli v1.22.2
|
github.com/urfave/cli v1.22.2
|
||||||
|
golang.org/x/mod v0.4.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -24,8 +27,8 @@ require (
|
|||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 // indirect
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
|
||||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 // indirect
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.6.2 h1:l504GWCoQi1Pk68vSUFGLmDIEMzRf
|
|||||||
github.com/aws/aws-sdk-go-v2/service/sts v1.6.2/go.mod h1:RBhoMJB8yFToaCnbe0jNq5Dcdy0jp6LhHqg55rjClkM=
|
github.com/aws/aws-sdk-go-v2/service/sts v1.6.2/go.mod h1:RBhoMJB8yFToaCnbe0jNq5Dcdy0jp6LhHqg55rjClkM=
|
||||||
github.com/aws/smithy-go v1.7.0 h1:+cLHMRrDZvQ4wk+KuQ9yH6eEg6KZEJ9RI2IkDqnygCg=
|
github.com/aws/smithy-go v1.7.0 h1:+cLHMRrDZvQ4wk+KuQ9yH6eEg6KZEJ9RI2IkDqnygCg=
|
||||||
github.com/aws/smithy-go v1.7.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
|
github.com/aws/smithy-go v1.7.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
|
||||||
|
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
|
||||||
|
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
@@ -53,10 +55,23 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
|
|||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
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 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
|
||||||
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
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/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-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 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
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=
|
||||||
|
|||||||
@@ -7,28 +7,38 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/drone/drone-kaniko/cmd/artifact"
|
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||||
|
"github.com/drone/drone-kaniko/pkg/tagger"
|
||||||
|
"golang.org/x/mod/semver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Build defines Docker build parameters.
|
// Build defines Docker build parameters.
|
||||||
Build struct {
|
Build struct {
|
||||||
Dockerfile string // Docker build Dockerfile
|
DroneCommitRef string // Drone git commit reference
|
||||||
Context string // Docker build context
|
DroneRepoBranch string // Drone repo branch
|
||||||
Tags []string // Docker build tags
|
Dockerfile string // Docker build Dockerfile
|
||||||
Args []string // Docker build args
|
Context string // Docker build context
|
||||||
Target string // Docker build target
|
Tags []string // Docker build tags
|
||||||
Repo string // Docker build repository
|
AutoTag bool // Set this to auto detect tags from git commits and semver-tagged labels
|
||||||
Labels []string // Label map
|
AutoTagSuffix string // Suffix to append to the auto detect tags
|
||||||
SkipTlsVerify bool // Docker skip tls certificate verify for registry
|
ExpandTag bool // Set this to expand the `Tags` into semver-tagged labels
|
||||||
SnapshotMode string // Kaniko snapshot mode
|
Args []string // Docker build args
|
||||||
EnableCache bool // Whether to enable kaniko cache
|
Target string // Docker build target
|
||||||
CacheRepo string // Remote repository that will be used to store cached layers
|
Repo string // Docker build repository
|
||||||
CacheTTL int // Cache timeout in hours
|
Mirrors []string // Docker repository mirrors
|
||||||
DigestFile string // Digest file location
|
Labels []string // Label map
|
||||||
NoPush bool // Set this flag if you only want to build the image, without pushing to a registry
|
SkipTlsVerify bool // Docker skip tls certificate verify for registry
|
||||||
Verbosity string // Log level
|
SnapshotMode string // Kaniko snapshot mode
|
||||||
|
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
|
||||||
|
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 defines content of artifact file
|
||||||
Artifact struct {
|
Artifact struct {
|
||||||
Tags []string // Docker artifact tags
|
Tags []string // Docker artifact tags
|
||||||
@@ -45,6 +55,71 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// labelsForTag returns the labels to use for the given tag, subject to the value of ExpandTag.
|
||||||
|
//
|
||||||
|
// 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 expand-tag is not set, or if the tag is not a semantic version
|
||||||
|
if !b.ExpandTag || !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)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the auto detected tags. See the AutoTag section of
|
||||||
|
// https://plugins.drone.io/drone-plugins/drone-docker/ for more info.
|
||||||
|
func (b Build) AutoTags() (tags []string, err error) {
|
||||||
|
if len(b.Tags) > 1 || len(b.Tags) == 1 && b.Tags[0] != "latest" {
|
||||||
|
err = fmt.Errorf("The auto-tag flag does not work with user provided tags %s", b.Tags)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// We have tried the best to prevent enabling auto-tag and passing in
|
||||||
|
// user specified at the same time. Starts to auto detect tags.
|
||||||
|
// Note: passing in a "latest" tag with auto-tag enabled won't trigger the
|
||||||
|
// early returns above, because we cannot tell if the tag is provided by
|
||||||
|
// the default value or by the users.
|
||||||
|
commitRef := b.DroneCommitRef
|
||||||
|
if !tagger.UseAutoTag(commitRef, b.DroneRepoBranch) {
|
||||||
|
err = fmt.Errorf("Could not auto detect the tag. Skipping automated docker build for commit %s", commitRef)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tags, err = tagger.AutoTagsSuffix(commitRef, b.AutoTagSuffix)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("Invalid semantic version when auto detecting the tag. Skipping automated docker build for %s.", commitRef)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Exec executes the plugin step
|
// Exec executes the plugin step
|
||||||
func (p Plugin) Exec() error {
|
func (p Plugin) Exec() error {
|
||||||
if !p.Build.NoPush && p.Build.Repo == "" {
|
if !p.Build.NoPush && p.Build.Repo == "" {
|
||||||
@@ -55,6 +130,18 @@ func (p Plugin) Exec() error {
|
|||||||
return fmt.Errorf("dockerfile does not exist at path: %s", p.Build.Dockerfile)
|
return fmt.Errorf("dockerfile does not exist at path: %s", p.Build.Dockerfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tags = p.Build.Tags
|
||||||
|
if p.Build.AutoTag && p.Build.ExpandTag {
|
||||||
|
return fmt.Errorf("The auto-tag flag conflicts with the expand-tag flag")
|
||||||
|
}
|
||||||
|
if p.Build.AutoTag {
|
||||||
|
var err error
|
||||||
|
tags, err = p.Build.AutoTags()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cmdArgs := []string{
|
cmdArgs := []string{
|
||||||
fmt.Sprintf("--dockerfile=%s", p.Build.Dockerfile),
|
fmt.Sprintf("--dockerfile=%s", p.Build.Dockerfile),
|
||||||
fmt.Sprintf("--context=dir://%s", p.Build.Context),
|
fmt.Sprintf("--context=dir://%s", p.Build.Context),
|
||||||
@@ -62,8 +149,10 @@ func (p Plugin) Exec() error {
|
|||||||
|
|
||||||
// Set the destination repository
|
// Set the destination repository
|
||||||
if !p.Build.NoPush {
|
if !p.Build.NoPush {
|
||||||
for _, tag := range p.Build.Tags {
|
for _, tag := range 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
|
// Set the build arguments
|
||||||
@@ -74,7 +163,10 @@ func (p Plugin) Exec() error {
|
|||||||
for _, label := range p.Build.Labels {
|
for _, label := range p.Build.Labels {
|
||||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--label=%s", label))
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--label=%s", label))
|
||||||
}
|
}
|
||||||
|
// Set repository mirrors
|
||||||
|
for _, mirror := range p.Build.Mirrors {
|
||||||
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--registry-mirror=%s", mirror))
|
||||||
|
}
|
||||||
if p.Build.Target != "" {
|
if p.Build.Target != "" {
|
||||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--target=%s", p.Build.Target))
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--target=%s", p.Build.Target))
|
||||||
}
|
}
|
||||||
@@ -96,7 +188,7 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if p.Build.CacheTTL != 0 {
|
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 != "" {
|
if p.Build.DigestFile != "" {
|
||||||
@@ -111,6 +203,10 @@ func (p Plugin) Exec() error {
|
|||||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--verbosity=%s", p.Build.Verbosity))
|
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 := exec.Command("/kaniko/executor", cmdArgs...)
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
|
|||||||
+140
@@ -1 +1,141 @@
|
|||||||
package kaniko
|
package kaniko
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuild_labelsForTag(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
tag string
|
||||||
|
expandTags []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "semver",
|
||||||
|
tag: "v1.2.3",
|
||||||
|
expandTags: []string{"1", "1.2", "1.2.3"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no_patch",
|
||||||
|
tag: "v1.2",
|
||||||
|
expandTags: []string{"1", "1.2", "1.2.0"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "only_major",
|
||||||
|
tag: "v1",
|
||||||
|
expandTags: []string{"1", "1.0", "1.0.0"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "full_with_build",
|
||||||
|
tag: "v1.2.3+build-info",
|
||||||
|
expandTags: []string{"1+build-info", "1.2+build-info", "1.2.3+build-info"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "build_with_underscores",
|
||||||
|
tag: "v1.2.3+linux_amd64",
|
||||||
|
expandTags: []string{"1+linux-amd64", "1.2+linux-amd64", "1.2.3+linux-amd64"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "prerelease",
|
||||||
|
tag: "v1.2.3-rc1",
|
||||||
|
expandTags: []string{"1.2.3-rc1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "prerelease_with_build",
|
||||||
|
tag: "v1.2.3-rc1+bld",
|
||||||
|
expandTags: []string{"1.2.3-rc1+bld"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid_build",
|
||||||
|
tag: "v1+bld", // can only include build detail with all three elements
|
||||||
|
expandTags: []string{"v1+bld"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "accidental_non_semver",
|
||||||
|
tag: "1.2.3",
|
||||||
|
expandTags: []string{"1", "1.2", "1.2.3"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "non_semver",
|
||||||
|
tag: "latest",
|
||||||
|
expandTags: []string{"latest"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
tags := Build{ExpandTag: true}.labelsForTag(tt.tag)
|
||||||
|
if got, want := tags, tt.expandTags; !cmp.Equal(got, want) {
|
||||||
|
t.Errorf("tagsFor(%q) = %q, want %q", tt.tag, got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuild_AutoTags(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
repoBranch string
|
||||||
|
commitRef string
|
||||||
|
autoTagSuffix string
|
||||||
|
expectedTags []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "commit push",
|
||||||
|
repoBranch: "master",
|
||||||
|
commitRef: "refs/heads/master",
|
||||||
|
autoTagSuffix: "",
|
||||||
|
expectedTags: []string{"latest"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tag push",
|
||||||
|
repoBranch: "master",
|
||||||
|
commitRef: "refs/tags/v1.0.0",
|
||||||
|
autoTagSuffix: "",
|
||||||
|
expectedTags: []string{
|
||||||
|
"1",
|
||||||
|
"1.0",
|
||||||
|
"1.0.0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tag push",
|
||||||
|
repoBranch: "master",
|
||||||
|
commitRef: "refs/tags/v1.0.0",
|
||||||
|
autoTagSuffix: "linux-amd64",
|
||||||
|
expectedTags: []string{
|
||||||
|
"1-linux-amd64",
|
||||||
|
"1.0-linux-amd64",
|
||||||
|
"1.0.0-linux-amd64",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
b := Build{DroneCommitRef: tt.commitRef, DroneRepoBranch: tt.repoBranch, AutoTag: true}
|
||||||
|
if tt.autoTagSuffix != "" {
|
||||||
|
b.AutoTagSuffix = tt.autoTagSuffix
|
||||||
|
}
|
||||||
|
tags, err := b.AutoTags()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected err %q", err)
|
||||||
|
}
|
||||||
|
if got, want := tags, tt.expectedTags; !cmp.Equal(got, want) {
|
||||||
|
t.Errorf("auto detected tags = %q, wanted = %q", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
t.Run("auto-tag cannot be enabled with user provided tags", func(t *testing.T) {
|
||||||
|
b := Build{
|
||||||
|
DroneCommitRef: "refs/tags/v1.0.0",
|
||||||
|
DroneRepoBranch: "master",
|
||||||
|
AutoTag: true,
|
||||||
|
Tags: []string{"v1"},
|
||||||
|
}
|
||||||
|
_, err := b.AutoTags()
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expect error for invalid flags")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
// A fork of https://github.com/drone-plugins/drone-docker/blob/master/tags.go
|
||||||
|
|
||||||
|
package tagger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coreos/go-semver/semver"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AutoTagsSuffix returns a set of default suggested tags
|
||||||
|
// based on the commit ref with an attached suffix.
|
||||||
|
func AutoTagsSuffix(ref, suffix string) ([]string, error) {
|
||||||
|
tags, err := AutoTags(ref)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(suffix) == 0 {
|
||||||
|
return tags, nil
|
||||||
|
}
|
||||||
|
for i, tag := range tags {
|
||||||
|
if tag == "latest" {
|
||||||
|
tags[i] = suffix
|
||||||
|
} else {
|
||||||
|
tags[i] = fmt.Sprintf("%s-%s", tag, suffix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tags, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitOff(input string, delim string) string {
|
||||||
|
parts := strings.SplitN(input, delim, 2)
|
||||||
|
|
||||||
|
if len(parts) == 2 {
|
||||||
|
return parts[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
// AutoTags returns a set of default suggested tags based on
|
||||||
|
// the commit ref.
|
||||||
|
func AutoTags(ref string) ([]string, error) {
|
||||||
|
if !strings.HasPrefix(ref, "refs/tags/") {
|
||||||
|
return []string{"latest"}, nil
|
||||||
|
}
|
||||||
|
v := stripTagPrefix(ref)
|
||||||
|
version, err := semver.NewVersion(v)
|
||||||
|
if err != nil {
|
||||||
|
return []string{"latest"}, err
|
||||||
|
}
|
||||||
|
if version.PreRelease != "" || version.Metadata != "" {
|
||||||
|
return []string{
|
||||||
|
version.String(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
v = stripTagPrefix(ref)
|
||||||
|
v = splitOff(splitOff(v, "+"), "-")
|
||||||
|
dotParts := strings.SplitN(v, ".", 3)
|
||||||
|
|
||||||
|
if version.Major == 0 {
|
||||||
|
return []string{
|
||||||
|
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
|
||||||
|
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
return []string{
|
||||||
|
fmt.Sprintf("%0*d", len(dotParts[0]), version.Major),
|
||||||
|
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
|
||||||
|
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseAutoTag for keep only default branch for latest tag.
|
||||||
|
func UseAutoTag(ref, defaultBranch string) bool {
|
||||||
|
if strings.HasPrefix(ref, "refs/tags/") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if stripHeadPrefix(ref) == defaultBranch {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func stripHeadPrefix(ref string) string {
|
||||||
|
return strings.TrimPrefix(ref, "refs/heads/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func stripTagPrefix(ref string) string {
|
||||||
|
ref = strings.TrimPrefix(ref, "refs/tags/")
|
||||||
|
ref = strings.TrimPrefix(ref, "v")
|
||||||
|
return ref
|
||||||
|
}
|
||||||
@@ -0,0 +1,199 @@
|
|||||||
|
// A fork of https://github.com/drone-plugins/drone-docker/blob/master/tags_test.go
|
||||||
|
|
||||||
|
package tagger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_stripTagPrefix(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
Before string
|
||||||
|
After string
|
||||||
|
}{
|
||||||
|
{"refs/tags/1.0.0", "1.0.0"},
|
||||||
|
{"refs/tags/v1.0.0", "1.0.0"},
|
||||||
|
{"v1.0.0", "1.0.0"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
got, want := stripTagPrefix(test.Before), test.After
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("Got tag %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoTags(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
Before string
|
||||||
|
After []string
|
||||||
|
}{
|
||||||
|
{"", []string{"latest"}},
|
||||||
|
{"refs/heads/master", []string{"latest"}},
|
||||||
|
{"refs/tags/0.9.0", []string{"0.9", "0.9.0"}},
|
||||||
|
{"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}},
|
||||||
|
{"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}},
|
||||||
|
{"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
tags, err := AutoTags(test.Before)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
got, want := tags, test.After
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("Got tag %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoTagsError(t *testing.T) {
|
||||||
|
var tests = []string{
|
||||||
|
"refs/tags/x1.0.0",
|
||||||
|
"refs/tags/20190203",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
_, err := AutoTags(test)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expect tag error for %s", test)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoTagsSuffix(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
Before string
|
||||||
|
Suffix string
|
||||||
|
After []string
|
||||||
|
}{
|
||||||
|
// without suffix
|
||||||
|
{
|
||||||
|
After: []string{"latest"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Before: "refs/tags/v1.0.0",
|
||||||
|
After: []string{
|
||||||
|
"1",
|
||||||
|
"1.0",
|
||||||
|
"1.0.0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// with suffix
|
||||||
|
{
|
||||||
|
Suffix: "linux-amd64",
|
||||||
|
After: []string{"linux-amd64"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Before: "refs/tags/v1.0.0",
|
||||||
|
Suffix: "linux-amd64",
|
||||||
|
After: []string{
|
||||||
|
"1-linux-amd64",
|
||||||
|
"1.0-linux-amd64",
|
||||||
|
"1.0.0-linux-amd64",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Suffix: "nanoserver",
|
||||||
|
After: []string{"nanoserver"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Before: "refs/tags/v1.9.2",
|
||||||
|
Suffix: "nanoserver",
|
||||||
|
After: []string{
|
||||||
|
"1-nanoserver",
|
||||||
|
"1.9-nanoserver",
|
||||||
|
"1.9.2-nanoserver",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Before: "refs/tags/v18.06.0",
|
||||||
|
Suffix: "nanoserver",
|
||||||
|
After: []string{
|
||||||
|
"18-nanoserver",
|
||||||
|
"18.06-nanoserver",
|
||||||
|
"18.06.0-nanoserver",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
tag, err := AutoTagsSuffix(test.Before, test.Suffix)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
got, want := tag, test.After
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("Got tag %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_stripHeadPrefix(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
ref string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
args: args{
|
||||||
|
ref: "refs/heads/master",
|
||||||
|
},
|
||||||
|
want: "master",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := stripHeadPrefix(tt.args.ref); got != tt.want {
|
||||||
|
t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUseAutoTag(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
ref string
|
||||||
|
defaultBranch string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "latest tag for default branch",
|
||||||
|
args: args{
|
||||||
|
ref: "refs/heads/master",
|
||||||
|
defaultBranch: "master",
|
||||||
|
},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "build from tags",
|
||||||
|
args: args{
|
||||||
|
ref: "refs/tags/v1.0.0",
|
||||||
|
defaultBranch: "master",
|
||||||
|
},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "skip build for not default branch",
|
||||||
|
args: args{
|
||||||
|
ref: "refs/heads/develop",
|
||||||
|
defaultBranch: "master",
|
||||||
|
},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := UseAutoTag(tt.args.ref, tt.args.defaultBranch); got != tt.want {
|
||||||
|
t.Errorf("%q. UseAutoTag() = %v, want %v", tt.name, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user