Skip to content

Commit b707b02

Browse files
yrodieregsmet
authored andcommitted
Improve build cache strategy
Ripped off from Quarkus. Here's how it will work: 1. We create a cache entry on push only. Pull requests only restore it. 2. We create a new cache entry every day, prefixed with something like 2024-10-25. 3. When restoring the cache, we try the entry for the day first (2024-10-25) and default to the one for the month (2024-10-*). Critically, this means we will build each day's cache based on the previous day's cache. 4. Atlas infra uses its own, separate cache entries. Co-Authored-By: Guillaume Smet <[email protected]> (cherry picked from commit 5eaaff2)
1 parent dcabe10 commit b707b02

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

.github/workflows/atlas.yml

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,50 @@ jobs:
5353
with:
5454
distribution: 'graalvm'
5555
java-version: '21'
56-
- name: Get year/month for cache key
57-
id: get-date
58-
run: echo "yearmonth=$(/bin/date -u "+%Y-%m")" >> $GITHUB_OUTPUT
59-
shell: bash
60-
- name: Cache Maven local repository
61-
uses: actions/cache@v3
56+
57+
- name: Generate cache key
58+
id: cache-key
59+
run: |
60+
CURRENT_BRANCH="${{ github.repository != 'hibernate/hibernate-orm' && 'fork' || github.base_ref || github.ref_name }}"
61+
CURRENT_MONTH=$(/bin/date -u "+%Y-%m")
62+
CURRENT_DAY=$(/bin/date -u "+%d")
63+
ROOT_CACHE_KEY="buildtool-cache-atlas"
64+
echo "buildtool-monthly-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}" >> $GITHUB_OUTPUT
65+
echo "buildtool-monthly-branch-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}-${CURRENT_BRANCH}" >> $GITHUB_OUTPUT
66+
echo "buildtool-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}-${CURRENT_BRANCH}-${CURRENT_DAY}" >> $GITHUB_OUTPUT
67+
- name: Cache Maven/Gradle Local Caches
6268
id: cache-maven
69+
uses: actions/cache@v4
70+
# if it's not a pull request, we restore and save the cache
71+
if: github.event_name != 'pull_request'
6372
with:
6473
path: |
65-
~/.m2/repository
74+
~/.m2/repository/
75+
~/.m2/wrapper/
6676
~/.gradle/caches/
6777
~/.gradle/wrapper/
68-
# refresh cache every month to avoid unlimited growth
69-
key: maven-localrepo-${{ steps.get-date.outputs.yearmonth }}
78+
# A new cache will be stored daily. After that first store of the day, cache save actions will fail because the cache is immutable but it's not a problem.
79+
# The whole cache is dropped monthly to prevent unlimited growth.
80+
# The cache is per branch but in case we don't find a branch for a given branch, we will get a cache from another branch.
81+
key: ${{ steps.cache-key.outputs.buildtool-cache-key }}
82+
restore-keys: |
83+
${{ steps.cache-key.outputs.buildtool-monthly-branch-cache-key }}-
84+
${{ steps.cache-key.outputs.buildtool-monthly-cache-key }}-
85+
- name: Restore Maven/Gradle Local Caches
86+
uses: actions/cache/restore@v3
87+
# if it a pull request, we restore the cache but we don't save it
88+
if: github.event_name == 'pull_request'
89+
with:
90+
path: |
91+
~/.m2/repository/
92+
~/.m2/wrapper/
93+
~/.gradle/caches/
94+
~/.gradle/wrapper/
95+
key: ${{ steps.cache-key.outputs.buildtool-cache-key }}
96+
restore-keys: |
97+
${{ steps.cache-key.outputs.buildtool-monthly-branch-cache-key }}-
98+
${{ steps.cache-key.outputs.buildtool-monthly-cache-key }}-
99+
70100
- name: Run build script
71101
env:
72102
RDBMS: ${{ matrix.rdbms }}

.github/workflows/contributor-build.yml

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,50 @@ jobs:
6262
with:
6363
distribution: 'temurin'
6464
java-version: '11'
65-
- name: Get year/month for cache key
66-
id: get-date
67-
run: echo "yearmonth=$(/bin/date -u "+%Y-%m")" >> $GITHUB_OUTPUT
68-
shell: bash
69-
- name: Cache Maven local repository
70-
uses: actions/cache@v3
65+
66+
- name: Generate cache key
67+
id: cache-key
68+
run: |
69+
CURRENT_BRANCH="${{ github.repository != 'hibernate/hibernate-orm' && 'fork' || github.base_ref || github.ref_name }}"
70+
CURRENT_MONTH=$(/bin/date -u "+%Y-%m")
71+
CURRENT_DAY=$(/bin/date -u "+%d")
72+
ROOT_CACHE_KEY="buildtool-cache"
73+
echo "buildtool-monthly-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}" >> $GITHUB_OUTPUT
74+
echo "buildtool-monthly-branch-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}-${CURRENT_BRANCH}" >> $GITHUB_OUTPUT
75+
echo "buildtool-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}-${CURRENT_BRANCH}-${CURRENT_DAY}" >> $GITHUB_OUTPUT
76+
- name: Cache Maven/Gradle Local Caches
7177
id: cache-maven
78+
uses: actions/cache@v4
79+
# if it's not a pull request, we restore and save the cache
80+
if: github.event_name != 'pull_request'
7281
with:
7382
path: |
74-
~/.m2/repository
83+
~/.m2/repository/
84+
~/.m2/wrapper/
7585
~/.gradle/caches/
7686
~/.gradle/wrapper/
77-
# refresh cache every month to avoid unlimited growth
78-
key: maven-localrepo-${{ steps.get-date.outputs.yearmonth }}
87+
# A new cache will be stored daily. After that first store of the day, cache save actions will fail because the cache is immutable but it's not a problem.
88+
# The whole cache is dropped monthly to prevent unlimited growth.
89+
# The cache is per branch but in case we don't find a branch for a given branch, we will get a cache from another branch.
90+
key: ${{ steps.cache-key.outputs.buildtool-cache-key }}
91+
restore-keys: |
92+
${{ steps.cache-key.outputs.buildtool-monthly-branch-cache-key }}-
93+
${{ steps.cache-key.outputs.buildtool-monthly-cache-key }}-
94+
- name: Restore Maven/Gradle Local Caches
95+
uses: actions/cache/restore@v3
96+
# if it a pull request, we restore the cache but we don't save it
97+
if: github.event_name == 'pull_request'
98+
with:
99+
path: |
100+
~/.m2/repository/
101+
~/.m2/wrapper/
102+
~/.gradle/caches/
103+
~/.gradle/wrapper/
104+
key: ${{ steps.cache-key.outputs.buildtool-cache-key }}
105+
restore-keys: |
106+
${{ steps.cache-key.outputs.buildtool-monthly-branch-cache-key }}-
107+
${{ steps.cache-key.outputs.buildtool-monthly-cache-key }}-
108+
79109
- name: Run build script
80110
env:
81111
RDBMS: ${{ matrix.rdbms }}

0 commit comments

Comments
 (0)