Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port the auto tag feature from docker plugin #36

Merged
merged 3 commits into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 46 additions & 5 deletions README.md
Expand Up @@ -39,6 +39,7 @@ docker build \
```

## Usage
### Manual Tagging

```console
docker run --rm \
Expand All @@ -52,14 +53,12 @@ docker run --rm \
plugins/kaniko:linux-amd64
```

### Automatic Tagging

With auto tagging enabled, semantic versions can be passed to PLUGIN_TAGS directly for expansion:
With expanded tagging enabled, semantic versions can be passed to PLUGIN_TAGS directly for expansion:

```console
docker run --rm \
-e PLUGIN_TAGS=v1.2.3,latest \
-e PLUGIN_AUTO_TAG=true \
-e PLUGIN_EXPAND_TAGS=true \
-v $(pwd):/drone \
-w /drone \
plugins/kaniko:linux-amd64
Expand All @@ -72,14 +71,56 @@ PLUGIN_TAGS=1,1.2,1.2.3,latest

This allows for passing `$DRONE_TAG` directly as a tag for repos that use [semver](https://semver.org) tags.

To avoid confusion between repo tags and image tags, `PLUGIN_AUTO_TAG` also recognizes a semantic version
To avoid confusion between repo tags and image tags, `PLUGIN_EXPAND_TAGS` 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_TAGS=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.

#### 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
40 changes: 34 additions & 6 deletions cmd/kaniko-docker/main.go
Expand Up @@ -14,6 +14,7 @@ import (

kaniko "github.com/drone/drone-kaniko"
"github.com/drone/drone-kaniko/pkg/artifact"
"github.com/drone/drone-kaniko/pkg/tagger"
)

const (
Expand Down Expand Up @@ -58,18 +59,38 @@ func main() {
Value: ".",
EnvVar: "PLUGIN_CONTEXT",
},
cli.StringFlag{
Name: "commit.ref",
Usage: "git commit ref",
EnvVar: "DRONE_COMMIT_REF",
},
cli.StringFlag{
Name: "repo.branch",
Usage: "repository default branch",
EnvVar: "DRONE_REPO_BRANCH",
},
cli.StringSliceFlag{
Name: "tags",
Usage: "build tags",
Value: &cli.StringSlice{"latest"},
Value: &cli.StringSlice{},
EnvVar: "PLUGIN_TAGS",
FilePath: ".tags",
},
cli.BoolFlag{
Name: "auto_tag",
Name: "tag.expand",
Usage: "enable for semver tagging",
EnvVar: "PLUGIN_EXPAND_TAGS",
},
cli.BoolFlag{
Name: "tags.auto",
Usage: "enable auto generate build tags",
EnvVar: "PLUGIN_AUTO_TAG",
},
cli.StringFlag{
Name: "tags.auto_suffix",
Usage: "the suffix of auto build tags",
EnvVar: "PLUGIN_AUTO_TAG_SUFFIX",
},
cli.StringSliceFlag{
Name: "args",
Usage: "build args",
Expand Down Expand Up @@ -169,12 +190,19 @@ func run(c *cli.Context) error {
}
}

enableAutoTag := c.Bool("tags.auto")
enableExpandTag := c.Bool("tags.expand")
tags, shouldSkipBuild, err := tagger.MaybeAutoTag(c.StringSlice("tags"), c.String("commit.ref"), c.String("tags.auto_suffix"), c.String("repo.branch"), enableAutoTag, enableExpandTag)
if shouldSkipBuild {
return err
}

plugin := kaniko.Plugin{
Build: kaniko.Build{
Dockerfile: c.String("dockerfile"),
Context: c.String("context"),
Tags: c.StringSlice("tags"),
AutoTag: c.Bool("auto_tag"),
Tags: tags,
ExpandTag: !enableAutoTag && enableExpandTag,
Args: c.StringSlice("args"),
Target: c.String("target"),
Repo: c.String("repo"),
Expand All @@ -190,7 +218,7 @@ func run(c *cli.Context) error {
Platform: c.String("platform"),
},
Artifact: kaniko.Artifact{
Tags: c.StringSlice("tags"),
Tags: tags,
Repo: buildRepo(c.String("registry"), c.String("repo")),
Registry: c.String("registry"),
ArtifactFile: c.String("artifact-file"),
Expand Down Expand Up @@ -238,7 +266,7 @@ func buildRepo(registry, repo string) string {
// No custom registry, just return the repo name
return repo
}
if strings.HasPrefix(repo, registry + "/") {
if strings.HasPrefix(repo, registry+"/") {
// Repo already includes the registry prefix
// For backward compatibility, we won't add the prefix again.
return repo
Expand Down
38 changes: 33 additions & 5 deletions cmd/kaniko-ecr/main.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/drone/drone-kaniko/pkg/tagger"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -71,18 +72,38 @@ func main() {
Value: ".",
EnvVar: "PLUGIN_CONTEXT",
},
cli.StringFlag{
Name: "commit.ref",
Usage: "git commit ref",
EnvVar: "DRONE_COMMIT_REF",
},
cli.StringFlag{
Name: "repo.branch",
Usage: "repository default branch",
EnvVar: "DRONE_REPO_BRANCH",
},
cli.StringSliceFlag{
Name: "tags",
Usage: "build tags",
Value: &cli.StringSlice{"latest"},
Value: &cli.StringSlice{},
EnvVar: "PLUGIN_TAGS",
FilePath: ".tags",
},
cli.BoolFlag{
Name: "auto_tag",
Name: "expand_tags",
Usage: "enable for semver tagging",
EnvVar: "PLUGIN_EXPAND_TAGS",
},
cli.BoolFlag{
Name: "tags.auto",
Usage: "enable auto generate build tags",
EnvVar: "PLUGIN_AUTO_TAG",
},
cli.StringFlag{
Name: "tags.auto_suffix",
Usage: "the suffix of auto build tags",
EnvVar: "PLUGIN_AUTO_TAG_SUFFIX",
},
cli.StringSliceFlag{
Name: "args",
Usage: "build args",
Expand Down Expand Up @@ -240,12 +261,19 @@ func run(c *cli.Context) error {
}
}

enableAutoTag := c.Bool("tags.auto")
enableExpandTag := c.Bool("tags.expand")
tags, shouldSkipBuild, err := tagger.MaybeAutoTag(c.StringSlice("tags"), c.String("commit.ref"), c.String("tags.auto_suffix"), c.String("repo.branch"), enableAutoTag, enableExpandTag)
if shouldSkipBuild {
return err
}

plugin := kaniko.Plugin{
Build: kaniko.Build{
Dockerfile: c.String("dockerfile"),
Context: c.String("context"),
Tags: c.StringSlice("tags"),
AutoTag: c.Bool("auto_tag"),
Tags: tags,
ExpandTag: !enableAutoTag && enableExpandTag,
Args: c.StringSlice("args"),
Target: c.String("target"),
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
Expand All @@ -260,7 +288,7 @@ func run(c *cli.Context) error {
Platform: c.String("platform"),
},
Artifact: kaniko.Artifact{
Tags: c.StringSlice("tags"),
Tags: tags,
Repo: c.String("repo"),
Registry: c.String("registry"),
ArtifactFile: c.String("artifact-file"),
Expand Down
38 changes: 33 additions & 5 deletions cmd/kaniko-gcr/main.go
Expand Up @@ -12,6 +12,7 @@ import (

kaniko "github.com/drone/drone-kaniko"
"github.com/drone/drone-kaniko/pkg/artifact"
"github.com/drone/drone-kaniko/pkg/tagger"
)

const (
Expand Down Expand Up @@ -52,18 +53,38 @@ func main() {
Value: ".",
EnvVar: "PLUGIN_CONTEXT",
},
cli.StringFlag{
Name: "commit.ref",
Usage: "git commit ref",
EnvVar: "DRONE_COMMIT_REF",
},
cli.StringFlag{
Name: "repo.branch",
Usage: "repository default branch",
EnvVar: "DRONE_REPO_BRANCH",
},
cli.StringSliceFlag{
Name: "tags",
Usage: "build tags",
Value: &cli.StringSlice{"latest"},
Value: &cli.StringSlice{},
EnvVar: "PLUGIN_TAGS",
FilePath: ".tags",
},
cli.BoolFlag{
Name: "auto_tag",
Name: "expand_tags",
Usage: "enable for semver tagging",
EnvVar: "PLUGIN_EXPAND_TAGS",
},
cli.BoolFlag{
Name: "tags.auto",
Usage: "enable auto generate build tags",
EnvVar: "PLUGIN_AUTO_TAG",
},
cli.StringFlag{
Name: "tags.auto_suffix",
Usage: "the suffix of auto build tags",
EnvVar: "PLUGIN_AUTO_TAG_SUFFIX",
},
cli.StringSliceFlag{
Name: "args",
Usage: "build args",
Expand Down Expand Up @@ -155,12 +176,19 @@ func run(c *cli.Context) error {
}
}

enableAutoTag := c.Bool("tags.auto")
enableExpandTag := c.Bool("tags.expand")
tags, shouldSkipBuild, err := tagger.MaybeAutoTag(c.StringSlice("tags"), c.String("commit.ref"), c.String("tags.auto_suffix"), c.String("repo.branch"), enableAutoTag, enableExpandTag)
15cm marked this conversation as resolved.
Show resolved Hide resolved
if shouldSkipBuild {
return err
}

plugin := kaniko.Plugin{
Build: kaniko.Build{
Dockerfile: c.String("dockerfile"),
Context: c.String("context"),
Tags: c.StringSlice("tags"),
AutoTag: c.Bool("auto_tag"),
Tags: tags,
ExpandTag: !enableAutoTag && enableExpandTag,
Args: c.StringSlice("args"),
Target: c.String("target"),
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
Expand All @@ -175,7 +203,7 @@ func run(c *cli.Context) error {
Platform: c.String("platform"),
},
Artifact: kaniko.Artifact{
Tags: c.StringSlice("tags"),
Tags: tags,
Repo: c.String("repo"),
Registry: c.String("registry"),
ArtifactFile: c.String("artifact-file"),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -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/ecrpublic v1.4.3
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/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -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/smithy-go v1.7.0 h1:+cLHMRrDZvQ4wk+KuQ9yH6eEg6KZEJ9RI2IkDqnygCg=
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/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
6 changes: 3 additions & 3 deletions kaniko.go
Expand Up @@ -17,7 +17,7 @@ type (
Dockerfile string // Docker build Dockerfile
Context string // Docker build context
Tags []string // Docker build tags
AutoTag bool // Set this to create semver-tagged labels
ExpandTag bool // Set this to create semver-tagged labels
Args []string // Docker build args
Target string // Docker build target
Repo string // Docker build repository
Expand Down Expand Up @@ -49,7 +49,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.
// Pre-release information (e.g. -rc1) suppresses major and major+minor auto-labels.
Expand All @@ -67,7 +67,7 @@ func (b Build) labelsForTag(tag string) (labels []string) {
}

// Pass through tags if auto-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}
}
tag = semverTag
Expand Down