Merge pull request #6311 from flouthoc/correctly-burst-cache

build: `--mount=type=bind` ignore `modTime` for cache candidates
This commit is contained in:
openshift-merge-bot[bot] 2025-08-06 19:29:29 +00:00 committed by GitHub
commit 4ac53d21ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/containers/buildah"
digest "github.com/opencontainers/go-digest"
@ -69,6 +70,11 @@ func generatePathChecksum(sourcePath string) (string, error) {
}
header.Name = filepath.ToSlash(relPath)
// Zero out timestamp fields to ignore modification time in checksum calculation
header.ModTime = time.Time{}
header.AccessTime = time.Time{}
header.ChangeTime = time.Time{}
if err := tarWriter.WriteHeader(header); err != nil {
return err
}

View File

@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
"github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
@ -52,6 +53,11 @@ func TestGeneratePathChecksum(t *testing.T) {
}
header.Name = filepath.ToSlash(relPath)
// Zero out timestamp fields to match the modified generatePathChecksum function
header.ModTime = time.Time{}
header.AccessTime = time.Time{}
header.ChangeTime = time.Time{}
if err := tarWriter.WriteHeader(header); err != nil {
return err
}

View File

@ -662,6 +662,38 @@ _EOF
expect_output --substring "Cache burst add diff"
}
@test "bud --layers with --mount type bind should preserve cache when file mod time changes but content stays same" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/samplefile << _EOF
samplefile content unchanged
_EOF
cat > $contextdir/Containerfile << _EOF
FROM alpine
RUN --mount=type=bind,source=samplefile,target=file,Z cat file
_EOF
# on first run since there is no cache so content must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "samplefile content unchanged"
# on second run since there is cache so content should not be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
# output should not contain content from the file since entire build is cached
assert "$output" !~ "samplefile content unchanged"
# Change mod time of this file (this changes modification time)
touch -d "@1577836800" $contextdir/samplefile
# on third run since content is unchanged, cache should still be used despite different mod time
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
# output should not contain content from the file since cache should still be valid
assert "$output" !~ "samplefile content unchanged"
}
@test "bud --layers should not hit cache if heredoc is changed - with ARG" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform