Files
wrong-notebook/.github/workflows/ci.yml
2026-10-24 22:45:18 +08:00

206 lines
5.9 KiB
YAML

name: CI
on:
push:
branches: [main]
tags:
- 'v*'
pull_request:
branches: [main]
# 默认只读;需要写权限的 Job(如推送镜像)单独声明
permissions:
contents: read
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@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
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
# ============================================
# 测试阶段(仅 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@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: '22'
cache: 'npm'
- name: Install dependencies
run: npm ci
- 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: unit-test
steps:
- name: Checkout repository
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: '22'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Generate Prisma Client
run: npx prisma generate
- name: Run integration tests
run: npm run test:integration
build-check:
runs-on: ubuntu-latest
name: Build Check
needs: integration-test
steps:
- name: Checkout repository
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: '22'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Generate Prisma Client
run: npx prisma generate
- name: Build application
run: npm run build
e2e-test:
runs-on: ubuntu-latest
name: E2E Tests
needs: build-check
timeout-minutes: 60
env:
DATABASE_URL: "file:./e2e.db"
NEXTAUTH_SECRET: "ci-secret-value-123456"
NEXTAUTH_URL: "http://127.0.0.1:3000"
steps:
- name: Checkout repository
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: '22'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Setup Database
run: |
npx prisma db push
npx prisma db seed
- name: Build application
run: npm run build
- name: Run Playwright tests
run: npx playwright test
env:
CI: true
- name: Upload Playwright Report
if: failure()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: playwright-report
path: playwright-report/
retention-days: 30
# ============================================
# Docker 构建(仅 tag push)
# ============================================
docker-build:
runs-on: ubuntu-latest
name: Docker Build & Push
# 只在 tag 推送时构建镜像,且须先通过公开历史检查
needs: publication-check
if: startsWith(github.ref, 'refs/tags/')
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- name: Log in to GitHub Container Registry
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.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 QEMU (Enable multi-arch)
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
- name: Build and push Docker image
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
APP_VERSION=${{ steps.meta.outputs.version }}
APP_REVISION=${{ github.sha }}
TEST_TIMEOUT_MS=120000
cache-from: type=gha
cache-to: type=gha,mode=max