mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 00:43:30 +02:00
Expand build argument from environment when no value specified
Fixes #713
This commit is contained in:
@@ -55,6 +55,7 @@ var RootCmd = &cobra.Command{
|
||||
Use: "executor",
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if cmd.Use == "executor" {
|
||||
resolveEnvironmentBuildArgs(opts.BuildArgs, os.Getenv)
|
||||
if err := util.ConfigureLogging(logLevel); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -200,6 +201,17 @@ func resolveDockerfilePath() error {
|
||||
return errors.New("please provide a valid path to a Dockerfile within the build context with --dockerfile")
|
||||
}
|
||||
|
||||
// resolveEnvironmentBuildArgs replace build args without value by the same named environment variable
|
||||
func resolveEnvironmentBuildArgs(arguments []string, resolver func(string) string) {
|
||||
for index, argument := range arguments {
|
||||
i := strings.Index(argument, "=")
|
||||
if i < 0 {
|
||||
value := resolver(argument)
|
||||
arguments[index] = fmt.Sprintf("%s=%s", argument, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// copy Dockerfile to /kaniko/Dockerfile so that if it's specified in the .dockerignore
|
||||
// it won't be copied into the image
|
||||
func copyDockerfile() error {
|
||||
|
||||
@@ -97,3 +97,55 @@ func TestIsUrl(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveEnvironmentBuildArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input []string
|
||||
expected []string
|
||||
mockedEnvironmentResolver func(string) string
|
||||
}{
|
||||
{
|
||||
description: "replace when environment variable is present and value is not specified",
|
||||
input: []string{"variable1"},
|
||||
expected: []string{"variable1=value1"},
|
||||
mockedEnvironmentResolver: func(variable string) string {
|
||||
if variable == "variable1" {
|
||||
return "value1"
|
||||
}
|
||||
return ""
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "do not replace when environment variable is present and value is specified",
|
||||
input: []string{"variable1=value1", "variable2=value2"},
|
||||
expected: []string{"variable1=value1", "variable2=value2"},
|
||||
mockedEnvironmentResolver: func(variable string) string {
|
||||
return "unexpected"
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "do not replace when environment variable is present and empty value is specified",
|
||||
input: []string{"variable1="},
|
||||
expected: []string{"variable1="},
|
||||
mockedEnvironmentResolver: func(variable string) string {
|
||||
return "unexpected"
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "replace with empty value when environment variable is not present or empty and value is not specified",
|
||||
input: []string{"variable1", "variable2=value2"},
|
||||
expected: []string{"variable1=", "variable2=value2"},
|
||||
mockedEnvironmentResolver: func(variable string) string {
|
||||
return ""
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
resolveEnvironmentBuildArgs(tt.input, tt.mockedEnvironmentResolver)
|
||||
testutil.CheckDeepEqual(t, tt.expected, tt.input)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user