Add support for automatic opencontainer labels

This commit is contained in:
Codruț Constantin Gușoi 2021-01-11 15:47:04 +00:00
parent 9c86f98ea5
commit bd4029884c
2 changed files with 64 additions and 47 deletions

@ -192,6 +192,16 @@ func main() {
Usage: "label-schema labels", Usage: "label-schema labels",
EnvVar: "PLUGIN_LABEL_SCHEMA", EnvVar: "PLUGIN_LABEL_SCHEMA",
}, },
cli.BoolTFlag{
Name: "auto-label",
Usage: "auto-label true|false",
EnvVar: "PLUGIN_AUTO_LABEL",
},
cli.StringFlag{
Name: "link",
Usage: "link https://example.com/org/repo-name",
EnvVar: "PLUGIN_REPO_LINK,DRONE_REPO_LINK",
},
cli.StringFlag{ cli.StringFlag{
Name: "docker.registry", Name: "docker.registry",
Usage: "docker registry", Usage: "docker registry",
@ -257,24 +267,26 @@ func run(c *cli.Context) error {
Config: c.String("docker.config"), Config: c.String("docker.config"),
}, },
Build: docker.Build{ Build: docker.Build{
Remote: c.String("remote.url"), Remote: c.String("remote.url"),
Name: c.String("commit.sha"), Name: c.String("commit.sha"),
Dockerfile: c.String("dockerfile"), Dockerfile: c.String("dockerfile"),
Context: c.String("context"), Context: c.String("context"),
Tags: c.StringSlice("tags"), Tags: c.StringSlice("tags"),
Args: c.StringSlice("args"), Args: c.StringSlice("args"),
ArgsEnv: c.StringSlice("args-from-env"), ArgsEnv: c.StringSlice("args-from-env"),
Target: c.String("target"), Target: c.String("target"),
Squash: c.Bool("squash"), Squash: c.Bool("squash"),
Pull: c.BoolT("pull-image"), Pull: c.BoolT("pull-image"),
CacheFrom: c.StringSlice("cache-from"), CacheFrom: c.StringSlice("cache-from"),
Compress: c.Bool("compress"), Compress: c.Bool("compress"),
Repo: c.String("repo"), Repo: c.String("repo"),
Labels: c.StringSlice("custom-labels"), Labels: c.StringSlice("custom-labels"),
LabelSchema: c.StringSlice("label-schema"), LabelSchema: c.StringSlice("label-schema"),
NoCache: c.Bool("no-cache"), AutoLabel: c.BoolT("auto-label"),
AddHost: c.StringSlice("add-host"), Link: c.String("link"),
Quiet: c.Bool("quiet"), NoCache: c.Bool("no-cache"),
AddHost: c.StringSlice("add-host"),
Quiet: c.Bool("quiet"),
}, },
Daemon: docker.Daemon{ Daemon: docker.Daemon{
Registry: c.String("docker.registry"), Registry: c.String("docker.registry"),

@ -39,24 +39,26 @@ type (
// Build defines Docker build parameters. // Build defines Docker build parameters.
Build struct { Build struct {
Remote string // Git remote URL Remote string // Git remote URL
Name string // Docker build using default named tag Name string // Docker build using default named tag
Dockerfile string // Docker build Dockerfile Dockerfile string // Docker build Dockerfile
Context string // Docker build context Context string // Docker build context
Tags []string // Docker build tags Tags []string // Docker build tags
Args []string // Docker build args Args []string // Docker build args
ArgsEnv []string // Docker build args from env ArgsEnv []string // Docker build args from env
Target string // Docker build target Target string // Docker build target
Squash bool // Docker build squash Squash bool // Docker build squash
Pull bool // Docker build pull Pull bool // Docker build pull
CacheFrom []string // Docker build cache-from CacheFrom []string // Docker build cache-from
Compress bool // Docker build compress Compress bool // Docker build compress
Repo string // Docker build repository Repo string // Docker build repository
LabelSchema []string // label-schema Label map LabelSchema []string // label-schema Label map
Labels []string // Label map AutoLabel bool // auto-label bool
NoCache bool // Docker build no-cache Labels []string // Label map
AddHost []string // Docker build add-host Link string // Git repo link
Quiet bool // Docker build quiet NoCache bool // Docker build no-cache
AddHost []string // Docker build add-host
Quiet bool // Docker build quiet
} }
// Plugin defines the Docker plugin parameters. // Plugin defines the Docker plugin parameters.
@ -252,19 +254,22 @@ func commandBuild(build Build) *exec.Cmd {
args = append(args, "--quiet") args = append(args, "--quiet")
} }
labelSchema := []string{ if build.AutoLabel {
"schema-version=1.0", labelSchema := []string{
fmt.Sprintf("build-date=%s", time.Now().Format(time.RFC3339)), fmt.Sprintf("created=%s", time.Now().Format(time.RFC3339)),
fmt.Sprintf("vcs-ref=%s", build.Name), fmt.Sprintf("revision=%s", build.Name),
fmt.Sprintf("vcs-url=%s", build.Remote), fmt.Sprintf("source=%s", build.Remote),
} fmt.Sprintf("url=%s", build.Link),
}
labelPrefix := "org.opencontainers.image"
if len(build.LabelSchema) > 0 { if len(build.LabelSchema) > 0 {
labelSchema = append(labelSchema, build.LabelSchema...) labelSchema = append(labelSchema, build.LabelSchema...)
} }
for _, label := range labelSchema { for _, label := range labelSchema {
args = append(args, "--label", fmt.Sprintf("org.label-schema.%s", label)) args = append(args, "--label", fmt.Sprintf("%s.%s", labelPrefix, label))
}
} }
if len(build.Labels) > 0 { if len(build.Labels) > 0 {