Skip to content

Commit 86de147

Browse files
authored
Merge pull request drone-plugins#462 from drone-plugins/CI-14845
fix:[CI-14845]: support for build args which has comma seperated values
2 parents 4999d4c + 6860504 commit 86de147

File tree

3 files changed

+124
-55
lines changed

3 files changed

+124
-55
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// CustomStringSliceFlag is like a regular StringSlice flag but with
8+
// semicolon as a delimiter
9+
type CustomStringSliceFlag struct {
10+
Value []string
11+
}
12+
13+
func (f *CustomStringSliceFlag) GetValue() []string {
14+
if f.Value == nil {
15+
return make([]string, 0)
16+
}
17+
return f.Value
18+
}
19+
20+
func (f *CustomStringSliceFlag) String() string {
21+
if f.Value == nil {
22+
return ""
23+
}
24+
return strings.Join(f.Value, ";")
25+
}
26+
27+
func (f *CustomStringSliceFlag) Set(v string) error {
28+
for _, s := range strings.Split(v, ";") {
29+
s = strings.TrimSpace(s)
30+
f.Value = append(f.Value, s)
31+
}
32+
return nil
33+
}

cmd/drone-docker/main.go

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ func main() {
151151
Usage: "build args",
152152
EnvVar: "PLUGIN_BUILD_ARGS_FROM_ENV",
153153
},
154+
cli.GenericFlag{
155+
Name: "args-new",
156+
Usage: "build args new",
157+
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
158+
Value: new(CustomStringSliceFlag),
159+
},
160+
cli.BoolFlag{
161+
Name: "plugin-multiple-build-agrs",
162+
Usage: "plugin multiple build agrs",
163+
EnvVar: "PLUGIN_MULTIPLE_BUILD_ARGS",
164+
},
154165
cli.BoolFlag{
155166
Name: "quiet",
156167
Usage: "quiet docker build",
@@ -339,32 +350,34 @@ func run(c *cli.Context) error {
339350
CardPath: c.String("drone-card-path"),
340351
ArtifactFile: c.String("artifact-file"),
341352
Build: docker.Build{
342-
Remote: c.String("remote.url"),
343-
Name: c.String("commit.sha"),
344-
TempTag: generateTempTag(),
345-
Dockerfile: c.String("dockerfile"),
346-
Context: c.String("context"),
347-
Tags: c.StringSlice("tags"),
348-
Args: c.StringSlice("args"),
349-
ArgsEnv: c.StringSlice("args-from-env"),
350-
Target: c.String("target"),
351-
Squash: c.Bool("squash"),
352-
Pull: c.BoolT("pull-image"),
353-
CacheFrom: c.StringSlice("cache-from"),
354-
Compress: c.Bool("compress"),
355-
Repo: c.String("repo"),
356-
Labels: c.StringSlice("custom-labels"),
357-
LabelSchema: c.StringSlice("label-schema"),
358-
AutoLabel: c.BoolT("auto-label"),
359-
Link: c.String("link"),
360-
NoCache: c.Bool("no-cache"),
361-
Secret: c.String("secret"),
362-
SecretEnvs: c.StringSlice("secrets-from-env"),
363-
SecretFiles: c.StringSlice("secrets-from-file"),
364-
AddHost: c.StringSlice("add-host"),
365-
Quiet: c.Bool("quiet"),
366-
Platform: c.String("platform"),
367-
SSHAgentKey: c.String("ssh-agent-key"),
353+
Remote: c.String("remote.url"),
354+
Name: c.String("commit.sha"),
355+
TempTag: generateTempTag(),
356+
Dockerfile: c.String("dockerfile"),
357+
Context: c.String("context"),
358+
Tags: c.StringSlice("tags"),
359+
Args: c.StringSlice("args"),
360+
ArgsEnv: c.StringSlice("args-from-env"),
361+
ArgsNew: c.Generic("args-new").(*CustomStringSliceFlag).GetValue(),
362+
IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),
363+
Target: c.String("target"),
364+
Squash: c.Bool("squash"),
365+
Pull: c.BoolT("pull-image"),
366+
CacheFrom: c.StringSlice("cache-from"),
367+
Compress: c.Bool("compress"),
368+
Repo: c.String("repo"),
369+
Labels: c.StringSlice("custom-labels"),
370+
LabelSchema: c.StringSlice("label-schema"),
371+
AutoLabel: c.BoolT("auto-label"),
372+
Link: c.String("link"),
373+
NoCache: c.Bool("no-cache"),
374+
Secret: c.String("secret"),
375+
SecretEnvs: c.StringSlice("secrets-from-env"),
376+
SecretFiles: c.StringSlice("secrets-from-file"),
377+
AddHost: c.StringSlice("add-host"),
378+
Quiet: c.Bool("quiet"),
379+
Platform: c.String("platform"),
380+
SSHAgentKey: c.String("ssh-agent-key"),
368381
},
369382
Daemon: docker.Daemon{
370383
Registry: c.String("docker.registry"),

docker.go

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,35 @@ type (
4545

4646
// Build defines Docker build parameters.
4747
Build struct {
48-
Remote string // Git remote URL
49-
Name string // Docker build using default named tag
50-
TempTag string // Temporary tag used during docker build
51-
Dockerfile string // Docker build Dockerfile
52-
Context string // Docker build context
53-
Tags []string // Docker build tags
54-
Args []string // Docker build args
55-
ArgsEnv []string // Docker build args from env
56-
Target string // Docker build target
57-
Squash bool // Docker build squash
58-
Pull bool // Docker build pull
59-
CacheFrom []string // Docker build cache-from
60-
Compress bool // Docker build compress
61-
Repo string // Docker build repository
62-
LabelSchema []string // label-schema Label map
63-
AutoLabel bool // auto-label bool
64-
Labels []string // Label map
65-
Link string // Git repo link
66-
NoCache bool // Docker build no-cache
67-
Secret string // secret keypair
68-
SecretEnvs []string // Docker build secrets with env var as source
69-
SecretFiles []string // Docker build secrets with file as source
70-
AddHost []string // Docker build add-host
71-
Quiet bool // Docker build quiet
72-
Platform string // Docker build platform
73-
SSHAgentKey string // Docker build ssh agent key
74-
SSHKeyPath string // Docker build ssh key path
48+
Remote string // Git remote URL
49+
Name string // Docker build using default named tag
50+
TempTag string // Temporary tag used during docker build
51+
Dockerfile string // Docker build Dockerfile
52+
Context string // Docker build context
53+
Tags []string // Docker build tags
54+
Args []string // Docker build args
55+
ArgsEnv []string // Docker build args from env
56+
ArgsNew []string // docker build args which has comma seperated values
57+
IsMultipleBuildArgs bool // env variable for fall back to old build args
58+
Target string // Docker build target
59+
Squash bool // Docker build squash
60+
Pull bool // Docker build pull
61+
CacheFrom []string // Docker build cache-from
62+
Compress bool // Docker build compress
63+
Repo string // Docker build repository
64+
LabelSchema []string // label-schema Label map
65+
AutoLabel bool // auto-label bool
66+
Labels []string // Label map
67+
Link string // Git repo link
68+
NoCache bool // Docker build no-cache
69+
Secret string // secret keypair
70+
SecretEnvs []string // Docker build secrets with env var as source
71+
SecretFiles []string // Docker build secrets with file as source
72+
AddHost []string // Docker build add-host
73+
Quiet bool // Docker build quiet
74+
Platform string // Docker build platform
75+
SSHAgentKey string // Docker build ssh agent key
76+
SSHKeyPath string // Docker build ssh key path
7577
}
7678

7779
// Plugin defines the Docker plugin parameters.
@@ -413,8 +415,14 @@ func commandBuild(build Build) *exec.Cmd {
413415
for _, arg := range build.ArgsEnv {
414416
addProxyValue(&build, arg)
415417
}
416-
for _, arg := range build.Args {
417-
args = append(args, "--build-arg", arg)
418+
if build.IsMultipleBuildArgs {
419+
for _, arg := range build.ArgsNew {
420+
args = append(args, "--build-arg", arg)
421+
}
422+
} else {
423+
for _, arg := range build.Args {
424+
args = append(args, "--build-arg", arg)
425+
}
418426
}
419427
for _, host := range build.AddHost {
420428
args = append(args, "--add-host", host)
@@ -519,6 +527,10 @@ func addProxyValue(build *Build, key string) {
519527
build.Args = append(build.Args, fmt.Sprintf("%s=%s", key, value))
520528
build.Args = append(build.Args, fmt.Sprintf("%s=%s", strings.ToUpper(key), value))
521529
}
530+
if len(value) > 0 && !hasProxyBuildArgNew(build, key) {
531+
build.ArgsNew = append(build.ArgsNew, fmt.Sprintf("%s=%s", key, value))
532+
build.ArgsNew = append(build.ArgsNew, fmt.Sprintf("%s=%s", strings.ToUpper(key), value))
533+
}
522534
}
523535

524536
// helper function to get a proxy value from the environment.
@@ -546,6 +558,17 @@ func hasProxyBuildArg(build *Build, key string) bool {
546558

547559
return false
548560
}
561+
func hasProxyBuildArgNew(build *Build, key string) bool {
562+
keyUpper := strings.ToUpper(key)
563+
564+
for _, s := range build.ArgsNew {
565+
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
566+
return true
567+
}
568+
}
569+
570+
return false
571+
}
549572

550573
// helper function to create the docker tag command.
551574
func commandTag(build Build, tag string) *exec.Cmd {

0 commit comments

Comments
 (0)