How to use actions/cache@v2 in a matrix job

Viewed 113

Hey guys I am currently running my rect-native workflow with a matrix strategy (3 jobs) and I am downloading the node modules and other cache on every single job.

Any way I can download the cache just once a redistribute the files across all the jobs?

To give you a better understanding of the issue:

I have a matrix strategy that looks like this more or less:

````strategy:
  fail-fast: false
  matrix:
    groups: [
      'Test1',
      'Test2',
      'Test3',
    ]```

and for each of those jobs I am downloading the cache and cocoa pods this way:

        uses: actions/cache@v3
        with:
          path: '**/node_modules'
          key: ${{ runner.os }}-node_modules-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - uses: actions/cache@v3
        with:
          path: ./apps/ios/Pods
          key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-pods-

I would like to reproduce this step only once and then running the remaining steps in the matrix.

Any pro tip?

Thanks in advance

1 Answers

one way to do so is specifying both paths and also including both hash values in the key:

uses: actions/cache@v3
with:
  path: |
    **/node_modules
    **/apps/ios/Pods
  key: ${{ runner.os }}-${{ matrix.groups }}-node-modules-${{ hashFiles('**/yarn.lock') }}-pods-${{ hashFiles('**/Podfile.lock') }}
  • this will work as you expect as long as the hash for both manifests match the cache key
    • Podfile.lock
    • yarn.lock
  • include the matrix (current group) in the cache key
  • omit the restore keys for exact key matching - which i personally prefer in matrices
Related