Skip to content

Commit 0f6bd8a

Browse files
Merge pull request drone-plugins#313 from codrut-fc/opencontainer-labels
Add support for automatic opencontainer labels
2 parents 7ade37a + bd40298 commit 0f6bd8a

File tree

2 files changed

+64
-47
lines changed

2 files changed

+64
-47
lines changed

cmd/drone-docker/main.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ func main() {
192192
Usage: "label-schema labels",
193193
EnvVar: "PLUGIN_LABEL_SCHEMA",
194194
},
195+
cli.BoolTFlag{
196+
Name: "auto-label",
197+
Usage: "auto-label true|false",
198+
EnvVar: "PLUGIN_AUTO_LABEL",
199+
},
200+
cli.StringFlag{
201+
Name: "link",
202+
Usage: "link https://example.com/org/repo-name",
203+
EnvVar: "PLUGIN_REPO_LINK,DRONE_REPO_LINK",
204+
},
195205
cli.StringFlag{
196206
Name: "docker.registry",
197207
Usage: "docker registry",
@@ -257,24 +267,26 @@ func run(c *cli.Context) error {
257267
Config: c.String("docker.config"),
258268
},
259269
Build: docker.Build{
260-
Remote: c.String("remote.url"),
261-
Name: c.String("commit.sha"),
262-
Dockerfile: c.String("dockerfile"),
263-
Context: c.String("context"),
264-
Tags: c.StringSlice("tags"),
265-
Args: c.StringSlice("args"),
266-
ArgsEnv: c.StringSlice("args-from-env"),
267-
Target: c.String("target"),
268-
Squash: c.Bool("squash"),
269-
Pull: c.BoolT("pull-image"),
270-
CacheFrom: c.StringSlice("cache-from"),
271-
Compress: c.Bool("compress"),
272-
Repo: c.String("repo"),
273-
Labels: c.StringSlice("custom-labels"),
274-
LabelSchema: c.StringSlice("label-schema"),
275-
NoCache: c.Bool("no-cache"),
276-
AddHost: c.StringSlice("add-host"),
277-
Quiet: c.Bool("quiet"),
270+
Remote: c.String("remote.url"),
271+
Name: c.String("commit.sha"),
272+
Dockerfile: c.String("dockerfile"),
273+
Context: c.String("context"),
274+
Tags: c.StringSlice("tags"),
275+
Args: c.StringSlice("args"),
276+
ArgsEnv: c.StringSlice("args-from-env"),
277+
Target: c.String("target"),
278+
Squash: c.Bool("squash"),
279+
Pull: c.BoolT("pull-image"),
280+
CacheFrom: c.StringSlice("cache-from"),
281+
Compress: c.Bool("compress"),
282+
Repo: c.String("repo"),
283+
Labels: c.StringSlice("custom-labels"),
284+
LabelSchema: c.StringSlice("label-schema"),
285+
AutoLabel: c.BoolT("auto-label"),
286+
Link: c.String("link"),
287+
NoCache: c.Bool("no-cache"),
288+
AddHost: c.StringSlice("add-host"),
289+
Quiet: c.Bool("quiet"),
278290
},
279291
Daemon: docker.Daemon{
280292
Registry: c.String("docker.registry"),

docker.go

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,26 @@ type (
3939

4040
// Build defines Docker build parameters.
4141
Build struct {
42-
Remote string // Git remote URL
43-
Name string // Docker build using default named tag
44-
Dockerfile string // Docker build Dockerfile
45-
Context string // Docker build context
46-
Tags []string // Docker build tags
47-
Args []string // Docker build args
48-
ArgsEnv []string // Docker build args from env
49-
Target string // Docker build target
50-
Squash bool // Docker build squash
51-
Pull bool // Docker build pull
52-
CacheFrom []string // Docker build cache-from
53-
Compress bool // Docker build compress
54-
Repo string // Docker build repository
55-
LabelSchema []string // label-schema Label map
56-
Labels []string // Label map
57-
NoCache bool // Docker build no-cache
58-
AddHost []string // Docker build add-host
59-
Quiet bool // Docker build quiet
42+
Remote string // Git remote URL
43+
Name string // Docker build using default named tag
44+
Dockerfile string // Docker build Dockerfile
45+
Context string // Docker build context
46+
Tags []string // Docker build tags
47+
Args []string // Docker build args
48+
ArgsEnv []string // Docker build args from env
49+
Target string // Docker build target
50+
Squash bool // Docker build squash
51+
Pull bool // Docker build pull
52+
CacheFrom []string // Docker build cache-from
53+
Compress bool // Docker build compress
54+
Repo string // Docker build repository
55+
LabelSchema []string // label-schema Label map
56+
AutoLabel bool // auto-label bool
57+
Labels []string // Label map
58+
Link string // Git repo link
59+
NoCache bool // Docker build no-cache
60+
AddHost []string // Docker build add-host
61+
Quiet bool // Docker build quiet
6062
}
6163

6264
// Plugin defines the Docker plugin parameters.
@@ -252,19 +254,22 @@ func commandBuild(build Build) *exec.Cmd {
252254
args = append(args, "--quiet")
253255
}
254256

255-
labelSchema := []string{
256-
"schema-version=1.0",
257-
fmt.Sprintf("build-date=%s", time.Now().Format(time.RFC3339)),
258-
fmt.Sprintf("vcs-ref=%s", build.Name),
259-
fmt.Sprintf("vcs-url=%s", build.Remote),
260-
}
257+
if build.AutoLabel {
258+
labelSchema := []string{
259+
fmt.Sprintf("created=%s", time.Now().Format(time.RFC3339)),
260+
fmt.Sprintf("revision=%s", build.Name),
261+
fmt.Sprintf("source=%s", build.Remote),
262+
fmt.Sprintf("url=%s", build.Link),
263+
}
264+
labelPrefix := "org.opencontainers.image"
261265

262-
if len(build.LabelSchema) > 0 {
263-
labelSchema = append(labelSchema, build.LabelSchema...)
264-
}
266+
if len(build.LabelSchema) > 0 {
267+
labelSchema = append(labelSchema, build.LabelSchema...)
268+
}
265269

266-
for _, label := range labelSchema {
267-
args = append(args, "--label", fmt.Sprintf("org.label-schema.%s", label))
270+
for _, label := range labelSchema {
271+
args = append(args, "--label", fmt.Sprintf("%s.%s", labelPrefix, label))
272+
}
268273
}
269274

270275
if len(build.Labels) > 0 {

0 commit comments

Comments
 (0)