Skip to content

Commit 45fbaa9

Browse files
noodaneeclaude
andcommitted
feat(ci): 为所有构建 job 添加多层缓存优化
实现三层智能缓存策略: 1. **npm 依赖缓存** - 基于 package-lock.json 哈希 - 缓存路径: node_modules - 避免重复安装依赖 2. **RocketMQ 原生依赖缓存** - 基于 build.sh 和 CMakeLists.txt 哈希 - 缓存路径: deps/rocketmq/bin, libs/signature/lib, tmp_down_dir - 跳过耗时的第三方库编译 3. **Addon 构建缓存** - 基于 C++ 源码和构建配置哈希 - 缓存路径: build/rocketmq.node - 当源码未改变时,直接使用缓存的构建产物 缓存命中逻辑: - 当 addon 构建缓存命中时,跳过 RocketMQ 依赖构建和 addon 编译 - 显著减少 CI 构建时间(预计可节省 80%+ 时间) 适用于所有平台: - linux-gnu-x64/arm64 - linux-musl-x64/arm64 - macos-universal Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 8aa1792 commit 45fbaa9

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,43 @@ jobs:
5353
echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV"
5454
echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV"
5555
56+
- name: Cache npm dependencies
57+
uses: actions/cache@v4
58+
with:
59+
path: node_modules
60+
key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }}
61+
restore-keys: |
62+
npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-
63+
64+
- name: Cache RocketMQ native deps
65+
uses: actions/cache@v4
66+
with:
67+
path: |
68+
deps/rocketmq/bin
69+
deps/rocketmq/libs/signature/lib
70+
deps/rocketmq/tmp_down_dir
71+
key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }}
72+
restore-keys: |
73+
rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-
74+
75+
- name: Cache addon build
76+
id: cache-addon
77+
uses: actions/cache@v4
78+
with:
79+
path: build/rocketmq.node
80+
key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }}
81+
restore-keys: |
82+
addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-
83+
5684
- name: Install npm dependencies
5785
run: npm install --no-audit --prefer-offline
5886

5987
- name: Build RocketMQ native deps
88+
if: steps.cache-addon.outputs.cache-hit != 'true'
6089
run: ./deps/rocketmq/build.sh
6190

6291
- name: Build addon
92+
if: steps.cache-addon.outputs.cache-hit != 'true'
6393
run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release
6494

6595
- name: Package artifact
@@ -111,13 +141,43 @@ jobs:
111141
echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV"
112142
echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV"
113143
144+
- name: Cache npm dependencies
145+
uses: actions/cache@v4
146+
with:
147+
path: node_modules
148+
key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }}
149+
restore-keys: |
150+
npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-
151+
152+
- name: Cache RocketMQ native deps
153+
uses: actions/cache@v4
154+
with:
155+
path: |
156+
deps/rocketmq/bin
157+
deps/rocketmq/libs/signature/lib
158+
deps/rocketmq/tmp_down_dir
159+
key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }}
160+
restore-keys: |
161+
rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-
162+
163+
- name: Cache addon build
164+
id: cache-addon
165+
uses: actions/cache@v4
166+
with:
167+
path: build/rocketmq.node
168+
key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }}
169+
restore-keys: |
170+
addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-
171+
114172
- name: Install npm dependencies
115173
run: npm install --no-audit --prefer-offline
116174

117175
- name: Build RocketMQ native deps
176+
if: steps.cache-addon.outputs.cache-hit != 'true'
118177
run: ./deps/rocketmq/build.sh
119178

120179
- name: Build addon
180+
if: steps.cache-addon.outputs.cache-hit != 'true'
121181
run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release
122182

