'GitHub Actions: shared cached gradle dependencies

I'm trying to implement caching for my workflow using actions/cache@v2 (https://github.com/actions/cache) action.

I have a workflow which runs on pull requests.My project has tons of dependencies so it would be a great build time improvement if I could re-use artifacts, downloaded previously. After successful run artifacts got cached within this pull request/branch and can be re-used on next runs for this pull request:

Cache restored successfully

Cache restored from key: Linux-823bfbee4d05185dedf13d718e8e4ff4933074565b84dd636e19434372154c6a-8331f72d4c267e28baf5311f561dc9f09f1813899db5fd4929c70715b85b2934-45275122110c339513af5e3da3a953eed4a843d91a5d03cd48b8a26988749289

From this description: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows I got an impression, that the cache (including gradle dependencies, like libraries etc), once downloaded, should be available for other branches based on my develop, after one of the PRs got merged:

A workflow can access and restore a cache created in the current branch, the base branch (including base branches of forked repositories), or the default branch (usually main). For example, a cache created on the default branch would be accessible from any pull request. Also, if the branch feature-b has the base branch feature-a, a workflow triggered on feature-b would have access to caches created in the default branch (main), feature-a, and feature-b.

But apparently, it doesn't work for me, I'm getting this for all the new PRs:

Run actions/cache@v2
Cache not found for input keys: Linux-823bfbee4d05185dedf13d718e8e4ff4933074565b84dd636e19434372154c6a-8331f72d4c267e28baf5311f561dc9f09f1813899db5fd4929c70715b85b2934-45275122110c339513af5e3da3a953eed4a843d91a5d03cd48b8a26988749289

So I'm getting all the gradle dependencies downloaded, over and over again.

So my question is: is it possible to have the dependencies, once downloaded, be available from cache for all other branches/pull requests. If yes, can this be done using actions/cache@v2 or shall I look to another tasks/tools?

Here's simplified version of my workflow:

name: build-pull-request
on:
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
    branches:
      - '**'
  workflow_dispatch:

jobs:
  verify-pull-request-quality:
    name: Verify pull request

    runs-on: ubuntu-latest

    steps:
      - name: Cancel previous runs of this workflow
        uses: styfle/[email protected]
        with:
          access_token: ${{ github.token }}
      - name: Check out ${{ github.ref }}
        uses: actions/checkout@v2
      - name: Cache gradle dependencies
        uses: actions/cache@v2
        env:
          cache-name: cache-gradle-depedencies
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-${{ hashFiles('**/*.gradle*') }}-${{
            hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}-${{
            hashFiles('**/buildSrc/**/*.kt') }}
      - name: Run unit test
        uses: gradle/gradle-build-action@v2
        with:
          build-root-directory: ${{env.working-directory}}
          arguments: assemble



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source