gradle / gradle-build-action

Execute your Gradle build and trigger dependency submission
https://github.com/marketplace/actions/gradle-build-action
MIT License
679 stars 97 forks source link

Non-main default branch - cache misses #923

Closed mgagliardo91 closed 1 year ago

mgagliardo91 commented 1 year ago

We've been struggling to get the cache to hit on pull requests into our default branch based on the documentation. I had a feeling it could be related to our specific setup (our default branch is staging and main is a fast-forward only version that can only be pushed to from staging).

To test out this theory, I created a simple Gradle repo (via gradle init) and set it up with the workflow below:

name: CI

on:
  push:
    branches: [main, staging]
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-java@v3
      with:
        java-version: 17
        distribution: temurin
    - name: Setup Gradle
      uses: gradle/gradle-build-action@v2
      with:
        cache-read-only: ${{ github.ref != 'refs/heads/main' && github.head_ref != 'staging' }}
    - name: Build the project
      run: ./gradlew --build-cache assemble run

Case 1: Default Branch main:

  1. Push commit to main, let it build. Cache successfully is written and persisted
  2. Create branch test with minor change, open PR to main. Cache is successfully read from main's build, as read-only.

Case 2: Default Branch staging (our case):

  1. Push commit to staging, let it build. Cache successfully is written and persisted
  2. Create branch test2 with minor change, open PR to staging. Cache is missed with message Gradle User Home cache not found. Will initialize empty..

The Case 2 represents our actual workflow and the results that we are seeing. Is there a chance, even though the docs do say that the key matching will work for any PR against the default branch, that there is some hardcoded value where it will only work with the default branch set to main? If not, any other ideas on what's going on?