gradle / actions

A collection of GitHub Actions to accelerate your Gradle Builds on GitHub
https://github.com/marketplace/actions/build-with-gradle
MIT License
129 stars 28 forks source link

Gradle state from cache not restored from tags #274

Closed joschi closed 5 days ago

joschi commented 6 days ago

Hey folks, we are using gradle/actions/setup-gradle@v3 in our monorepo which also contains some Gradle builds.

We do not build the Gradle projects on our default branch (main) but only on tags and we want to reuse the Gradle build and configuration cache from the builds of these tags in our feature branches/pull requests and of course also in the builds of new tags.

Unfortunately, gradle/actions/setup-gradle@v3 fails to restore the existing caches when running on a feature branch or on a new tag, although the prefixes seem to match and they should be found.

The cache is correctly uploaded in the GitHub workflows running on the tagged commits and seem to have the correct names.

Any idea on why the caches are never restored?

Details

gradle/actions/setup-gradle@v3 configuration in workflow ```yaml - uses: actions/setup-java@v4 with: distribution: 'zulu' java-version-file: path/to/.java-version java-package: 'jdk' - uses: gradle/actions/wrapper-validation@v3 - uses: gradle/actions/setup-gradle@v3 with: dependency-graph: ${{ startsWith(github.ref, 'refs/tags/v') && 'generate-and-submit' || 'generate-and-upload' }} cache-read-only: ${{ !startsWith(github.ref, 'refs/tags/v') }} ```
gradle/actions/setup-gradle@v3 configuration from workflow output ```yaml with: dependency-graph: generate-and-upload cache-read-only: true cache-disabled: false cache-write-only: false cache-overwrite-existing: false gradle-home-cache-includes: caches notifications gradle-home-cache-cleanup: false add-job-summary: always add-job-summary-as-pr-comment: never dependency-graph-continue-on-failure: true build-scan-publish: false validate-wrappers: false generate-job-summary: true gradle-home-cache-strict-match: false workflow-job-context: { ... } # redacted github-token: *** env: JAVA_HOME: /opt/hostedtoolcache/Java_Zulu_jdk/21.0.3-9/x64 JAVA_HOME_21_X64: /opt/hostedtoolcache/Java_Zulu_jdk/21.0.3-9/x64 GRADLE_ACTION_ID: gradle/actions/wrapper-validation ```
Writing to the cache when building a tag ``` Caching Gradle state Caching generated-gradle-jars with path '/home/runner/.gradle/caches/[8](https://github.com/my-org/my-repo/actions/runs/9758629559/job/26933588138#step:32:8).8/generated-gradle-jars/gradle-api-8.8.jar' and cache key: gradle-generated-gradle-jars-v1-8b02b97fb417a75a34543e345c4e56f3 Caching wrapper-zips with path '/home/runner/.gradle/wrapper/dists/gradle-8.8-bin/dl7vupf4psengwqhwktix4v1' and cache key: gradle-wrapper-zips-v1-[9](https://github.com/my-org/my-repo/actions/runs/9758629559/job/26933588138#step:32:9)b12742562b89ad80b153fd0e40e926d /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt Caching dependencies with path '/home/runner/.gradle/caches/modules-*/files-*/*/*/*/*' and cache key: gradle-dependencies-v1-ade9f0be0ad0024fb290e56ed3b9a387 Caching instrumented-jars with path '/home/runner/.gradle/caches/jars-*/*/' and cache key: gradle-instrumented-jars-v1-d758f88079697c90056751ad31aac535 Caching kotlin-dsl with path '/home/runner/.gradle/caches/*/kotlin-dsl/accessors/*/ /home/runner/.gradle/caches/*/kotlin-dsl/scripts/*/' and cache key: gradle-kotlin-dsl-v1-330e0a5f497f7b7be3a533e9e2ca4[10](https://github.com/my-repo/my-repo/actions/runs/9758629559/job/26933588138#step:32:10)2 /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt Caching groovy-dsl with path '/home/runner/.gradle/caches/*/groovy-dsl/*/' and cache key: gradle-groovy-dsl-v1-6[11](https://github.com/my-repo/my-repo/actions/runs/9758629559/job/26933588138#step:32:11)779a35c11ad26d36b8e8dcff2cde2 /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt Cache Size: ~0 MB (70687 B) Cache Size: ~0 MB (64352 B) Caching transforms with path '/home/runner/.gradle/caches/transforms-4/*/ /home/runner/.gradle/caches/*/transforms/*/' and cache key: gradle-transforms-v1-1[12](https://github.com/my-repo/my-repo/actions/runs/9758629559/job/26933588138#step:32:12)f6d22872acc863896dfe51dfc1625 Cache saved successfully Cache Size: ~0 MB (108214 B) Cache saved successfully Cache saved successfully /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt Cache Size: ~39 MB (41049992 B) Cache saved successfully Cache Size: ~19 MB (20063639 B) Cache Size: ~[13](https://github.com/my-repo/my-repo/actions/runs/9758629559/job/26933588138#step:32:13)1 MB (137252019 B) Cache saved successfully Cache saved successfully Cache Size: ~140 MB (146538163 B) Cache saved successfully Caching Gradle User Home with cache key: gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]-8560[14](https://github.com/my-repo/my-repo/actions/runs/9758629559/job/26933588138#step:32:14)55b768973966cb11c75e4b6f9ddf537837 /usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/my-repo/my-repo --files-from manifest.txt --use-compress-program zstdmt Cache Size: ~6 MB (6505101 B) Cache saved successfully ```

