Skip to content

Commit d8602f6

Browse files
authored
CI/Caching: Fix Gradle cache retention (apache#2604)
Older versions of Gradle's setup-gradle action did not actively trigger stale cache entry cleanup, which is why there was a separate step "Trigger Gradle home cleanup" in `gradle.yml`. Nowadays, that action triggers a "noop build" to explicitly trigger stale cache entry cleanup, but uses somewhat different defaults than [described here](https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home:configure_cache_cleanup). This change adds an explicit configuration for Gradle cache cleanup/retention with reasonable values considering the total 10GB limit for all GitHub caches per repository. The change described above lead to a behavioral change, which evicts all non-accessed cache entries within the current GH workflow job, which effectively evicted all cache entries from dependent jobs - in other words: the (build) cache was nearly empty, leading to full rebuilds. This behavior could be observed in the output of the "Post Collect partial Gradle build caches"-step in the log message "Build cache (/home/runner/.gradle/caches/build-cache-1) removing files not accessed on or after ..." showing the timestamp when the setup-gradle action was started.
1 parent f9a2165 commit d8602f6

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

.github/actions/ci-incr-build-cache-prepare/action.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ inputs:
3232
runs:
3333
using: "composite"
3434
steps:
35+
- name: Configure Gradle cache retention settings
36+
# Without these settings, the Gradle Setup action will evict effectively all "restored incremental" entries,
37+
# as it will remove all cache entries created before the start-time of the "Store Gradle Cache" job.
38+
# The total size of GitHub caches for each repository is limited to 10GB, which means that we have to use
39+
# more aggressive retention settings to keep the size of the Gradle cache stored in GitHub at an appropriate
40+
# size.
41+
shell: bash
42+
run: |
43+
mkdir -p ~/.gradle/init.d
44+
cat > ~/.gradle/init.d/cache-settings.gradle.kts <<!
45+
beforeSettings {
46+
caches {
47+
releasedWrappers.setRemoveUnusedEntriesAfterDays(5)
48+
snapshotWrappers.setRemoveUnusedEntriesAfterDays(1)
49+
downloadedResources.setRemoveUnusedEntriesAfterDays(2)
50+
createdResources.setRemoveUnusedEntriesAfterDays(5)
51+
buildCache.setRemoveUnusedEntriesAfterDays(5)
52+
}
53+
}
54+
!
55+
3556
- name: Prep env
3657
shell: bash
3758
run: |
@@ -71,12 +92,12 @@ runs:
7192
if [[ -d ~/downloaded-artifacts/ ]] ; then
7293
find ~/downloaded-artifacts/ -type f -name "ci-gradle-caches-*-${{ inputs.java-version }}.tar" | while read arch ; do
7394
echo "Adding archive content from $arch ..."
74-
(cd ~/.gradle/caches/ ; tar xf $arch)
95+
(cd ~/.gradle/caches/ ; tar xf $arch --atime-preserve)
7596
done
7697
else
7798
echo "No previous build cache artifacts downloaded."
7899
fi
79-
100+
80101
date +%s > ~/caches-prepared-at-epoch
81102
82103
echo "::endgroup::"

.github/actions/ci-incr-build-cache-save/action.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ runs:
4747
-path './[0-9]*/kotlin-dsl/*' -or \
4848
-path './jars-*/*' -or \
4949
-path './modules-*/files-*/*' -or \
50-
-path './modules-*/files-*/*' -or \
5150
-path './build-cache-*/*' \
5251
')' | grep -v '[.]lock$' > ~/ci-gradle-caches-diff || true
5352
echo "Identified $(wc -l < ~/ci-gradle-caches-diff) changed/added files in caches/"

.github/workflows/gradle.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ jobs:
160160
uses: ./.github/actions/ci-incr-build-cache-prepare
161161
with:
162162
cache-read-only: false
163-
- name: Trigger Gradle home cleanup
164-
env:
165-
DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
166-
run: ./gradlew --no-daemon :showVersion
167163

168-
# Note: the "Post Gradle invocation" archives the updated build cache.
164+
# Note: the "Post Collect partial Gradle build caches"-step cleans up stale cache entries
165+
# and archives the updated build cache.

0 commit comments

Comments
 (0)