Is hashFiles of actions/cache can be dynamic?

I want to test against multiple rails versions, but it would be nice to benefit of the caching.

For instance I use:

- name: Install Ruby dependencies
        MATRIX_RAILS_VERSION: ${{ matrix.rails-version }}
      run: |
        export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/rails_${MATRIX_RAILS_VERSION}.gemfile"

But it doesn’t seem possible to passe dynamic reference of a file into hashFiles

key: ${{ runner.os }}-${{ matrix.ruby-version }}-${{ matrix.rails-version }}-test-${{ hashFiles("${GITHUB_WORKSPACE}/gemfiles/rails_${{ matrix.rails-version }}.gemfile.lock") }}

Obviously this would have to exclude some versions like head and master.

Nesting ${{ }} expressions inside of expressions is not supported. I’m also pretty sure that $GITHUB_WORKSPACE won’t work here because the value of key isn’t executed in a shell. You can try the following however:

key: ${{ runner.os }}-${{ matrix.ruby-version }}-${{ matrix.rails-version }}-test-${{ hashFiles(format('{0}/gemfiles/rails_{1}.gemfile.lock', github.workspace, matrix.rails-version)) }}

1 Like

Hi @Simran-B thanks for the reply, it’s much appreciated!

Indeed your proposal create properly the expected key:

However, I have no clue why but the cache is never hits even if the tests are green and the key generated have exactly same value.

Anyway, the encouraged way to cache:

      BUNDLE_GEMFILE: "${{ github.workspace }}/gemfiles/rails_${{ matrix.rails-version }}.gemfile"
    - name: Set up Ruby ${{ matrix.ruby-version }}
      uses: ruby/setup-ruby@v1
        ruby-version: ${{ matrix.ruby-version }}
        bundler-cache: true

Generate a correct unique key and works!

Cache key: setup-ruby-bundler-cache-v3-ubuntu-20.04-ruby-3.0.0-/home/runner/work/swl/swl/gemfiles/rails_6.1.3.1.gemfile.lock-211b34df2618f8c21c0e5de2f6f46650fe73e6cba559b547f6fb289d6ed2cb4b
Cache key: setup-ruby-bundler-cache-v3-ubuntu-20.04-ruby-debug-revision-2a02b61fae2c5dcfaf123f43c08c7c7949c1790c-/home/runner/work/swl/swl/gemfiles/rails_main.gemfile.lock-4d904ceb814ad73ded167c4cd1d4035caadc4f9814ca9056d85a1daf598bc7a1

If it’s the exact same value, then none of the hashed files can have changed… Very peculiar. I wonder why it doesn’t work with the cache action. But good that you found an alternative solution!

Yes indeed, it might have an edge case over there but I have not time to investigate further. you can have look though:

Here I added back the Manual Cache Setup

I did a nonsignificant change, pushed again:

Same Key in both, but the key still not found.


Thanks anyway.

1 Like

Shouldn’t it be gemfiles/vendor/bundle instead of vendor/bundle for the cache action path?