Skip to content

Commit dee34eb

Browse files
committed
ci: optimize GHA caching in ci.yaml using prime-maven-cache composite action
1 parent 7f6e7e0 commit dee34eb

2 files changed

Lines changed: 52 additions & 41 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: 'Prime Maven Cache'
2+
description: 'Pre-populates Maven dependencies into ~/.m2/repository and GitHub Actions Cache'
3+
4+
runs:
5+
using: 'composite'
6+
steps:
7+
# JDK version does not impact priming or matching the Maven cache, as cache keys are derived
8+
# from ${{ runner.os }} and SHA-256 hashes of pom.xml files, not the JDK version.
9+
# We default to the latest LTS version (25) to run the pre-population step.
10+
# Furthermore, GitHub Actions Cache entries are immutable by primary key: downstream jobs
11+
# restoring this cache can run 'mvn install' or add SNAPSHOT JARs locally without ever
12+
# polluting or overwriting the remote cache.
13+
- uses: actions/setup-java@c1e323688fd81a25caa38c78aa6df2d33d3e20d9 # v4
14+
id: setup-java
15+
with:
16+
java-version: 25
17+
distribution: 'temurin'
18+
cache: 'maven'
19+
# Only run mvn test-compile if this is a primary key CACHE MISS (new/modified pom.xml).
20+
# If cache-hit == 'true', ~/.m2/repository is already warm in GitHub Actions Cache and
21+
# this step is skipped entirely (~3s total job execution!).
22+
# Why 'mvn test-compile'?
23+
# Using test-compile (instead of compile) downloads both production dependencies (compile scope)
24+
# AND test libraries (test scope: JUnit, Mockito, Truth, Testcontainers) into ~/.m2/repository.
25+
# When this job completes, setup-java saves ~/.m2/repository to GitHub Actions Cache.
26+
# Downstream unit test jobs restore this cache and run offline without downloading any JARs from Maven Central.
27+
- name: Prime Maven Cache
28+
if: steps.setup-java.outputs.cache-hit != 'true'
29+
shell: bash
30+
run: mvn test-compile -DskipTests -Pquick-build -T 1C --batch-mode --no-transfer-progress

