Restore build args after optimize. Fixes #1910, #1912. (#1915)

* Restore build args after optimize. Fixes #1910, #1912.

* Apply review suggestions.
This commit is contained in:
Florian Apolloner 2022-02-09 17:50:40 +01:00 committed by GitHub
parent 0adbbee21d
commit ef97636546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -51,8 +51,11 @@ func (b *BuildArgs) Clone() *BuildArgs {
// ReplacementEnvs returns a list of filtered environment variables
func (b *BuildArgs) ReplacementEnvs(envs []string) []string {
// Ensure that we operate on a new array and do not modify the underlying array
resultEnv := make([]string, len(envs))
copy(resultEnv, envs)
filtered := b.FilterAllowed(envs)
return append(envs, filtered...)
return append(resultEnv, filtered...)
}
// AddMetaArgs adds the supplied args map to b's allowedMetaArgs

View File

@ -21,6 +21,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
@ -181,6 +182,9 @@ func initConfig(img partial.WithConfigFile, opts *config.KanikoOptions) (*v1.Con
func (s *stageBuilder) populateCompositeKey(command fmt.Stringer, files []string, compositeKey CompositeCache, args *dockerfile.BuildArgs, env []string) (CompositeCache, error) {
// First replace all the environment variables or args in the command
replacementEnvs := args.ReplacementEnvs(env)
// The sort order of `replacementEnvs` is basically undefined, sort it
// so we can ensure a stable cache key.
sort.Strings(replacementEnvs)
resolvedCmd, err := util.ResolveEnvironmentReplacement(command.String(), replacementEnvs, false)
if err != nil {
return compositeKey, err
@ -227,6 +231,11 @@ func (s *stageBuilder) optimize(compositeKey CompositeCache, cfg v1.Config) erro
if !s.opts.Cache {
return nil
}
var buildArgs = s.args.Clone()
// Restore build args back to their original values
defer func() {
s.args = buildArgs
}()
stopCache := false
// Possibly replace commands with their cached implementations.