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: Deploy to GitHub Pages | |
| on: | |
| # 当推送到 main 分支时触发 | |
| push: | |
| branches: [ "main" ] | |
| # 允许在 Actions 选项卡中手动运行工作流 | |
| workflow_dispatch: | |
| # 设置权限以允许部署到 GitHub Pages | |
| permissions: | |
| contents: read | |
| pages: write | |
| id-token: write | |
| # 允许一个并发部署 | |
| concurrency: | |
| group: "pages" | |
| cancel-in-progress: true | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Install Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Install pnpm | |
| uses: pnpm/action-setup@v3 | |
| with: | |
| version: 9 | |
| run_install: false | |
| - name: Get pnpm store directory | |
| shell: bash | |
| run: | | |
| echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV | |
| - name: Setup pnpm cache | |
| uses: actions/cache@v4 | |
| with: | |
| path: ${{ env.STORE_PATH }} | |
| key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-pnpm-store- | |
| - name: Install dependencies | |
| run: pnpm install --frozen-lockfile | |
| - name: Build | |
| run: pnpm build:only | |
| # 如果需要设置 API URL 环境变量,请取消注释下一行并修改值 | |
| # env: | |
| # VITE_API_URL: ${{ secrets.VITE_API_URL }} | |
| # 针对 SPA 的路由修复(复制 index.html 为 404.html) | |
| # 这让 GitHub Pages 可以处理 React Router 的深层链接 | |
| - name: Fix SPA Routing for GitHub Pages | |
| run: cp dist/index.html dist/404.html | |
| # 禁用 Jekyll 处理(因为以下划线开头的文件会被 GitHub Pages 忽略,例如 _assets) | |
| - name: Disable Jekyll | |
| run: touch dist/.nojekyll | |
| - name: Upload artifact | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: ./dist | |
| deploy: | |
| environment: | |
| name: github-pages | |
| url: ${{ steps.deployment.outputs.page_url }} | |
| runs-on: ubuntu-latest | |
| needs: build | |
| steps: | |
| - name: Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v4 |