Expand build argument from environment when no value specified

Fixes #713
This commit is contained in:
Ben Einaudi
2020-01-30 12:01:06 +01:00
parent 8bdcb4f544
commit 2f1e54e591
4 changed files with 129 additions and 7 deletions
+12
View File
@@ -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 {
+52
View File
@@ -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)
})
}
}