@@ -184,16 +184,50 @@ jobs:
184184 run : |
185185 echo "::set-output name=yearmonth::$(/bin/date -u "+%Y-%m")"
186186 shell : bash
187- - name : Cache Gradle downloads
188- uses : actions/cache@v2
189- id : cache-gradle
187+
188+ - name : Generate cache key
189+ id : cache-key
190+ run : |
191+ CURRENT_BRANCH="${{ github.repository != 'hibernate/hibernate-orm' && 'fork' || github.base_ref || github.ref_name }}"
192+ CURRENT_MONTH=$(/bin/date -u "+%Y-%m")
193+ CURRENT_DAY=$(/bin/date -u "+%d")
194+ ROOT_CACHE_KEY="buildtool-cache"
195+ echo "buildtool-monthly-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}" >> $GITHUB_OUTPUT
196+ echo "buildtool-monthly-branch-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}-${CURRENT_BRANCH}" >> $GITHUB_OUTPUT
197+ echo "buildtool-cache-key=${ROOT_CACHE_KEY}-${CURRENT_MONTH}-${CURRENT_BRANCH}-${CURRENT_DAY}" >> $GITHUB_OUTPUT
198+ - name : Cache Maven/Gradle Dependency/Dist Caches
199+ id : cache-maven
200+ uses : actions/cache@v4
201+ # if it's not a pull request, we restore and save the cache
202+ if : github.event_name != 'pull_request'
190203 with :
191204 path : |
192- .gradle/caches
193- .gradle/jdks
194- .gradle/wrapper
195- # refresh cache every month to avoid unlimited growth
196- key : gradle-java${{ matrix.java }}-${{ steps.get-date.outputs.yearmonth }}
205+ ~/.m2/repository/
206+ ~/.m2/wrapper/
207+ ~/.gradle/caches/modules-2
208+ ~/.gradle/wrapper/
209+ # 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.
210+ # The whole cache is dropped monthly to prevent unlimited growth.
211+ # 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.
212+ key : ${{ steps.cache-key.outputs.buildtool-cache-key }}
213+ restore-keys : |
214+ ${{ steps.cache-key.outputs.buildtool-monthly-branch-cache-key }}-
215+ ${{ steps.cache-key.outputs.buildtool-monthly-cache-key }}-
216+ - name : Restore Maven/Gradle Dependency/Dist Caches
217+ uses : actions/cache/restore@v4
218+ # if it's a pull request, we restore the cache, but we don't save it
219+ if : github.event_name == 'pull_request'
220+ with :
221+ path : |
222+ ~/.m2/repository/
223+ ~/.m2/wrapper/
224+ ~/.gradle/caches/modules-2
225+ ~/.gradle/wrapper/
226+ key : ${{ steps.cache-key.outputs.buildtool-cache-key }}
227+ restore-keys : |
228+ ${{ steps.cache-key.outputs.buildtool-monthly-branch-cache-key }}-
229+ ${{ steps.cache-key.outputs.buildtool-monthly-cache-key }}-
230+
197231 - name : Set up latest JDK ${{ matrix.java.name }} from jdk.java.net
198232 if : matrix.java.from == 'jdk.java.net'
199233 uses : oracle-actions/setup-java@v1
0 commit comments