.github/workflows/ci.yaml

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,24 @@ jobs:
5858
- '.github/workflows/ci.yaml'
5959
- '.kokoro/**'
6060
# these unit tests are "bulk" (non-handwritten) libraries
61+
# prime-cache populates ~/.m2/repository once for the entire workflow.
62+
# This downloads all required JARs so downstream unit test jobs do not need to download any JARs from Maven Central.
63+
prime-cache:
64+
runs-on: ubuntu-latest
65+
steps:
66+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
67+
with:
68+
persist-credentials: false
69+
- uses: ./.github/actions/prime-maven-cache
70+
6171
units:
6272
runs-on: ubuntu-latest
63-
needs: bulk-filter
73+
needs: [bulk-filter, prime-cache]
6474
strategy:
6575
fail-fast: false
6676
matrix:
6777
java: [11, 17, 21, 25, 26]
6878
steps:
69-
- name: Get current week within the year
70-
id: date
71-
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
72-
run: echo "::set-output name=week_of_year::$(date +'%W' --utc)"
7379
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
7480
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
7581
with:
@@ -79,28 +85,19 @@ jobs:
7985
with:
8086
distribution: temurin
8187
java-version: ${{matrix.java}}
88+
cache: maven
8289
- run: java -version
8390
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
84-
- uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
85-
id: mvn-cache
86-
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
87-
with:
88-
path: ~/.m2/repository
89-
key: ${{ runner.os }}-maven-unified-${{ steps.date.outputs.week_of_year }}
9091
- run: .kokoro/build.sh
9192
if: ${{ needs.bulk-filter.outputs.runnable == 'true' && (needs.bulk-filter.outputs.src == 'true' || needs.bulk-filter.outputs.ci == 'true') }}
9293
env:
9394
JOB_TYPE: test
9495
JOB_NAME: units-${{matrix.java}}
9596
units-8-runtime:
9697
runs-on: ubuntu-latest
97-
needs: bulk-filter
98+
needs: [bulk-filter, prime-cache]
9899
name: "units (8)"
99100
steps:
100-
- name: Get current week within the year
101-
id: date
102-
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
103-
run: echo "::set-output name=week_of_year::$(date +'%W' --utc)"
104101
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
105102
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
106103
with:
@@ -122,12 +119,6 @@ jobs:
122119
java-version: 11
123120
distribution: temurin
124121
cache: maven
125-
- uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
126-
id: mvn-cache
127-
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
128-
with:
129-
path: ~/.m2/repository
130-
key: ${{ runner.os }}-maven-unified-${{ steps.date.outputs.week_of_year }}
131122
- run: .kokoro/build.sh
132123
if: ${{ needs.bulk-filter.outputs.runnable == 'true' && (needs.bulk-filter.outputs.src == 'true' || needs.bulk-filter.outputs.ci == 'true') }}
133124
shell: bash
@@ -136,7 +127,7 @@ jobs:
136127
JOB_NAME: units-8-runtime-${{matrix.java}}
137128
# detect which libraries have changed
138129
changes:
139-
needs: bulk-filter
130+
needs: [bulk-filter, prime-cache]
140131
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
141132
runs-on: ubuntu-latest
142133
permissions:
@@ -271,16 +262,14 @@ jobs:
271262
package: ${{ fromJSON(needs.changes.outputs.packages) }}
272263
java: [11, 17, 21, 25, 26]
273264
steps:
274-
- name: Get current week within the year
275-
id: date
276-
run: echo "::set-output name=week_of_year::$(date +'%W' --utc)"
277265
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
278266
with:
279267
persist-credentials: false
280268
- uses: actions/setup-java@c1e323688fd81a25caa38c78aa6df2d33d3e20d9 # v4
281269
with:
282270
distribution: temurin
283271
java-version: ${{matrix.java}}
272+
cache: maven
284273
- run: .kokoro/build.sh
285274
env:
286275
BUILD_SUBDIR: ${{matrix.package}}
@@ -296,9 +285,6 @@ jobs:
296285
package: ${{ fromJSON(needs.changes.outputs.packages) }}
297286
java: [8]
298287
steps:
299-
- name: Get current week within the year
300-
id: date
301-
run: echo "::set-output name=week_of_year::$(date +'%W' --utc)"
302288
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
303289
with:
304290
persist-credentials: false
@@ -308,11 +294,6 @@ jobs:
308294
distribution: temurin
309295
cache: maven
310296
- run: java -version
311-
- uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
312-
id: mvn-cache
313-
with:
314-
path: ~/.m2/repository
315-
key: ${{ runner.os }}-maven-unified-${{ steps.date.outputs.week_of_year }}
316297
- name: Install all modules using Java 11
317298
shell: bash
318299
run: .kokoro/build.sh
@@ -353,6 +334,7 @@ jobs:
353334
with:
354335
distribution: temurin
355336
java-version: 11
337+
cache: maven
356338
- run: .kokoro/build.sh
357339
env:
358340
BUILD_SUBDIR: ${{matrix.package}}
@@ -372,6 +354,7 @@ jobs:
372354
with:
373355
distribution: temurin
374356
java-version: 17
357+
cache: maven
375358
- run: .kokoro/dependencies.sh
376359
env:
377360
BUILD_SUBDIR: ${{matrix.package}}
@@ -387,7 +370,7 @@ jobs:
387370
- name: Success otherwise
388371
run: echo "Success!"
389372
windows:
390-
needs: bulk-filter
373+
needs: [bulk-filter, prime-cache]
391374
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
392375
runs-on: windows-latest
393376
steps:
@@ -407,7 +390,7 @@ jobs:
407390
JOB_TYPE: test
408391
JOB_NAME: windows-units
409392
lint:
410-
needs: bulk-filter
393+
needs: [bulk-filter, prime-cache]
411394
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
412395
runs-on: ubuntu-latest
413396
steps:
@@ -419,20 +402,18 @@ jobs:
419402
with:
420403
distribution: temurin
421404
java-version: 17
405+
cache: maven
422406
- run: java -version
423407
- run: .kokoro/build.sh
424408
env:
425409
JOB_TYPE: lint
426410
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
427411
BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.before }}
428412
enforcer:
429-
needs: bulk-filter
413+
needs: [bulk-filter, prime-cache]
430414
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
431415
runs-on: ubuntu-latest
432416
steps:
433-
- name: Get current week within the year
434-
id: date
435-
run: echo "::set-output name=week_of_year::$(date +'%W' --utc)"
436417
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
437418
with:
438419
persist-credentials: false
@@ -449,7 +430,7 @@ jobs:
449430
- run: java -version
450431
- run: mvn -B -ntp enforcer:enforce@enforce -T 1C
451432
gapic-libraries-bom:
452-
needs: bulk-filter
433+
needs: [bulk-filter, prime-cache]
453434
if: ${{ needs.bulk-filter.outputs.runnable == 'true' }}
454435
runs-on: ubuntu-latest
455436
steps:

0 commit comments

Comments
 (0)