Share cache between distinct jobs

Viewed 1992

I have two jobs in the same GitHub Actions workflow. The first one creates a file and the second one expects to find this file in the same directory where the first one created it.

I thought I can use actions/cache@v3 for it like so:

jobs:
  job1:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - uses: actions/cache@v3
        with:
          path: some_dir/my_file
          key: my_file

      ... (create the file)

  job2:
    needs: job1
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - uses: actions/cache@v3
        with:
          path: some_dir/my_file
          key: my_file

      ... (use the file)

GitHub Action says that cache is restored successfully in job2, however, in job2 I can't find my_file in the directory where I expect it to be. What is the problem?

1 Answers

So, the problem was actually that I used to look for cache in path relative to custom working-directory while steps with uses are not influenced by this setting, so I had to use absolute paths for actions/cache.

# In the first job
- uses: actions/cache@v3
  with:
    path: <path to the files you need>  # Note that this path is not influenced by working-directory set in defaults, for example
    key: some-key

# In the second job
- uses: actions/cache@v3
  with:
    path: <path to the files you need>  # See the note below
    key: some-key  # Must be the same key you specified in the first job

Now it works as expected.

Note: I'm not sure if the same files must be specified in the second job as in the first one. In one of my projects specifying only the subset I need works fine, while in the other I have to specify exactly the same files in both jobs.

Related