Build Astro dist file #5
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: Build Astro dist file | |
| on: | |
| push: | |
| branches: [main] # 推送到 main 分支时触发 | |
| pull_request: | |
| branches: [main] # 拉取请求时也触发(可选) | |
| schedule: | |
| - cron: "0 5 * * *" | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| # 核心修改1:添加权限声明,赋予足够的写入权限 | |
| permissions: | |
| contents: write # 允许写入仓库内容(推送分支必需) | |
| pages: write # 允许操作 GitHub Pages(可选,增强兼容性) | |
| id-token: write # 配合 Pages 部署的身份验证(可选) | |
| steps: | |
| - name: Install pnpm | |
| uses: pnpm/action-setup@v2 | |
| with: | |
| version: latest # 安装最新版本的 pnpm | |
| - name: Cache pnpm dependencies | |
| uses: actions/cache@v3 | |
| with: | |
| path: ~/.pnpm-store | |
| key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-pnpm- | |
| # 1. 拉取代码 - 核心修改2:拉取完整历史,避免推送时冲突 | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # 拉取所有提交历史,解决分支推送冲突问题 | |
| # 2. 安装 Node.js(Astro 要求 Node.js 18+) | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 # 推荐 20.x | |
| cache: 'pnpm' # 优化:自动缓存 pnpm 依赖,加速构建 | |
| # 3. 安装依赖(解决权限问题的关键步骤) | |
| - name: Install dependencies | |
| run: pnpm install --no-frozen-lockfile | |
| # 4. 构建项目,生成 dist 目录 | |
| - name: Build Astro site | |
| run: pnpm run build # 等价于 astro build,输出到 dist | |
| # 5. 推送到 page 分支 - 核心修改3:优化第三方 Action 配置 | |
| - name: Deploy to page branch | |
| uses: peaceiris/actions-gh-pages@v4 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| publish_dir: ./dist # Astro 构建产物目录 | |
| publish_branch: page # 目标分支 | |
| force_orphan: true # 强制创建孤儿分支(只保留 dist 内容,清理冗余文件) | |
| user_name: 'github-actions[bot]' # 显式指定提交用户名 | |
| user_email: 'github-actions[bot]@users.noreply.github.com' # 显式指定提交邮箱 |