Skip to content

Commit

Permalink
fix: ARG/ENV used in script does not invalidate build cache (#1688) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
madawei2699 authored Dec 30, 2021
1 parent e62c80e commit ee2249b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
8 changes: 8 additions & 0 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ func (s *stageBuilder) populateCompositeKey(command fmt.Stringer, files []string
if err != nil {
return compositeKey, err
}
// Use the special argument "|#" at the start of the args array. This will
// avoid conflicts with any RUN command since commands can not
// start with | (vertical bar). The "#" (number of build envs) is there to
// help ensure proper cache matches.
if len(replacementEnvs) > 0 {
compositeKey.AddKey(fmt.Sprintf("|%d", len(replacementEnvs)))
compositeKey.AddKey(replacementEnvs...)
}
// Add the next command to the cache key.
compositeKey.AddKey(resolvedCmd)
if copyCmd, ok := commands.CastAbstractCopyCommand(command); ok == true {
Expand Down
60 changes: 47 additions & 13 deletions pkg/executor/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,35 +597,35 @@ func Test_stageBuilder_populateCompositeKey(t *testing.T) {
shdEqual bool
}{
{
description: "cache key for same command, different buildargs, args not used in command",
description: "cache key for same command with same build args",
cmd1: newStageContext(
"RUN echo const > test",
"RUN echo $ARG > test",
map[string]string{"ARG": "foo"},
[]string{"ENV=foo1"},
[]string{},
),
cmd2: newStageContext(
"RUN echo const > test",
map[string]string{"ARG": "bar"},
[]string{"ENV=bar1"},
"RUN echo $ARG > test",
map[string]string{"ARG": "foo"},
[]string{},
),
shdEqual: true,
},
{
description: "cache key for same command with same build args",
description: "cache key for same command with same env and args",
cmd1: newStageContext(
"RUN echo $ARG > test",
"RUN echo $ENV > test",
map[string]string{"ARG": "foo"},
[]string{},
[]string{"ENV=same"},
),
cmd2: newStageContext(
"RUN echo $ARG > test",
"RUN echo $ENV > test",
map[string]string{"ARG": "foo"},
[]string{},
[]string{"ENV=same"},
),
shdEqual: true,
},
{
description: "cache key for same command with same env",
description: "cache key for same command with same env but different args",
cmd1: newStageContext(
"RUN echo $ENV > test",
map[string]string{"ARG": "foo"},
Expand All @@ -636,7 +636,35 @@ func Test_stageBuilder_populateCompositeKey(t *testing.T) {
map[string]string{"ARG": "bar"},
[]string{"ENV=same"},
),
shdEqual: true,
},
{
description: "cache key for same command, different buildargs, args not used in command",
cmd1: newStageContext(
"RUN echo const > test",
map[string]string{"ARG": "foo"},
[]string{"ENV=foo1"},
),
cmd2: newStageContext(
"RUN echo const > test",
map[string]string{"ARG": "bar"},
[]string{"ENV=bar1"},
),
},
{
description: "cache key for same command, different buildargs, args used in script",
// test.sh
// #!/bin/sh
// echo ${ARG}
cmd1: newStageContext(
"RUN ./test.sh",
map[string]string{"ARG": "foo"},
[]string{"ENV=foo1"},
),
cmd2: newStageContext(
"RUN ./test.sh",
map[string]string{"ARG": "bar"},
[]string{"ENV=bar1"},
),
},
{
description: "cache key for same command with a build arg values",
Expand Down Expand Up @@ -1116,6 +1144,8 @@ RUN foobar
func() testcase {
dir, _ := tempDirAndFile(t)
ch := NewCompositeCache("")
ch.AddKey("|1")
ch.AddKey("test=value")
ch.AddKey("RUN foobar")
hash, err := ch.Hash()
if err != nil {
Expand Down Expand Up @@ -1151,6 +1181,8 @@ RUN foobar
dir, _ := tempDirAndFile(t)

ch := NewCompositeCache("")
ch.AddKey("|1")
ch.AddKey("arg=value")
ch.AddKey("RUN value")
hash, err := ch.Hash()
if err != nil {
Expand Down Expand Up @@ -1193,6 +1225,8 @@ RUN foobar
}

ch2 := NewCompositeCache("")
ch2.AddKey("|1")
ch2.AddKey("arg=anotherValue")
ch2.AddKey("RUN anotherValue")
hash2, err := ch2.Hash()
if err != nil {
Expand Down

0 comments on commit ee2249b

Please sign in to comment.