From a296e319082555eb3512d8a9de78d94900c70c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Codru=C8=9B=20Constantin=20Gu=C8=99oi?= Date: Mon, 11 Jan 2021 15:47:04 +0000 Subject: [PATCH] Add support for automatic opencontainer labels --- cmd/drone-docker/main.go | 49 ++++++++++++++++---------- docker.go | 76 +++++++++++++++++++++++++--------------- 2 files changed, 78 insertions(+), 47 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index f6448c2d..9989a36c 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -192,6 +192,17 @@ func main() { Usage: "label-schema labels", EnvVar: "PLUGIN_LABEL_SCHEMA", }, + cli.StringFlag{ + Name: "label-standard", + Usage: "label-standard off|label-schema|opencontainers", + Value: "label-schema", + EnvVar: "PLUGIN_LABEL_STANDARD", + }, + cli.StringFlag{ + Name: "link", + Usage: "link https://example.com/org/repo-name", + EnvVar: "PLUGIN_REPO_LINK,DRONE_REPO_LINK", + }, cli.StringFlag{ Name: "docker.registry", Usage: "docker registry", @@ -257,24 +268,26 @@ func run(c *cli.Context) error { Config: c.String("docker.config"), }, Build: docker.Build{ - Remote: c.String("remote.url"), - Name: c.String("commit.sha"), - Dockerfile: c.String("dockerfile"), - Context: c.String("context"), - Tags: c.StringSlice("tags"), - Args: c.StringSlice("args"), - ArgsEnv: c.StringSlice("args-from-env"), - Target: c.String("target"), - Squash: c.Bool("squash"), - Pull: c.BoolT("pull-image"), - CacheFrom: c.StringSlice("cache-from"), - Compress: c.Bool("compress"), - Repo: c.String("repo"), - Labels: c.StringSlice("custom-labels"), - LabelSchema: c.StringSlice("label-schema"), - NoCache: c.Bool("no-cache"), - AddHost: c.StringSlice("add-host"), - Quiet: c.Bool("quiet"), + Remote: c.String("remote.url"), + Name: c.String("commit.sha"), + Dockerfile: c.String("dockerfile"), + Context: c.String("context"), + Tags: c.StringSlice("tags"), + Args: c.StringSlice("args"), + ArgsEnv: c.StringSlice("args-from-env"), + Target: c.String("target"), + Squash: c.Bool("squash"), + Pull: c.BoolT("pull-image"), + CacheFrom: c.StringSlice("cache-from"), + Compress: c.Bool("compress"), + Repo: c.String("repo"), + Labels: c.StringSlice("custom-labels"), + LabelSchema: c.StringSlice("label-schema"), + LabelStandard: c.String("label-standard"), + Link: c.String("link"), + NoCache: c.Bool("no-cache"), + AddHost: c.StringSlice("add-host"), + Quiet: c.Bool("quiet"), }, Daemon: docker.Daemon{ Registry: c.String("docker.registry"), diff --git a/docker.go b/docker.go index 90301c28..f49ce3ea 100644 --- a/docker.go +++ b/docker.go @@ -39,24 +39,26 @@ type ( // Build defines Docker build parameters. Build struct { - Remote string // Git remote URL - Name string // Docker build using default named tag - Dockerfile string // Docker build Dockerfile - Context string // Docker build context - Tags []string // Docker build tags - Args []string // Docker build args - ArgsEnv []string // Docker build args from env - Target string // Docker build target - Squash bool // Docker build squash - Pull bool // Docker build pull - CacheFrom []string // Docker build cache-from - Compress bool // Docker build compress - Repo string // Docker build repository - LabelSchema []string // label-schema Label map - Labels []string // Label map - NoCache bool // Docker build no-cache - AddHost []string // Docker build add-host - Quiet bool // Docker build quiet + Remote string // Git remote URL + Name string // Docker build using default named tag + Dockerfile string // Docker build Dockerfile + Context string // Docker build context + Tags []string // Docker build tags + Args []string // Docker build args + ArgsEnv []string // Docker build args from env + Target string // Docker build target + Squash bool // Docker build squash + Pull bool // Docker build pull + CacheFrom []string // Docker build cache-from + Compress bool // Docker build compress + Repo string // Docker build repository + LabelSchema []string // label-schema Label map + LabelStandard string // label-standard string + Labels []string // Label map + Link string // Git repo link + NoCache bool // Docker build no-cache + AddHost []string // Docker build add-host + Quiet bool // Docker build quiet } // Plugin defines the Docker plugin parameters. @@ -252,19 +254,35 @@ func commandBuild(build Build) *exec.Cmd { args = append(args, "--quiet") } - labelSchema := []string{ - "schema-version=1.0", - fmt.Sprintf("build-date=%s", time.Now().Format(time.RFC3339)), - fmt.Sprintf("vcs-ref=%s", build.Name), - fmt.Sprintf("vcs-url=%s", build.Remote), - } + if build.LabelStandard != "off" { + labelSchema := []string{} + labelPrefix := "" + + if build.LabelStandard == "opencontainers" { + labelSchema = []string{ + fmt.Sprintf("created=%s", time.Now().Format(time.RFC3339)), + fmt.Sprintf("revision=%s", build.Name), + fmt.Sprintf("source=%s", build.Remote), + fmt.Sprintf("url=%s", build.Link), + } + labelPrefix = "org.opencontainers.image" + } else { + labelSchema = []string{ + "schema-version=1.0", + fmt.Sprintf("build-date=%s", time.Now().Format(time.RFC3339)), + fmt.Sprintf("vcs-ref=%s", build.Name), + fmt.Sprintf("vcs-url=%s", build.Remote), + } + labelPrefix = "org.label-schema" + } - if len(build.LabelSchema) > 0 { - labelSchema = append(labelSchema, build.LabelSchema...) - } + if len(build.LabelSchema) > 0 { + labelSchema = append(labelSchema, build.LabelSchema...) + } - for _, label := range labelSchema { - args = append(args, "--label", fmt.Sprintf("org.label-schema.%s", label)) + for _, label := range labelSchema { + args = append(args, "--label", fmt.Sprintf("%s.%s", labelPrefix, label)) + } } if len(build.Labels) > 0 {