Skip to content
Latchkey

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-java for the JDK.
  • Add gradle/actions/setup-gradle to 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-daemon

Gotchas

  • Commit the Gradle wrapper (gradlew, gradle/wrapper/) so CI uses a pinned Gradle version.
  • --no-daemon avoids leaving a daemon running on the ephemeral runner.

Related guides

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