The Gradle caches exist in GitHub Actions Caches.

GitHub Actions Caches
Failing to restore the cache in a feature branch The action apparently only tries to download `gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]-7fb4e5a6a4f5a839b7a360204248ab6117a28e15` which fails but never tries the other restore keys (which would succeed): - `gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]` - `gradle-home-v1|Linux|test-product-components` - `gradle-home-v1|Linux` ``` ::group::Restore Gradle state from cache Restore Gradle state from cache Requesting Gradle User Home with key:gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]-7fb4e5a6a4f5a839b7a360204248ab6117a28e15 restoreKeys:[gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e],gradle-home-v1|Linux|test-product-components,gradle-home-v1|Linux] Using cache paths: /home/runner/.gradle/caches,/home/runner/.gradle/notifications,/home/runner/.gradle/.setup-gradle ##[debug]Resolved Keys: ##[debug]["gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]-7fb4e5a6a4f5a839b7a360204248ab6117a28e15","gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]","gradle-home-v1|Linux|test-product-components","gradle-home-v1|Linux"] ##[debug]Checking zstd --quiet --version ##[debug]1.5.6 ##[debug]zstd version: 1.5.6 ##[debug]Resource Url: https://acghubeus1.actions.githubusercontent.com/ZGfbyNhsFXAUzxJD3gbJFuUJtqwsRACAfUloIwyhBySosBWnVR/_apis/artifactcache/cache?keys=gradle-home-v1%7CLinux%7Ctest-product-components%5B5d434b537dd905dcf51d5f2bd5e8022e%5D-7fb4e5a6a4f5a839b7a360204248ab6117a28e15%2Cgradle-home-v1%7CLinux%7Ctest-product-components%5B5d434b537dd905dcf51d5f2bd5e8022e%5D%2Cgradle-home-v1%7CLinux%7Ctest-product-components%2Cgradle-home-v1%7CLinux&version=efbc4f98d90c3a37d4c88199cd0a027f9a903840cd0ec083c3af7c7fd4363155 ##[debug]Resource Url: https://acghubeus1.actions.githubusercontent.com/ZGfbyNhsFXAUzxJD3gbJFuUJtqwsRACAfUloIwyhBySosBWnVR/_apis/artifactcache/caches?key=gradle-home-v1%7CLinux%7Ctest-product-components%5B5d434b537dd905dcf51d5f2bd5e8022e%5D-7fb4e5a6a4f5a839b7a360204248ab6117a28e15 ##[debug]Failed to delete archive: Error: ENOENT: no such file or directory, unlink '' Gradle User Home cache not found. Will initialize empty. ::endgroup:: ```
joschi commented 6 days ago

This might be related to https://github.com/actions/cache/issues/1361.

bigdaz commented 5 days ago

Thanks for your report.

What you're seeing is the result of access restrictions applied by GitHub Actions on cache entries. Read here for the details, but note in particular that Workflow runs also cannot restore caches created for different tag names. There is more context in the setup-gradle docs.

You said:

The action apparently only tries to download gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]-7fb4e5a6a4f5a839b7a360204248ab6117a28e15 which fails but never tries the other restore keys (which would succeed)

This is incorrect. You can see from the logs that the action actually passes all of the keys including the restore keys in it's request.

This logging comes from the action:

  Requesting Gradle User Home with
      key:gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]-7fb4e5a6a4f5a839b7a360204248ab6117a28e15
      restoreKeys:[gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e],gradle-home-v1|Linux|test-product-components,gradle-home-v1|Linux]

This logging comes from the GitHub Actions caching infrastructure:

  ##[debug]Resolved Keys:
  ##[debug]["gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]-7fb4e5a6a4f5a839b7a360204248ab6117a28e15","gradle-home-v1|Linux|test-product-components[5d434b537dd905dcf51d5f2bd5e8022e]","gradle-home-v1|Linux|test-product-components","gradle-home-v1|Linux"]

In both cases, you can see that the restore keys are being passed. As explained, the cache misses are due to cache access restrictions, not due to the incorrect keys being used.

The setup-gradle action has no control over these access restrictions or over the branch/tag that is bound to a cache entry.

joschi commented 5 days ago

@bigdaz Thanks for the explanation! Too bad it doesn't work in our setup, but I see why now. 😄