Skip to content

Commit

Permalink
Restore build args after optimize. Fixes #1910, #1912. (#1915)
Browse files Browse the repository at this point in the history
* Restore build args after optimize. Fixes #1910, #1912.

* Apply review suggestions.
  • Loading branch information
apollo13 authored Feb 9, 2022
1 parent 0adbbee commit ef97636
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/dockerfile/buildargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ef97636

Please sign in to comment.