name: CI on: push: branches: [main] tags: - 'v*' pull_request: branches: [main] # 默认只读;需要写权限的 Job(如推送镜像)单独声明 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 # 只在 GitHub 上运行;Gitea 读取 main 时会回退到本目录,需跳过 if: github.server_url == 'https://github.com' name: Public History Check steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - name: Check public history before continuing CI run: | python3 -m unittest discover -s tests/publication -v python3 scripts/check-public-history.py HEAD windows-signing-check: name: Windows Signature Validation needs: publication-check if: "!startsWith(github.ref, 'refs/tags/')" runs-on: windows-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: 检查签名文件验证与异常拦截 shell: pwsh run: ./tests/packaging/test-windows-signatures.ps1 # ============================================ # 测试阶段(仅 main 分支 push 或 PR) # ============================================ unit-test: needs: publication-check runs-on: ubuntu-latest name: Unit Tests # 只在非 tag push 时运行测试 if: "!startsWith(github.ref, 'refs/tags/')" steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Setup Node.js uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: '24' cache: 'npm' - name: Install dependencies run: npm ci --no-audit --no-fund - name: Lint run: npm run lint - name: Run unit tests run: npm run test:unit integration-test: runs-on: ubuntu-latest name: Integration Tests needs: publication-check if: "!startsWith(github.ref, 'refs/tags/')" steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Setup Node.js uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: '24' cache: 'npm' - name: Install dependencies run: npm ci --no-audit --no-fund - name: Generate Prisma Client run: npx prisma generate - name: Run integration tests run: npm run test:integration coverage: runs-on: ubuntu-latest name: Test Coverage needs: publication-check if: "!startsWith(github.ref, 'refs/tags/')" steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Setup Node.js uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: '24' cache: 'npm' - name: Install dependencies run: npm ci --no-audit --no-fund - name: Generate Prisma Client run: npx prisma generate # 低于 vitest.config.mts 中的覆盖率阈值时失败 - name: Run tests with coverage run: npm run test:coverage - name: Write coverage summary if: always() run: | node -e ' const fs = require("fs"); if (!fs.existsSync("coverage/coverage-summary.json")) process.exit(0); const { total } = JSON.parse(fs.readFileSync("coverage/coverage-summary.json", "utf8")); const rows = ["statements", "branches", "functions", "lines"].map((k) => `| ${k} | ${total[k].pct}% | ${total[k].covered}/${total[k].total} |`); fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, ["### 测试覆盖率(src/lib、src/app/api)", "", "| 指标 | 覆盖率 | 覆盖数 |", "| --- | --- | --- |", ...rows, ""].join("\n")); ' - name: Upload coverage report if: always() uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: coverage-report path: coverage/ retention-days: 14 build-check: runs-on: ubuntu-latest name: Build Check needs: publication-check if: "!startsWith(github.ref, 'refs/tags/')" steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Setup Node.js uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: '24' cache: 'npm' - name: Cache Next.js build uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ github.workspace }}/.next/cache key: nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-${{ github.sha }} restore-keys: | nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}- nextjs-${{ runner.os }}- - name: Install dependencies run: npm ci --no-audit --no-fund - name: Generate Prisma Client run: npx prisma generate - name: Build application run: npm run build docker-build-check: name: Docker Build Check (${{ matrix.arch }}) needs: publication-check if: "!startsWith(github.ref, 'refs/tags/')" timeout-minutes: 30 strategy: fail-fast: false matrix: include: - arch: amd64 platform: linux/amd64 os: ubuntu-24.04 - arch: arm64 platform: linux/arm64 os: ubuntu-24.04-arm runs-on: ${{ matrix.os }} steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Set up Docker Buildx uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 # 使用正式 Dockerfile 验证构建上下文、Alpine 测试及生产构建 - name: Build Docker image without publishing uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 with: context: . platforms: ${{ matrix.platform }} push: false cache-from: type=gha,scope=docker-${{ matrix.arch }} cache-to: type=gha,scope=docker-${{ matrix.arch }},mode=max e2e-test: runs-on: ubuntu-latest name: E2E Tests needs: [unit-test, integration-test, build-check] timeout-minutes: 60 steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Setup Node.js uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: '24' cache: 'npm' - name: Restore Next.js build cache uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ github.workspace }}/.next/cache key: nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-${{ github.sha }} restore-keys: | nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}- nextjs-${{ runner.os }}- - name: Install dependencies run: npm ci --no-audit --no-fund - name: Install Playwright Browsers # 系统依赖来自 Ubuntu 软件源,托管机器偶尔解析不了软件源域名; # 每次最多等 4 分钟、重试 3 次,仍失败时只装浏览器本体(托管镜像已带大部分系统库) run: | for attempt in 1 2 3; do if timeout 240 npx playwright install --with-deps chromium; then exit 0; fi echo "::warning::安装 Playwright 系统依赖失败(第 ${attempt} 次),30 秒后重试" sleep 30 done echo "::warning::系统依赖仍无法安装,改为只安装浏览器" npx playwright install chromium - name: Run isolated E2E with local model service run: npm run test:e2e env: CI: true E2E_CACHE_READONLY: '1' - name: Upload Playwright Report if: failure() uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: playwright-report path: ${{ env.E2E_REPORT_DIR }} retention-days: 30 # ============================================ # Docker 构建(仅 tag push,原生双架构并行构建后合并多架构 Manifest) # ============================================ docker-build-arch: name: Docker Build (${{ matrix.arch }}) # 只在 tag 推送时构建镜像,且须先通过公开历史检查 needs: publication-check if: startsWith(github.ref, 'refs/tags/') strategy: fail-fast: false matrix: include: - arch: amd64 platform: linux/amd64 os: ubuntu-24.04 - arch: arm64 platform: linux/arm64 os: ubuntu-24.04-arm runs-on: ${{ matrix.os }} permissions: contents: read packages: write steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Log in to GitHub Container Registry uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract Docker metadata id: meta uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 with: images: ghcr.io/${{ github.repository }} tags: | type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} type=raw,value=latest,enable=${{ !contains(github.ref, '-') }} type=sha,prefix=sha- - name: Set up Docker Buildx uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 - name: Build and push Docker image by digest id: build uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 with: context: . platforms: ${{ matrix.platform }} labels: ${{ steps.meta.outputs.labels }} build-args: | APP_VERSION=${{ steps.meta.outputs.version }} APP_REVISION=${{ github.sha }} outputs: type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=true cache-from: type=gha,scope=docker-${{ matrix.arch }} cache-to: type=gha,scope=docker-${{ matrix.arch }},mode=max - name: Export digest env: DIGEST: ${{ steps.build.outputs.digest }} run: | mkdir -p /tmp/digests touch "/tmp/digests/${DIGEST#sha256:}" - name: Upload digest uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: docker-digest-${{ matrix.arch }} path: /tmp/digests/* if-no-files-found: error retention-days: 1 docker-build: runs-on: ubuntu-24.04 name: Docker Build & Push needs: docker-build-arch if: startsWith(github.ref, 'refs/tags/') permissions: contents: read packages: write steps: - name: Download digests uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: path: /tmp/digests pattern: docker-digest-* merge-multiple: true - name: Log in to GitHub Container Registry uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 - name: Extract Docker metadata id: meta uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 with: images: ghcr.io/${{ github.repository }} tags: | type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} type=raw,value=latest,enable=${{ !contains(github.ref, '-') }} type=sha,prefix=sha- - name: Create multi-arch manifest list and push working-directory: /tmp/digests env: IMAGE: ghcr.io/${{ github.repository }} TAGS: ${{ steps.meta.outputs.tags }} run: | tag_args=() while IFS= read -r tag; do [[ -n "$tag" ]] && tag_args+=(-t "$tag") done <<< "$TAGS" sources=() for digest in *; do sources+=("${IMAGE}@sha256:${digest}") done docker buildx imagetools create "${tag_args[@]}" "${sources[@]}" - name: Inspect image env: IMAGE: ghcr.io/${{ github.repository }} VERSION: ${{ steps.meta.outputs.version }} run: | docker buildx imagetools inspect "${IMAGE}:${VERSION}"