chore: 完善配置 #15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| branches: [main] | |
| # 允许手动触发工作流 | |
| workflow_dispatch: | |
| # 定义共享的设置步骤 | |
| jobs: | |
| setup: | |
| name: 环境设置 | |
| runs-on: ubuntu-latest | |
| outputs: | |
| pnpm-store-path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v4 | |
| - name: 设置 Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: 安装 pnpm | |
| uses: pnpm/action-setup@v3 | |
| id: pnpm-install | |
| with: | |
| version: 10 | |
| run_install: false | |
| - name: 验证 pnpm 安装 | |
| run: | | |
| echo "PNPM_HOME=$PNPM_HOME" | |
| export PATH="$PNPM_HOME:$PATH" | |
| which pnpm || echo "pnpm not found in PATH" | |
| pnpm --version || echo "pnpm command failed" | |
| - name: 获取 pnpm 缓存目录 | |
| id: pnpm-cache | |
| shell: bash | |
| run: | | |
| echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT | |
| lint-and-check: | |
| name: 代码检查 | |
| runs-on: ubuntu-latest | |
| needs: setup | |
| env: | |
| NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }} | |
| NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }} | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v4 | |
| - name: 设置 Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: 安装 pnpm | |
| uses: pnpm/action-setup@v3 | |
| id: pnpm-install | |
| with: | |
| version: 10 | |
| run_install: false | |
| - name: 验证 pnpm 安装 | |
| run: | | |
| echo "PNPM_HOME=$PNPM_HOME" | |
| export PATH="$PNPM_HOME:$PATH" | |
| which pnpm || echo "pnpm not found in PATH" | |
| pnpm --version || echo "pnpm command failed" | |
| - name: 设置 pnpm 缓存 | |
| uses: actions/cache@v4 | |
| with: | |
| path: ${{ needs.setup.outputs.pnpm-store-path }} | |
| key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-pnpm-store- | |
| - name: 缓存 Astro 构建和图片资源 | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| .astro | |
| src/assets/images | |
| node_modules/.astro | |
| key: ${{ runner.os }}-astro-build-${{ hashFiles('src/assets/images/**', '.astro/**') }} | |
| restore-keys: | | |
| ${{ runner.os }}-astro-build- | |
| - name: 安装依赖 | |
| run: pnpm install --frozen-lockfile | |
| - name: 检查代码格式 | |
| run: pnpm exec prettier --check "./src/**/*.{js,jsx,ts,tsx,json,md,mdx,css,html,yml,yaml}" | |
| - name: 运行 ESLint 检查 | |
| run: pnpm lint:quiet | |
| - name: 生成 Astro 类型 | |
| run: pnpm astro sync | |
| - name: 运行 TypeScript 类型检查 | |
| run: pnpm type-check | |
| e2e-tests: | |
| name: E2E 测试 | |
| runs-on: ubuntu-latest | |
| needs: setup | |
| env: | |
| CI: 'true' # 设置 CI 环境变量为 true,用于测试脚本中区分环境 | |
| NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }} | |
| NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }} | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v4 | |
| - name: 设置 Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: 安装 pnpm | |
| uses: pnpm/action-setup@v3 | |
| id: pnpm-install | |
| with: | |
| version: 10 | |
| run_install: false | |
| - name: 验证 pnpm 安装 | |
| run: | | |
| echo "PNPM_HOME=$PNPM_HOME" | |
| export PATH="$PNPM_HOME:$PATH" | |
| which pnpm || echo "pnpm not found in PATH" | |
| pnpm --version || echo "pnpm command failed" | |
| - name: 设置 pnpm 缓存 | |
| uses: actions/cache@v4 | |
| with: | |
| path: ${{ needs.setup.outputs.pnpm-store-path }} | |
| key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-pnpm-store- | |
| - name: 缓存 Astro 构建和图片资源 | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| .astro | |
| src/assets/images | |
| node_modules/.astro | |
| key: ${{ runner.os }}-astro-build-${{ hashFiles('src/assets/images/**', '.astro/**') }} | |
| restore-keys: | | |
| ${{ runner.os }}-astro-build- | |
| - name: 验证 pnpm 安装 | |
| run: | | |
| which pnpm || echo "pnpm not found in PATH" | |
| echo "PATH=$PATH" | |
| - name: 安装依赖 | |
| run: pnpm install --frozen-lockfile | |
| - name: 构建项目 | |
| run: pnpm build | |
| - name: 缓存 Playwright 浏览器 | |
| uses: actions/cache@v4 | |
| id: playwright-cache | |
| with: | |
| path: ~/.cache/ms-playwright | |
| key: ${{ runner.os }}-playwright-${{ hashFiles('**/pnpm-lock.yaml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-playwright- | |
| - name: 安装 Playwright 浏览器 | |
| if: steps.playwright-cache.outputs.cache-hit != 'true' | |
| run: pnpm exec playwright install --with-deps chromium | |
| - name: 运行 URL 重定向测试 | |
| run: | | |
| export PATH="$PNPM_HOME:$PATH" | |
| pnpm exec playwright test tests/e2e/url-redirects.spec.ts | |
| - name: 保存 Playwright 报告 | |
| uses: actions/upload-artifact@v4 | |
| if: always() | |
| with: | |
| name: playwright-report | |
| path: playwright-report/ | |
| retention-days: 30 | |
| - name: 保存测试截图 | |
| uses: actions/upload-artifact@v4 | |
| if: always() | |
| with: | |
| name: playwright-screenshots | |
| path: playwright-results/ | |
| retention-days: 30 |