【问题标题】:Share cache between distinct jobs在不同作业之间共享缓存
【发布时间】:2022-08-06 19:57:51
【问题描述】:

我在同一个 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)

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

    标签: github caching continuous-integration github-actions


    【解决方案1】:

    所以,问题实际上是我过去常常在相对于自定义working-directory 的路径中查找缓存,而uses 的步骤不受此设置的影响,所以我不得不为actions/cache 使用绝对路径。

    # In the first job
    - uses: actions/cache@v3
      with:
        path: ${{github.workspace}}/<path to the files you need>  # Must be an absolute path
        key: some-key
    
    # In the second job
    - uses: actions/cache@v3
      with:
        path: ${{github.workspace}}/<path to the files you need>  # This may be any subset of the files you cached in the first job
        key: some-key  # Must be the same key you specified in the first job
    

    现在它按预期工作。

    【讨论】:

    • 嗨,您能否提供如何在另一个作业中使用缓存在一个作业中的目录的语法,我们需要使用 needs.job1.<id>.output 吗?还是有更简单的方法可以做到这一点?
    • @wehelpdox 不,没有必要像这样使用needs。您只需在两个作业中使用相同的代码(相同的路径和相同的键)使用actions/cache@v3 缓存目录
    • 您好,如何在 Job 1 中添加 [ if: steps.cache.outputs.cache-hit != 'true' ] 以验证 Job 2 中是否存在缓存?我们是否需要在 Job 1 中将缓存作为输出发送并在 Job 2 中使用它?
    • 你能提供代码吗?我没有关注你并遇到同样的问题
    • @camiloposada 添加了代码 sn-p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多