Skip to content

Commit

Permalink
fix: [CI-14845]: support for build args with comma seperated values (#47
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sahithibanda01 authored Nov 29, 2024
1 parent d9549f5 commit d713a71
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 57 deletions.
67 changes: 40 additions & 27 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ func Run() {
Usage: "build args",
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",

This comment has been minimized.

Copy link
@hemanthmantri

hemanthmantri Dec 2, 2024

Typo: args

Usage: "plugin multiple build agrs",
EnvVar: "PLUGIN_MULTIPLE_BUILD_ARGS",
},
cli.BoolFlag{
Name: "quiet",
Usage: "quiet docker build",
Expand Down Expand Up @@ -382,33 +393,35 @@ func run(c *cli.Context) error {
ArtifactFile: c.String("artifact-file"),
CacheMetricsFile: c.String("cache-metrics-file"),
Build: 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.Generic("cache-from").(*CustomStringSliceFlag).GetValue(),
CacheTo: c.Generic("cache-to").(*CustomStringSliceFlag).GetValue(),
Compress: c.Bool("compress"),
Repo: c.String("repo"),
Labels: c.StringSlice("custom-labels"),
LabelSchema: c.StringSlice("label-schema"),
AutoLabel: c.BoolT("auto-label"),
Link: c.String("link"),
NoCache: c.Bool("no-cache"),
Secret: c.String("secret"),
SecretEnvs: c.StringSlice("secrets-from-env"),
SecretFiles: c.StringSlice("secrets-from-file"),
AddHost: c.StringSlice("add-host"),
Quiet: c.Bool("quiet"),
Platform: c.String("platform"),
SSHAgentKey: c.String("ssh-agent-key"),
BuildxLoad: c.Bool("buildx-load"),
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"),
ArgsNew: c.Generic("args-new").(*CustomStringSliceFlag).GetValue(),
IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),

This comment has been minimized.

Copy link
@hemanthmantri

hemanthmantri Dec 2, 2024

args

Target: c.String("target"),
Squash: c.Bool("squash"),
Pull: c.BoolT("pull-image"),
CacheFrom: c.Generic("cache-from").(*CustomStringSliceFlag).GetValue(),
CacheTo: c.Generic("cache-to").(*CustomStringSliceFlag).GetValue(),
Compress: c.Bool("compress"),
Repo: c.String("repo"),
Labels: c.StringSlice("custom-labels"),
LabelSchema: c.StringSlice("label-schema"),
AutoLabel: c.BoolT("auto-label"),
Link: c.String("link"),
NoCache: c.Bool("no-cache"),
Secret: c.String("secret"),
SecretEnvs: c.StringSlice("secrets-from-env"),
SecretFiles: c.StringSlice("secrets-from-file"),
AddHost: c.StringSlice("add-host"),
Quiet: c.Bool("quiet"),
Platform: c.String("platform"),
SSHAgentKey: c.String("ssh-agent-key"),
BuildxLoad: c.Bool("buildx-load"),
},
Daemon: Daemon{
Registry: c.String("docker.registry"),
Expand Down
83 changes: 53 additions & 30 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,36 @@ 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 buildx cache-from
CacheTo []string // Docker buildx cache-to
Compress bool // Docker build compress
Repo string // Docker build repository
LabelSchema []string // label-schema Label map
AutoLabel bool // auto-label bool
Labels []string // Label map
Link string // Git repo link
NoCache bool // Docker build no-cache
Secret string // secret keypair
SecretEnvs []string // Docker build secrets with env var as source
SecretFiles []string // Docker build secrets with file as source
AddHost []string // Docker build add-host
Quiet bool // Docker build quiet
Platform string // Docker build platform
SSHAgentKey string // Docker build ssh agent key
SSHKeyPath string // Docker build ssh key path
BuildxLoad bool // Docker buildx --load
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
ArgsNew []string // Docker build args with comma seperated values
IsMultipleBuildArgs bool // env variable for fall back
Target string // Docker build target
Squash bool // Docker build squash
Pull bool // Docker build pull
CacheFrom []string // Docker buildx cache-from
CacheTo []string // Docker buildx cache-to
Compress bool // Docker build compress
Repo string // Docker build repository
LabelSchema []string // label-schema Label map
AutoLabel bool // auto-label bool
Labels []string // Label map
Link string // Git repo link
NoCache bool // Docker build no-cache
Secret string // secret keypair
SecretEnvs []string // Docker build secrets with env var as source
SecretFiles []string // Docker build secrets with file as source
AddHost []string // Docker build add-host
Quiet bool // Docker build quiet
Platform string // Docker build platform
SSHAgentKey string // Docker build ssh agent key
SSHKeyPath string // Docker build ssh key path
BuildxLoad bool // Docker buildx --load
}

// Plugin defines the Docker plugin parameters.
Expand Down Expand Up @@ -512,8 +514,14 @@ func commandBuildx(build Build, builder Builder, dryrun bool, metadataFile strin
for _, arg := range build.ArgsEnv {
addProxyValue(&build, arg)
}
for _, arg := range build.Args {
args = append(args, "--build-arg", arg)
if build.IsMultipleBuildArgs {
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 {
args = append(args, "--add-host", host)
Expand Down Expand Up @@ -613,6 +621,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", 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.
Expand Down Expand Up @@ -641,6 +653,17 @@ func hasProxyBuildArg(build *Build, key string) bool {
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.
func commandTag(build Build, tag string) *exec.Cmd {
var (
Expand Down

0 comments on commit d713a71

Please sign in to comment.