CI 提速:缓存 node_modules 并改用 git archive 传输工作区

This commit is contained in:
2026-03-03 16:28:10 +08:00
parent 303e32f599
commit 6490cd068e
Vendored
+29 -4
View File
@@ -13,6 +13,9 @@ pipeline {
APP_NAME = 'no-whatever'
CI_IMAGE = 'mcr.microsoft.com/playwright:v1.51.1-jammy'
NPM_CACHE_VOLUME = 'no-whatever-npm-cache'
NODE_MODULES_VOLUME = 'no-whatever-node-modules'
NPM_LOCK_HASH_FILE = '/npm-cache/no-whatever-package-lock.sha256'
NPM_REGISTRY = 'https://registry.npmmirror.com'
}
triggers {
@@ -62,14 +65,25 @@ pipeline {
cid=$(docker create --ipc=host \
-e HOME=/tmp \
-e NPM_CONFIG_CACHE=/npm-cache \
-e NPM_CONFIG_REGISTRY=${NPM_REGISTRY} \
-v ${NPM_CACHE_VOLUME}:/npm-cache \
-v ${NODE_MODULES_VOLUME}:/workspace/node_modules \
${CI_IMAGE} \
sh -lc "cd /workspace && npm ci --prefer-offline --no-audit --progress=false && npm run lint && npx tsc --noEmit && npm run test:coverage && npm run test:e2e:smoke")
sh -lc "cd /workspace && \
lock_hash=\$(sha256sum package-lock.json | cut -d ' ' -f1) && \
cached_hash=\$(cat ${NPM_LOCK_HASH_FILE} 2>/dev/null || true) && \
if [ \"\$lock_hash\" = \"\$cached_hash\" ] && [ -d node_modules ] && [ \"\$(ls -A node_modules 2>/dev/null)\" ]; then \
echo 'node_modules cache hit, skip npm ci'; \
else \
npm ci --prefer-offline --no-audit --progress=false && \
echo \"\$lock_hash\" > ${NPM_LOCK_HASH_FILE}; \
fi && \
npm run lint && npx tsc --noEmit && npm run test:coverage && npm run test:e2e:smoke")
cleanup() {
docker rm -f "$cid" >/dev/null 2>&1 || true
}
trap cleanup EXIT
docker cp . "$cid":/workspace
git archive --format=tar HEAD | docker cp - "$cid":/workspace
set +e
docker start -a "$cid"
status=$?
@@ -137,14 +151,25 @@ pipeline {
cid=$(docker create --ipc=host \
-e HOME=/tmp \
-e NPM_CONFIG_CACHE=/npm-cache \
-e NPM_CONFIG_REGISTRY=${NPM_REGISTRY} \
-v ${NPM_CACHE_VOLUME}:/npm-cache \
-v ${NODE_MODULES_VOLUME}:/workspace/node_modules \
${CI_IMAGE} \
sh -lc "cd /workspace && npm ci --prefer-offline --no-audit --progress=false && npm run test:e2e")
sh -lc "cd /workspace && \
lock_hash=\$(sha256sum package-lock.json | cut -d ' ' -f1) && \
cached_hash=\$(cat ${NPM_LOCK_HASH_FILE} 2>/dev/null || true) && \
if [ \"\$lock_hash\" = \"\$cached_hash\" ] && [ -d node_modules ] && [ \"\$(ls -A node_modules 2>/dev/null)\" ]; then \
echo 'node_modules cache hit, skip npm ci'; \
else \
npm ci --prefer-offline --no-audit --progress=false && \
echo \"\$lock_hash\" > ${NPM_LOCK_HASH_FILE}; \
fi && \
npm run test:e2e")
cleanup() {
docker rm -f "$cid" >/dev/null 2>&1 || true
}
trap cleanup EXIT
docker cp . "$cid":/workspace
git archive --format=tar HEAD | docker cp - "$cid":/workspace
set +e
docker start -a "$cid"
status=$?