mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-14 14:02:35 +08:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b1c01c18ff | |||
| a11da49965 | |||
| fae9271dc7 | |||
| 8bd45524d4 | |||
| 427f9abb99 | |||
| dbdf13277d | |||
| c38eb07dea | |||
| de43f3afb6 | |||
| bebe65a6c2 |
+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 \
|
||||||
@@ -52,14 +53,14 @@ docker run --rm \
|
|||||||
plugins/kaniko:linux-amd64
|
plugins/kaniko:linux-amd64
|
||||||
```
|
```
|
||||||
|
|
||||||
### Automatic Tagging
|
With expanded tagging enabled, semantic versions can be passed to PLUGIN_TAGS directly for expansion.
|
||||||
|
|
||||||
With auto 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
|
```console
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
-e PLUGIN_TAGS=v1.2.3,latest \
|
-e PLUGIN_TAGS=v1.2.3,latest \
|
||||||
-e PLUGIN_AUTO_TAG=true \
|
-e PLUGIN_EXPAND_TAG=true \
|
||||||
-v $(pwd):/drone \
|
-v $(pwd):/drone \
|
||||||
-w /drone \
|
-w /drone \
|
||||||
plugins/kaniko:linux-amd64
|
plugins/kaniko:linux-amd64
|
||||||
@@ -72,14 +73,58 @@ 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.
|
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
|
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:
|
without the `v` prefix. As such, the following is also equivalent to the above:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
-e PLUGIN_TAGS=1.2.3,latest \
|
-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 \
|
-e PLUGIN_AUTO_TAG=true \
|
||||||
-v $(pwd):/drone \
|
-v $(pwd):/drone \
|
||||||
-w /drone \
|
-w /drone \
|
||||||
plugins/kaniko:linux-amd64
|
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
|
||||||
|
|||||||
+51
-19
@@ -58,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",
|
||||||
@@ -66,10 +76,20 @@ func main() {
|
|||||||
FilePath: ".tags",
|
FilePath: ".tags",
|
||||||
},
|
},
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "auto_tag",
|
Name: "expand-tag",
|
||||||
Usage: "enable for semver tagging",
|
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",
|
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",
|
||||||
@@ -96,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",
|
||||||
@@ -171,23 +196,28 @@ 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"),
|
||||||
AutoTag: c.Bool("auto_tag"),
|
Context: c.String("context"),
|
||||||
Args: c.StringSlice("args"),
|
Tags: c.StringSlice("tags"),
|
||||||
Target: c.String("target"),
|
AutoTag: c.Bool("auto-tag"),
|
||||||
Repo: c.String("repo"),
|
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||||
Labels: c.StringSlice("custom-labels"),
|
ExpandTag: c.Bool("expand-tag"),
|
||||||
SkipTlsVerify: c.Bool("skip-tls-verify"),
|
Args: c.StringSlice("args"),
|
||||||
SnapshotMode: c.String("snapshot-mode"),
|
Target: c.String("target"),
|
||||||
EnableCache: c.Bool("enable-cache"),
|
Repo: buildRepo(c.String("registry"), c.String("repo")),
|
||||||
CacheRepo: c.String("cache-repo"),
|
Mirrors: c.StringSlice("registry-mirrors"),
|
||||||
CacheTTL: c.Int("cache-ttl"),
|
Labels: c.StringSlice("custom-labels"),
|
||||||
DigestFile: defaultDigestFile,
|
SkipTlsVerify: c.Bool("skip-tls-verify"),
|
||||||
NoPush: noPush,
|
SnapshotMode: c.String("snapshot-mode"),
|
||||||
Verbosity: c.String("verbosity"),
|
EnableCache: c.Bool("enable-cache"),
|
||||||
Platform: c.String("platform"),
|
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"),
|
||||||
@@ -238,7 +268,9 @@ func buildRepo(registry, repo string) string {
|
|||||||
// No custom registry, just return the repo name
|
// No custom registry, just return the repo name
|
||||||
return repo
|
return repo
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(repo, registry + "/") {
|
// 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
|
// Repo already includes the registry prefix
|
||||||
// For backward compatibility, we won't add the prefix again.
|
// For backward compatibility, we won't add the prefix again.
|
||||||
return repo
|
return repo
|
||||||
|
|||||||
+47
-17
@@ -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",
|
||||||
@@ -79,10 +89,20 @@ func main() {
|
|||||||
FilePath: ".tags",
|
FilePath: ".tags",
|
||||||
},
|
},
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "auto_tag",
|
Name: "expand-tag",
|
||||||
Usage: "enable for semver tagging",
|
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",
|
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",
|
||||||
@@ -119,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",
|
||||||
@@ -242,22 +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"),
|
||||||
AutoTag: c.Bool("auto_tag"),
|
Context: c.String("context"),
|
||||||
Args: c.StringSlice("args"),
|
Tags: c.StringSlice("tags"),
|
||||||
Target: c.String("target"),
|
AutoTag: c.Bool("auto-tag"),
|
||||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||||
Labels: c.StringSlice("custom-labels"),
|
ExpandTag: c.Bool("expand-tag"),
|
||||||
SnapshotMode: c.String("snapshot-mode"),
|
Args: c.StringSlice("args"),
|
||||||
EnableCache: c.Bool("enable-cache"),
|
Target: c.String("target"),
|
||||||
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
|
Repo: fmt.Sprintf("%s/%s", 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,
|
SnapshotMode: c.String("snapshot-mode"),
|
||||||
Verbosity: c.String("verbosity"),
|
EnableCache: c.Bool("enable-cache"),
|
||||||
Platform: c.String("platform"),
|
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"),
|
||||||
|
|||||||
+47
-17
@@ -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",
|
||||||
@@ -60,10 +70,20 @@ func main() {
|
|||||||
FilePath: ".tags",
|
FilePath: ".tags",
|
||||||
},
|
},
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "auto_tag",
|
Name: "expand-tag",
|
||||||
Usage: "enable for semver tagging",
|
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",
|
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 +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",
|
||||||
@@ -157,22 +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"),
|
||||||
AutoTag: c.Bool("auto_tag"),
|
Context: c.String("context"),
|
||||||
Args: c.StringSlice("args"),
|
Tags: c.StringSlice("tags"),
|
||||||
Target: c.String("target"),
|
AutoTag: c.Bool("auto-tag"),
|
||||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||||
Labels: c.StringSlice("custom-labels"),
|
ExpandTag: c.Bool("expand-tag"),
|
||||||
SnapshotMode: c.String("snapshot-mode"),
|
Args: c.StringSlice("args"),
|
||||||
EnableCache: c.Bool("enable-cache"),
|
Target: c.String("target"),
|
||||||
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
|
Repo: fmt.Sprintf("%s/%s", 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,
|
SnapshotMode: c.String("snapshot-mode"),
|
||||||
Verbosity: c.String("verbosity"),
|
EnableCache: c.Bool("enable-cache"),
|
||||||
Platform: c.String("platform"),
|
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"),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM gcr.io/kaniko-project/executor:v1.7.0
|
FROM gcr.io/kaniko-project/executor:v1.6.0
|
||||||
|
|
||||||
ADD release/linux/amd64/kaniko-docker /kaniko/
|
ADD release/linux/amd64/kaniko-docker /kaniko/
|
||||||
ENTRYPOINT ["/kaniko/kaniko-docker"]
|
ENTRYPOINT ["/kaniko/kaniko-docker"]
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
FROM gcr.io/kaniko-project/executor:v1.8.0
|
||||||
|
|
||||||
|
ADD release/linux/amd64/kaniko-docker /kaniko/
|
||||||
|
ENTRYPOINT ["/kaniko/kaniko-docker"]
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM gcr.io/kaniko-project/executor:v1.7.0
|
FROM gcr.io/kaniko-project/executor:arm64-v1.6.0
|
||||||
|
|
||||||
ENV HOME /root
|
ENV HOME /root
|
||||||
ENV USER root
|
ENV USER root
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM gcr.io/kaniko-project/executor:v1.7.0
|
FROM gcr.io/kaniko-project/executor:v1.6.0
|
||||||
|
|
||||||
ADD release/linux/amd64/kaniko-ecr /kaniko/
|
ADD release/linux/amd64/kaniko-ecr /kaniko/
|
||||||
ENTRYPOINT ["/kaniko/kaniko-ecr"]
|
ENTRYPOINT ["/kaniko/kaniko-ecr"]
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
FROM gcr.io/kaniko-project/executor:v1.8.0
|
||||||
|
|
||||||
|
ADD release/linux/amd64/kaniko-ecr /kaniko/
|
||||||
|
ENTRYPOINT ["/kaniko/kaniko-ecr"]
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM gcr.io/kaniko-project/executor:v1.7.0
|
FROM gcr.io/kaniko-project/executor:arm64-v1.6.0
|
||||||
|
|
||||||
ENV HOME /root
|
ENV HOME /root
|
||||||
ENV USER root
|
ENV USER root
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
image: plugins/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/ecr:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64-kaniko1.8.0
|
||||||
|
platform:
|
||||||
|
architecture: amd64
|
||||||
|
os: linux
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM gcr.io/kaniko-project/executor:v1.7.0
|
FROM gcr.io/kaniko-project/executor:v1.6.0
|
||||||
|
|
||||||
ADD release/linux/amd64/kaniko-gcr /kaniko/
|
ADD release/linux/amd64/kaniko-gcr /kaniko/
|
||||||
ENTRYPOINT ["/kaniko/kaniko-gcr"]
|
ENTRYPOINT ["/kaniko/kaniko-gcr"]
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
FROM gcr.io/kaniko-project/executor:v1.8.0
|
||||||
|
|
||||||
|
ADD release/linux/amd64/kaniko-gcr /kaniko/
|
||||||
|
ENTRYPOINT ["/kaniko/kaniko-gcr"]
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM gcr.io/kaniko-project/executor:v1.7.0
|
FROM gcr.io/kaniko-project/executor:arm64-v1.6.0
|
||||||
|
|
||||||
ENV HOME /root
|
ENV HOME /root
|
||||||
ENV USER root
|
ENV USER root
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
image: plugins/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/gcr:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64-kaniko1.8.0
|
||||||
|
platform:
|
||||||
|
architecture: amd64
|
||||||
|
os: linux
|
||||||
@@ -6,6 +6,7 @@ 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/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
|
||||||
|
|||||||
@@ -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=
|
||||||
|
|||||||
@@ -8,29 +8,35 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||||
|
"github.com/drone/drone-kaniko/pkg/tagger"
|
||||||
"golang.org/x/mod/semver"
|
"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
|
||||||
AutoTag bool // Set this to create semver-tagged labels
|
Context string // Docker build context
|
||||||
Args []string // Docker build args
|
Tags []string // Docker build tags
|
||||||
Target string // Docker build target
|
AutoTag bool // Set this to auto detect tags from git commits and semver-tagged labels
|
||||||
Repo string // Docker build repository
|
AutoTagSuffix string // Suffix to append to the auto detect tags
|
||||||
Labels []string // Label map
|
ExpandTag bool // Set this to expand the `Tags` into semver-tagged labels
|
||||||
SkipTlsVerify bool // Docker skip tls certificate verify for registry
|
Args []string // Docker build args
|
||||||
SnapshotMode string // Kaniko snapshot mode
|
Target string // Docker build target
|
||||||
EnableCache bool // Whether to enable kaniko cache
|
Repo string // Docker build repository
|
||||||
CacheRepo string // Remote repository that will be used to store cached layers
|
Mirrors []string // Docker repository mirrors
|
||||||
CacheTTL int // Cache timeout in hours
|
Labels []string // Label map
|
||||||
DigestFile string // Digest file location
|
SkipTlsVerify bool // Docker skip tls certificate verify for registry
|
||||||
NoPush bool // Set this flag if you only want to build the image, without pushing to a registry
|
SnapshotMode string // Kaniko snapshot mode
|
||||||
Verbosity string // Log level
|
EnableCache bool // Whether to enable kaniko cache
|
||||||
Platform string // Allows to build with another default platform than the host, similarly to docker build --platform
|
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
|
||||||
@@ -49,7 +55,7 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// labelsForTag returns the labels to use for the given tag, subject to the value of AutoTag.
|
// 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.
|
// 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.
|
// Pre-release information (e.g. -rc1) suppresses major and major+minor auto-labels.
|
||||||
@@ -66,8 +72,8 @@ func (b Build) labelsForTag(tag string) (labels []string) {
|
|||||||
semverTag = withV
|
semverTag = withV
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pass through tags if auto-tag is not set, or if the tag is not a semantic version
|
// Pass through tags if expand-tag is not set, or if the tag is not a semantic version
|
||||||
if !b.AutoTag || !semver.IsValid(semverTag) {
|
if !b.ExpandTag || !semver.IsValid(semverTag) {
|
||||||
return []string{tag}
|
return []string{tag}
|
||||||
}
|
}
|
||||||
tag = semverTag
|
tag = semverTag
|
||||||
@@ -90,6 +96,30 @@ func (b Build) labelsForTag(tag string) (labels []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 == "" {
|
||||||
@@ -100,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),
|
||||||
@@ -107,7 +149,7 @@ 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 {
|
||||||
for _, label := range p.Build.labelsForTag(tag) {
|
for _, label := range p.Build.labelsForTag(tag) {
|
||||||
cmdArgs = append(cmdArgs, fmt.Sprintf("--destination=%s:%s", p.Build.Repo, label))
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--destination=%s:%s", p.Build.Repo, label))
|
||||||
}
|
}
|
||||||
@@ -121,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))
|
||||||
}
|
}
|
||||||
|
|||||||
+102
-35
@@ -8,67 +8,134 @@ import (
|
|||||||
|
|
||||||
func TestBuild_labelsForTag(t *testing.T) {
|
func TestBuild_labelsForTag(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
tag string
|
tag string
|
||||||
autoTags []string
|
expandTags []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "semver",
|
name: "semver",
|
||||||
tag: "v1.2.3",
|
tag: "v1.2.3",
|
||||||
autoTags: []string{"1", "1.2", "1.2.3"},
|
expandTags: []string{"1", "1.2", "1.2.3"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "no_patch",
|
name: "no_patch",
|
||||||
tag: "v1.2",
|
tag: "v1.2",
|
||||||
autoTags: []string{"1", "1.2", "1.2.0"},
|
expandTags: []string{"1", "1.2", "1.2.0"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "only_major",
|
name: "only_major",
|
||||||
tag: "v1",
|
tag: "v1",
|
||||||
autoTags: []string{"1", "1.0", "1.0.0"},
|
expandTags: []string{"1", "1.0", "1.0.0"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "full_with_build",
|
name: "full_with_build",
|
||||||
tag: "v1.2.3+build-info",
|
tag: "v1.2.3+build-info",
|
||||||
autoTags: []string{"1+build-info", "1.2+build-info", "1.2.3+build-info"},
|
expandTags: []string{"1+build-info", "1.2+build-info", "1.2.3+build-info"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "build_with_underscores",
|
name: "build_with_underscores",
|
||||||
tag: "v1.2.3+linux_amd64",
|
tag: "v1.2.3+linux_amd64",
|
||||||
autoTags: []string{"1+linux-amd64", "1.2+linux-amd64", "1.2.3+linux-amd64"},
|
expandTags: []string{"1+linux-amd64", "1.2+linux-amd64", "1.2.3+linux-amd64"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "prerelease",
|
name: "prerelease",
|
||||||
tag: "v1.2.3-rc1",
|
tag: "v1.2.3-rc1",
|
||||||
autoTags: []string{"1.2.3-rc1"},
|
expandTags: []string{"1.2.3-rc1"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "prerelease_with_build",
|
name: "prerelease_with_build",
|
||||||
tag: "v1.2.3-rc1+bld",
|
tag: "v1.2.3-rc1+bld",
|
||||||
autoTags: []string{"1.2.3-rc1+bld"},
|
expandTags: []string{"1.2.3-rc1+bld"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid_build",
|
name: "invalid_build",
|
||||||
tag: "v1+bld", // can only include build detail with all three elements
|
tag: "v1+bld", // can only include build detail with all three elements
|
||||||
autoTags: []string{"v1+bld"},
|
expandTags: []string{"v1+bld"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "accidental_non_semver",
|
name: "accidental_non_semver",
|
||||||
tag: "1.2.3",
|
tag: "1.2.3",
|
||||||
autoTags: []string{"1", "1.2", "1.2.3"},
|
expandTags: []string{"1", "1.2", "1.2.3"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "non_semver",
|
name: "non_semver",
|
||||||
tag: "latest",
|
tag: "latest",
|
||||||
autoTags: []string{"latest"},
|
expandTags: []string{"latest"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
tags := Build{AutoTag: true}.labelsForTag(tt.tag)
|
tags := Build{ExpandTag: true}.labelsForTag(tt.tag)
|
||||||
if got, want := tags, tt.autoTags; !cmp.Equal(got, want) {
|
if got, want := tags, tt.expandTags; !cmp.Equal(got, want) {
|
||||||
t.Errorf("tagsFor(%q) = %q, want %q", tt.tag, 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