123183
- name: Package artifact
@@ -167,13 +227,43 @@ jobs:
167227
echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV"
168228
echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV"
169229
230+
- name: Cache npm dependencies
231+
uses: actions/cache@v4
232+
with:
233+
path: node_modules
234+
key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }}
235+
restore-keys: |
236+
npm-${{ runner.os }}-
237+
238+
- name: Cache RocketMQ native deps
239+
uses: actions/cache@v4
240+
with:
241+
path: |
242+
deps/rocketmq/bin
243+
deps/rocketmq/libs/signature/lib
244+
deps/rocketmq/tmp_down_dir
245+
key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }}
246+
restore-keys: |
247+
rocketmq-${{ runner.os }}-
248+
249+
- name: Cache addon build
250+
id: cache-addon
251+
uses: actions/cache@v4
252+
with:
253+
path: build/rocketmq.node
254+
key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }}
255+
restore-keys: |
256+
addon-${{ runner.os }}-
257+
170258
- name: Install npm dependencies
171259
run: npm install --no-audit --prefer-offline
172260

173261
- name: Build RocketMQ native deps
262+
if: steps.cache-addon.outputs.cache-hit != 'true'
174263
run: ./deps/rocketmq/build.sh
175264

176265
- name: Build addon
266+
if: steps.cache-addon.outputs.cache-hit != 'true'
177267
run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release
178268

179269
- name: Package artifact
@@ -223,13 +313,43 @@ jobs:
223313
echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV"
224314
echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV"
225315
316+
- name: Cache npm dependencies
317+
uses: actions/cache@v4
318+
with:
319+
path: node_modules
320+
key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }}
321+
restore-keys: |
322+
npm-${{ runner.os }}-
323+
324+
- name: Cache RocketMQ native deps
325+
uses: actions/cache@v4
326+
with:
327+
path: |
328+
deps/rocketmq/bin
329+
deps/rocketmq/libs/signature/lib
330+
deps/rocketmq/tmp_down_dir
331+
key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }}
332+
restore-keys: |
333+
rocketmq-${{ runner.os }}-
334+
335+
- name: Cache addon build
336+
id: cache-addon
337+
uses: actions/cache@v4
338+
with:
339+
path: build/rocketmq.node
340+
key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }}
341+
restore-keys: |
342+
addon-${{ runner.os }}-
343+
226344
- name: Install npm dependencies
227345
run: npm install --no-audit --prefer-offline
228346

229347
- name: Build RocketMQ native deps
348+
if: steps.cache-addon.outputs.cache-hit != 'true'
230349
run: ./deps/rocketmq/build.sh
231350

232351
- name: Build addon
352+
if: steps.cache-addon.outputs.cache-hit != 'true'
233353
run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release
234354

235355
- name: Package artifact
@@ -258,13 +378,43 @@ jobs:
258378
- name: Install build tools
259379
run: brew install cmake ninja xz autoconf automake libtool pkg-config
260380

381+
- name: Cache npm dependencies
382+
uses: actions/cache@v4
383+
with:
384+
path: node_modules
385+
key: npm-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('package-lock.json') }}
386+
restore-keys: |
387+
npm-${{ runner.os }}-
388+
389+
- name: Cache RocketMQ native deps
390+
uses: actions/cache@v4
391+
with:
392+
path: |
393+
deps/rocketmq/bin
394+
deps/rocketmq/libs/signature/lib
395+
deps/rocketmq/tmp_down_dir
396+
key: rocketmq-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }}
397+
restore-keys: |
398+
rocketmq-${{ runner.os }}-
399+
400+
- name: Cache addon build
401+
id: cache-addon
402+
uses: actions/cache@v4
403+
with:
404+
path: build/rocketmq.node
405+
key: addon-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }}
406+
restore-keys: |
407+
addon-${{ runner.os }}-
408+
261409
- name: Install npm dependencies
262410
run: npm install --no-audit --prefer-offline
263411

264412
- name: Build RocketMQ native deps
413+
if: steps.cache-addon.outputs.cache-hit != 'true'
265414
run: ./deps/rocketmq/build.sh
266415

267416
- name: Build addon
417+
if: steps.cache-addon.outputs.cache-hit != 'true'
268418
run: |
269419
npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release \
270420
--CDCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \

0 commit comments

Comments
 (0)