How to Enable the Gradle Build Cache in GitHub Actions
The Gradle build cache reuses task outputs; persist ~/.gradle/caches so those outputs survive between CI runs.
Gradle can reuse the output of any up-to-date task via its build cache. Enable it with --build-cache or org.gradle.caching=true, then cache the Gradle home with actions/cache so cached task outputs carry over between runs.
Steps
- Enable caching with
--build-cacheororg.gradle.caching=true. - Persist
~/.gradle/cachesand~/.gradle/wrapperwith actions/cache. - Key on the Gradle build files so a change refreshes the cache.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: gradle-${{ runner.os }}-
- run: ./gradlew build --build-cacheGotchas
- The local build cache lives under the Gradle home, so caching that home carries it over.
- For team-wide reuse, a remote build cache node is more effective than the CI cache alone.
Related guides
How to Cache the Maven and Gradle Caches in GitHub ActionsCache the Maven ~/.m2 repository or the Gradle caches in GitHub Actions, keyed on pom.xml or the Gradle lockf…
How to Cache the Cargo Registry and target Directory in GitHub ActionsCache the Cargo registry, git dependency cache, and the target directory in GitHub Actions, keyed on Cargo.lo…