ci: 复用构建缓存并配置 CI 与 CodeQL 并发取消

This commit is contained in:
margrop
2026-12-04 06:35:39 +08:00
parent 1d7e6e9525
commit f894873764
3 changed files with 26 additions and 10 deletions

View File

@@ -12,6 +12,11 @@ on:
permissions:
contents: read
# PR 上有新提交时取消同一 PR 的旧运行;main 与版本标签不取消也不串行排队
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name == 'pull_request' && 'pr' || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
publication-check:
runs-on: ubuntu-latest
@@ -215,14 +220,14 @@ jobs:
node-version: '24'
cache: 'npm'
- name: Cache Next.js build
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
- name: Restore Next.js build cache
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ github.workspace }}/.next/e2e-cache
key: e2e-nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-${{ github.sha }}
path: ${{ github.workspace }}/.next/cache
key: nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-${{ github.sha }}
restore-keys: |
e2e-nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-
e2e-nextjs-${{ runner.os }}-
nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-
nextjs-${{ runner.os }}-
- name: Install dependencies
run: npm ci --no-audit --no-fund
@@ -243,6 +248,7 @@ jobs:
run: npm run test:e2e
env:
CI: true
E2E_CACHE_READONLY: '1'
- name: Upload Playwright Report
if: failure()

View File

@@ -12,6 +12,11 @@ on:
permissions:
contents: read
# 同分支/PR 有新推送时取消旧扫描;每周定时扫描单独一组不取消
concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'schedule' && 'schedule' || github.ref }}
cancel-in-progress: ${{ github.event_name != 'schedule' }}
jobs:
analyze:
runs-on: ubuntu-latest

View File

@@ -74,10 +74,13 @@ try {
await run(process.execPath, ['node_modules/prisma/build/index.js', 'migrate', 'deploy']);
await run(process.execPath, [join(app, 'scripts/prepare-e2e.js')], { env: environment });
// 从生产构建启动应用,测试专用支持代码不进入生产源码。
// 优先使用历史 E2E 缓存,否则回退复用 CI 构建阶段恢复的 .next/cache。
const sharedCache = join(root, '.next/e2e-cache');
if (process.env.CI && existsSync(sharedCache)) {
const buildCache = join(root, '.next/cache');
const sourceCache = existsSync(sharedCache) ? sharedCache : (existsSync(buildCache) ? buildCache : null);
if (process.env.CI && sourceCache) {
mkdirSync(join(app, '.next'), { recursive: true });
cpSync(sharedCache, join(app, '.next/cache'), { recursive: true, mode: constants.COPYFILE_FICLONE });
cpSync(sourceCache, join(app, '.next/cache'), { recursive: true, mode: constants.COPYFILE_FICLONE });
}
process.stdout.write('构建隔离应用…\n');
// 与 npm run build 的 prebuild 一致:复制 pdfjs 的 CMap 与标准字体到 public/pdfjs
@@ -132,9 +135,11 @@ try {
await mock.close();
// 保留报告和日志,移除体积较大的隔离构建,测试临时库也随本次目录隔离。
await new Promise((resolve_) => setTimeout(resolve_, 300));
if (built && process.env.CI && existsSync(join(app, '.next/cache'))) {
if (built && process.env.CI && process.env.E2E_CACHE_READONLY !== '1' && existsSync(join(app, '.next/cache'))) {
mkdirSync(join(root, '.next'), { recursive: true });
cpSync(join(app, '.next/cache'), join(root, '.next/e2e-cache'), { recursive: true, mode: constants.COPYFILE_FICLONE });
// 写回前先清空旧缓存目录,防止增量缓存跨次叠加膨胀
rmSync(sharedCache, { recursive: true, force: true });
cpSync(join(app, '.next/cache'), sharedCache, { recursive: true, mode: constants.COPYFILE_FICLONE });
}
rmSync(app, { recursive: true, force: true });
process.stdout.write(`E2E 报告:${reportDir}\n`);