【问题标题】:How to cache GatsbyJS .cache and public folders between builds using GitHub Actions如何使用 GitHub Actions 在构建之间缓存 GatsbyJS .cache 和公共文件夹
【发布时间】:2020-04-14 13:04:06
【问题描述】:

我正在尝试使用 actions/cache 任务的 GitHub Actions 在 GatsbyJS 构建之间持久化缓存数据。

name: Gatsby Contentful Build

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build_job:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1.1.0
      - uses: bahmutov/npm-install@v1.1.0
      - name: Cache
        uses: actions/cache@v1.1.2
        with:
          path: .cache
          key: cache-folder-key
      - name: Cache public folder
        uses: actions/cache@v1.1.2
        with:
          path: public
          key: public-folder-key
      - run: cd plugins/gatsby-source-contentful && yarn build
      - run: yarn build

在初始构建时找不到缓存,然后 gatsby 从头开始​​创建 .cache 和 public,构建并将这两个文件夹保存到动作缓存。

在第二次构建时,找到了新的缓存,并且 gatsby 构建成功,但没有更新缓存。

我似乎很难理解如何使用 key 名称破坏缓存。

这是我理解它应该起作用的方式,如果我错了,请纠正我。

【问题讨论】:

  • 你解决了吗?
  • 是的,我设法通过将缓存和公用文件夹保存到外部服务(例如 aws s3)来使其正常工作
  • 不能在 GH 动作中保留吗?您能否分享您的解决方案作为答案(即您如何设置工作流程)?我对设置类似的东西非常感兴趣:)

标签: gatsby github-actions


【解决方案1】:

如果缓存被命中,缓存不会自动更新。您的主缓存键必须包含对新构建唯一的文件哈希。如果你的 Gatsby 构建需要调用一些远程 API 来获取数据,那么你不能使用官方的缓存操作。

你可以试试这个总是保存缓存的fork:https://github.com/marketplace/actions/always-cache

但使用它需要您自担风险。

【讨论】:

  • 不需要调用远程 API,但 Gatsby 构建序列(参见问题中的图表)实际上应该处理缓存失效的逻辑(例如,它检查是否有任何包或 Gatsby 配置文件已更改 - docs)。在这种情况下 Gatsby 用户是否需要使用 always-cache 操作?
猜你喜欢
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多