Compare commits

...

2 Commits

Author SHA1 Message Date
sahithiharness 68605049b7 fix 2024-11-28 13:31:19 +05:30
sahithiharness e2e30f3015 fix:[CI-14845]: support for build args which has comma seperated values 2024-11-26 21:28:32 +05:30
3 changed files with 124 additions and 55 deletions
+33
View File
@@ -0,0 +1,33 @@
package main
import (
"strings"
)
// CustomStringSliceFlag is like a regular StringSlice flag but with
// semicolon as a delimiter
type CustomStringSliceFlag struct {
Value []string
}
func (f *CustomStringSliceFlag) GetValue() []string {
if f.Value == nil {
return make([]string, 0)
}
return f.Value
}
func (f *CustomStringSliceFlag) String() string {
if f.Value == nil {
return ""
}
return strings.Join(f.Value, ";")
}
func (f *CustomStringSliceFlag) Set(v string) error {
for _, s := range strings.Split(v, ";") {
s = strings.TrimSpace(s)
f.Value = append(f.Value, s)
}
return nil
}
+39 -26
View File
@@ -151,6 +151,17 @@ func main() {
Usage: "build args", Usage: "build args",
EnvVar: "PLUGIN_BUILD_ARGS_FROM_ENV", EnvVar: "PLUGIN_BUILD_ARGS_FROM_ENV",
}, },
cli.GenericFlag{
Name: "args-new",
Usage: "build args new",
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
Value: new(CustomStringSliceFlag),
},
cli.BoolFlag{
Name: "plugin-multiple-build-agrs",
Usage: "plugin multiple build agrs",
EnvVar: "PLUGIN_MULTIPLE_BUILD_ARGS",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "quiet", Name: "quiet",
Usage: "quiet docker build", Usage: "quiet docker build",
@@ -339,32 +350,34 @@ func run(c *cli.Context) error {
CardPath: c.String("drone-card-path"), CardPath: c.String("drone-card-path"),
ArtifactFile: c.String("artifact-file"), ArtifactFile: c.String("artifact-file"),
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"),
TempTag: generateTempTag(), TempTag: generateTempTag(),
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"), ArgsNew: c.Generic("args-new").(*CustomStringSliceFlag).GetValue(),
Squash: c.Bool("squash"), IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),
Pull: c.BoolT("pull-image"), Target: c.String("target"),
CacheFrom: c.StringSlice("cache-from"), Squash: c.Bool("squash"),
Compress: c.Bool("compress"), Pull: c.BoolT("pull-image"),
Repo: c.String("repo"), CacheFrom: c.StringSlice("cache-from"),
Labels: c.StringSlice("custom-labels"), Compress: c.Bool("compress"),
LabelSchema: c.StringSlice("label-schema"), Repo: c.String("repo"),
AutoLabel: c.BoolT("auto-label"), Labels: c.StringSlice("custom-labels"),
Link: c.String("link"), LabelSchema: c.StringSlice("label-schema"),
NoCache: c.Bool("no-cache"), AutoLabel: c.BoolT("auto-label"),
Secret: c.String("secret"), Link: c.String("link"),
SecretEnvs: c.StringSlice("secrets-from-env"), NoCache: c.Bool("no-cache"),
SecretFiles: c.StringSlice("secrets-from-file"), Secret: c.String("secret"),
AddHost: c.StringSlice("add-host"), SecretEnvs: c.StringSlice("secrets-from-env"),
Quiet: c.Bool("quiet"), SecretFiles: c.StringSlice("secrets-from-file"),
Platform: c.String("platform"), AddHost: c.StringSlice("add-host"),
SSHAgentKey: c.String("ssh-agent-key"), Quiet: c.Bool("quiet"),
Platform: c.String("platform"),
SSHAgentKey: c.String("ssh-agent-key"),
}, },
Daemon: docker.Daemon{ Daemon: docker.Daemon{
Registry: c.String("docker.registry"), Registry: c.String("docker.registry"),
+52 -29
View File
@@ -45,33 +45,35 @@ 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
TempTag string // Temporary tag used during docker build TempTag string // Temporary tag used during docker build
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 ArgsNew []string // docker build args which has comma seperated values
Squash bool // Docker build squash IsMultipleBuildArgs bool // env variable for fall back to old build args
Pull bool // Docker build pull Target string // Docker build target
CacheFrom []string // Docker build cache-from Squash bool // Docker build squash
Compress bool // Docker build compress Pull bool // Docker build pull
Repo string // Docker build repository CacheFrom []string // Docker build cache-from
LabelSchema []string // label-schema Label map Compress bool // Docker build compress
AutoLabel bool // auto-label bool Repo string // Docker build repository
Labels []string // Label map LabelSchema []string // label-schema Label map
Link string // Git repo link AutoLabel bool // auto-label bool
NoCache bool // Docker build no-cache Labels []string // Label map
Secret string // secret keypair Link string // Git repo link
SecretEnvs []string // Docker build secrets with env var as source NoCache bool // Docker build no-cache
SecretFiles []string // Docker build secrets with file as source Secret string // secret keypair
AddHost []string // Docker build add-host SecretEnvs []string // Docker build secrets with env var as source
Quiet bool // Docker build quiet SecretFiles []string // Docker build secrets with file as source
Platform string // Docker build platform AddHost []string // Docker build add-host
SSHAgentKey string // Docker build ssh agent key Quiet bool // Docker build quiet
SSHKeyPath string // Docker build ssh key path Platform string // Docker build platform
SSHAgentKey string // Docker build ssh agent key
SSHKeyPath string // Docker build ssh key path
} }
// Plugin defines the Docker plugin parameters. // Plugin defines the Docker plugin parameters.
@@ -413,8 +415,14 @@ func commandBuild(build Build) *exec.Cmd {
for _, arg := range build.ArgsEnv { for _, arg := range build.ArgsEnv {
addProxyValue(&build, arg) addProxyValue(&build, arg)
} }
for _, arg := range build.Args { if build.IsMultipleBuildArgs {
args = append(args, "--build-arg", arg) for _, arg := range build.ArgsNew {
args = append(args, "--build-arg", arg)
}
} else {
for _, arg := range build.Args {
args = append(args, "--build-arg", arg)
}
} }
for _, host := range build.AddHost { for _, host := range build.AddHost {
args = append(args, "--add-host", host) args = append(args, "--add-host", host)
@@ -519,6 +527,10 @@ func addProxyValue(build *Build, key string) {
build.Args = append(build.Args, fmt.Sprintf("%s=%s", key, value)) build.Args = append(build.Args, fmt.Sprintf("%s=%s", key, value))
build.Args = append(build.Args, fmt.Sprintf("%s=%s", strings.ToUpper(key), value)) build.Args = append(build.Args, fmt.Sprintf("%s=%s", strings.ToUpper(key), value))
} }
if len(value) > 0 && !hasProxyBuildArgNew(build, key) {
build.ArgsNew = append(build.ArgsNew, fmt.Sprintf("%s=%s", key, value))
build.ArgsNew = append(build.ArgsNew, fmt.Sprintf("%s=%s", strings.ToUpper(key), value))
}
} }
// helper function to get a proxy value from the environment. // helper function to get a proxy value from the environment.
@@ -546,6 +558,17 @@ func hasProxyBuildArg(build *Build, key string) bool {
return false return false
} }
func hasProxyBuildArgNew(build *Build, key string) bool {
keyUpper := strings.ToUpper(key)
for _, s := range build.ArgsNew {
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
return true
}
}
return false
}
// helper function to create the docker tag command. // helper function to create the docker tag command.
func commandTag(build Build, tag string) *exec.Cmd { func commandTag(build Build, tag string) *exec.Cmd {