Skip to content
Latchkey

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-cache or org.gradle.caching=true.
  • Persist ~/.gradle/caches and ~/.gradle/wrapper with 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-cache

Gotchas

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →