【问题标题】:GitHub actions: why 'Cache restored successfully' but 'cache-hit' got 'false' problemGitHub 操作:为什么“缓存成功恢复”但“缓存命中”出现“错误”问题
【发布时间】:2023-02-24 10:21:35
【问题描述】:

我正在使用 GitHub 操作体验 CI。

我在每个 CI 上安装依赖项时遇到问题,发现我可以使用 actions/cache 解决这个问题。

这是我的 action.yaml 部分

- name: Cache npm dependency
  uses: actions/cache@v3
  id: npm-cache
  with:
    path: ~/.npm
    key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-npm-

- if: steps.npm-cache.outputs.cache-hit != true
  name: Install Dependency
  run: |
    echo 'npm cache missed'
    npm ci

并在Cache npm dependency 步骤中恢复缓存。

Cache restored successfully
Cache restored from key: Linux-npm-...

但它总是在“安装依赖项”步骤中重新安装依赖项。

Run echo 'npm cache missed'
  echo 'npm cache missed'
  npm ci
  shell: /usr/bin/bash -e {0}
npm cache missed
> nodemon@2.0.16 ...
> node bin/postinstall || exit 0
added 661 packages in 19.862s

结果,缓存变得毫无意义。 我错过了什么或做错了什么?

【问题讨论】:

  • 除非有充分的理由,否则请将输入 cache: npmaction/setup-node 操作一起使用并删除您的自定义缓存。
  • @rethab 我试过了,我得到了同样的结果。
  • 你能创建一个minimal reproducer吗?
  • @rethab 我通过像这样修改它来解决它:!= true!= 'true' :)但我的意图是'node_module'缓存,文档中不推荐这样做,所以我正在努力解决这个问题。 '不建议缓存 node_modules,因为它可以跨 Node' 版本并且不能与 npm ci 链接一起使用:github.com/actions/cache/blob/main/examples.md#node---npm
  • 你还不能用cache: npm吗?

标签: github github-actions


【解决方案1】:

我通过这样修改它来解决它:!= true -> != 'true'

【讨论】:

  • 我似乎无法使用 != 'true' 访问缓存,一小时前我可以访问它
【解决方案2】:

我正在使用操作 cache/restore 从缓存中获取数据,但机制与一般操作类似。

所以就我而言,我遇到了同样令人沮丧的情况,直到最后注意到文档中的一条消息:

请注意,仅当针对精确键匹配发生缓存命中时,cache-hit 才会设置为 true。对于通过恢复键或缓存未命中的部分键匹配,它将被设置为 false

这意味着,如果您的缓存名称没有找到完全匹配,而是通过相对键加载了另一个缓存,cache-hit 将是 false,尽管它在日志中说的是相反的!

例如:

key: dev-npm-v5
restore-keys: dev-npm

但实际日志会说:

Cache restored successfully
Cache restored from key: dev-npm-v4

在那种情况下,你的cache-hit输出是false,但是cache-matched-keydev-npm-v4

所以仔细检查请求的名称与实际检索的缓存名称。我最后添加了一个 bash 脚本来确定是否至少使用 cache-matched-key 变量加载了一些缓存。

希望它能帮助任何人。浪费时间尝试自己调试此行为:3

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 2017-04-05
    • 2016-07-10
    相关资源
    最近更新 更多