How to Set Up CI for Java (Gradle) With GitHub Actions
gradle/actions/setup-gradle wires up the Gradle build cache and dependency cache for you.
Provide a JDK with actions/setup-java, add gradle/actions/setup-gradle for the Gradle cache, then run ./gradlew check (or build) using the committed Gradle wrapper.
Steps
- Check out the code.
- Run
actions/setup-javafor the JDK. - Add
gradle/actions/setup-gradleto enable the Gradle cache. - Run
./gradlew check(compiles, lints, and tests).
Workflow
.github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['17', '21']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew check --no-daemonGotchas
- Commit the Gradle wrapper (
gradlew,gradle/wrapper/) so CI uses a pinned Gradle version. --no-daemonavoids leaving a daemon running on the ephemeral runner.
Related guides
How to Set Up CI for Java (Maven) With GitHub ActionsSet up GitHub Actions CI for a Maven project: setup-java caches the local ~/.m2 repository, mvn verify compil…
How to Set Up CI for Kotlin With GitHub ActionsSet up GitHub Actions CI for a Kotlin project built with Gradle: setup-java supplies the JDK, setup-gradle ca…