在不同作业之间共享缓存

Eyj*_*afl 8 continuous-integration caching github github-actions

我在同一个 GitHub Actions 工作流程中有两项工作。第一个文件创建一个文件,第二个文件期望在第一个文件创建的同一目录中找到该文件。

我想我可以actions/cache@v3像这样使用它:

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)
Run Code Online (Sandbox Code Playgroud)

GitHub Action 表示缓存已在 中成功恢复job2,但是,在job2我无法my_file在我期望的目录中找到它。问题是什么?

Eyj*_*afl 9

所以,问题实际上是我过去常常在相对于自定义的路径中查找缓存working-directory,而 with 的步骤uses不受此设置的影响,所以我必须使用绝对路径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
Run Code Online (Sandbox Code Playgroud)

现在它按预期工作了。

注意:我不确定第二个作业中是否必须指定与第一个作业中相同的文件。在我的一个项目中,仅指定我需要的子集效果很好,而在另一个项目中,我必须在两个作业中指定完全相同的文件。