GitHub Actions Workflow for Cache Gradle Builds
Cache Gradle so CI builds reuse dependencies and outputs.
This workflow uses the official Gradle setup action, which manages the dependency and build cache for you.
The workflow
.github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with: { distribution: temurin, java-version: '21' }
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew buildNotes
gradle/actions/setup-gradlehandles caching automatically.- Cache is only written from the default branch to avoid PR pollution.
- Use the wrapper (
./gradlew) for reproducible Gradle versions.
Run it cheaper
Point runs-on: at a Latchkey label to run this workflow at roughly 69% lower per-minute cost, with self-healing for transient failures.
Related guides
How to Cache Dependencies in GitHub Actions (Every Ecosystem)Cache dependencies in GitHub Actions with actions/cache - correct keys and paths for npm, yarn, pnpm, pip, Ma…
GitHub Actions Workflow Template for Java (Maven)A ready-to-use GitHub Actions workflow for Java (Maven) with caching and best practices - copy, commit, and r…