Skip to content

Commit

Permalink
Restore build args after optimize. Fixes GoogleContainerTools#1910, G…
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed Feb 8, 2022
1 parent bde9043 commit 1dfda2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/dockerfile/buildargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package dockerfile

import (
"sort"
"strings"

d "github.com/docker/docker/builder/dockerfile"
Expand Down Expand Up @@ -51,8 +52,13 @@ 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...)
resultEnv = append(resultEnv, filtered...)
sort.Strings(resultEnv)
return resultEnv
}

// AddMetaArgs adds the supplied args map to b's allowedMetaArgs
Expand Down
5 changes: 5 additions & 0 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,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 1dfda2b

Please sign in to comment.