最近发 Newsletter 时,我附了不少图片。Hugo 构建网站时得处理这些图片,生成几种尺寸的版本来加快加载。结果构建时间明显变长,有时甚至要 4 分钟。为了解决这个问题,我研究并用上了 GitHub Actions Cache,缩短构建时间。
什么是 GitHub Actions Cache?
GitHub Actions Cache 是 GitHub Actions 的一项功能,可以在多次运行工作流之间保存文件或目录。这样就省去了重新下载或生成未改动的依赖和文件的工作,能大幅缩短工作流的运行时间。
下面是几个常见的缓存用途。
- 项目依赖(node_modules、vendor 等)
- 构建输出(resources、artifacts 等)
- 数据库或大型数据文件
为什么要缩短构建时间?
实际用 Hugo 构建时,一点小改动就要重建整个网站,可能会像开头说的那样耗费不少时间。大多数时候,网站只有一小部分发生了变化,图片、样式表等其他资源在上次构建中就已经处理好了。
Hugo 的 resources 目录里存着处理过的文件,例如调整完尺寸的图片、由 SCSS 编译出的 CSS。缓存这个目录可以省去对未改动文件的重复处理。
在 Hugo 中使用 GitHub Actions Cache
下面是我用来把 Hugo 网站部署到 GitHub Pages 的 GitHub Actions 工作流,其中缓存了 resources 目录。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| name: Deploy site to GitHub Pages
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: true
defaults:
run:
shell: bash
jobs:
build:
runs-on: ubuntu-latest
env:
HUGO_VERSION: 0.140.2
steps:
- name: Install Hugo CLI
run: |
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0
- name: Setup Pages
id: pages
uses: actions/configure-pages@v5
- name: Restore resources
uses: actions/cache/restore@v4
with:
path: |
resources
key: ${{ runner.os }}-resources-${{ github.run_id }}
restore-keys: |
${{ runner.os }}-resources-
- name: Build with Hugo
env:
HUGO_CACHEDIR: ${{ runner.temp }}/hugo_cache
HUGO_ENVIRONMENT: production
TZ: Asia/Ho_Chi_Minh
run: |
hugo \
--gc \
--minify \
--baseURL "${{ steps.pages.outputs.base_url }}/"
- name: Save resources
uses: actions/cache/save@v4
with:
path: |
resources
key: ${{ runner.os }}-resources-${{ github.run_id }}
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
|
缓存步骤详解
上面的工作流有两个与缓存有关的步骤。
- Restore resources: 恢复之前运行时留下的缓存
1
2
3
4
5
6
7
8
| - name: Restore resources
uses: actions/cache/restore@v4
with:
path: |
resources
key: ${{ runner.os }}-resources-${{ github.run_id }}
restore-keys: |
${{ runner.os }}-resources-
|
- Save resources: 构建结束后保存新的缓存
1
2
3
4
5
6
| - name: Save resources
uses: actions/cache/save@v4
with:
path: |
resources
key: ${{ runner.os }}-resources-${{ github.run_id }}
|
缓存键如何生效
缓存设置中用到了以下参数。
- path 指向要缓存的目录或文件,这里是 Hugo 的
resources 目录 - key 是标识缓存的唯一键,常用格式为
${{ runner.os }}-[cache-name]-[hash] - restore-keys 是一组缓存键前缀,用来在找不到精确匹配的缓存时查找可用的缓存
在这个例子中,各段分别代表以下含义。
${{ runner.os }} 确保只在同一种操作系统上使用缓存resources 是缓存名称${{ github.run_id }} 是每次工作流运行的唯一 ID
这样设置后,如果找不到键为 ${{ runner.os }}-resources-${{ github.run_id }} 的缓存,GitHub Actions 就会找前缀为 ${{ runner.os }}-resources- 的最新缓存。
性能对比
启用缓存后,我的构建时间明显缩短了。
| 状态 | 平均构建时间 |
|---|
| 无缓存 | 约 4 分 19 秒 |
| 有缓存 | 约 25 秒 |
如果项目更大,构建时间可能会相差几分钟,甚至几十分钟。

扩展到其他依赖的缓存
除了缓存 Hugo 的 resources,也可以用类似的方法来缓存其他类型的依赖。
Node.js 依赖(node_modules)
1
2
3
4
5
6
7
8
| - name: Restore node_modules
uses: actions/cache/restore@v4
with:
path: |
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
|
Go 模块
1
2
3
4
5
6
7
8
| - name: Restore Go modules
uses: actions/cache/restore@v4
with:
path: |
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
|
优化缓存
要更有效地利用缓存,可以考虑以下做法。
- 有选择地缓存。 只缓存真正需要的文件,避开临时文件和日志
- 对依赖文件计算哈希。 用 package-lock.json 这类文件的哈希生成缓存键,确保依赖发生变化时随之更新缓存
- 分层缓存。 给应用的不同部分分别设立缓存
总结
GitHub Actions Cache 是优化 CI/CD 流程的有力工具。对于 Hugo,缓存 resources 目录可以明显缩短构建时间,尤其是当项目里有大量图片或样式表要处理时。
这只是个简单的 GitHub Actions Cache 示例。实际使用时,要看具体项目,可能需要采用更复杂的缓存策略。
如果想了解更多,可以看看 GitHub Actions Cache 的仓库 https://github.com/actions/cache,或者阅读这篇文档 https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows。
希望这篇文章能帮你优化项目的 CI/CD 流程。有问题或建议的话,欢迎在下面留言。
我正在用的完整工作流可以在 我的 GitHub 仓库 找到,也可以直接复制本文中的示例。
评论