Skip to content

Commit 9ae4c7f

Browse files
Update GitHub Actions workflow for build and deploy
Added permissions and optimized deployment steps.
1 parent 113f83d commit 9ae4c7f

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

.github/workflows/build.yml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ on:
1111
jobs:
1212
build:
1313
runs-on: ubuntu-latest
14+
# 核心修改1:添加权限声明,赋予足够的写入权限
15+
permissions:
16+
contents: write # 允许写入仓库内容(推送分支必需)
17+
pages: write # 允许操作 GitHub Pages(可选,增强兼容性)
18+
id-token: write # 配合 Pages 部署的身份验证(可选)
19+
1420
steps:
1521
- name: Install pnpm
1622
uses: pnpm/action-setup@v2
@@ -25,35 +31,34 @@ jobs:
2531
restore-keys: |
2632
${{ runner.os }}-pnpm-
2733
28-
# 1. 拉取代码
34+
# 1. 拉取代码 - 核心修改2:拉取完整历史,避免推送时冲突
2935
- name: Checkout code
3036
uses: actions/checkout@v4
37+
with:
38+
fetch-depth: 0 # 拉取所有提交历史,解决分支推送冲突问题
3139

3240
# 2. 安装 Node.js(Astro 要求 Node.js 18+)
3341
- name: Setup Node.js
3442
uses: actions/setup-node@v4
3543
with:
3644
node-version: 20 # 推荐 20.x
45+
cache: 'pnpm' # 优化:自动缓存 pnpm 依赖,加速构建
3746

3847
# 3. 安装依赖(解决权限问题的关键步骤)
3948
- name: Install dependencies
4049
run: pnpm install --no-frozen-lockfile
41-
# 若使用 pnpm/yarn,替换为 pnpm install 或 yarn install
4250

4351
# 4. 构建项目,生成 dist 目录
4452
- name: Build Astro site
4553
run: pnpm run build # 等价于 astro build,输出到 dist
4654

47-
# 5. 推送到 page 分支
55+
# 5. 推送到 page 分支 - 核心修改3:优化第三方 Action 配置
4856
- name: Deploy to page branch
49-
uses: peaceiris/actions-gh-pages@v4 # 第三方 Action,简化推送流程
57+
uses: peaceiris/actions-gh-pages@v4
5058
with:
51-
github_token: ${{ secrets.GITHUB_TOKEN }} # 自动生成的临时令牌,有仓库操作权限
52-
publish_dir: ./dist # 要推送的目录(Astro 构建产物)
53-
publish_branch: page # 目标分支(page 分支)
54-
force_orphan: true # 强制创建孤儿分支(只保留 dist 内容,可选)
55-
56-
57-
58-
59-
59+
github_token: ${{ secrets.GITHUB_TOKEN }}
60+
publish_dir: ./dist # Astro 构建产物目录
61+
publish_branch: page # 目标分支
62+
force_orphan: true # 强制创建孤儿分支(只保留 dist 内容,清理冗余文件)
63+
user_name: 'github-actions[bot]' # 显式指定提交用户名
64+
user_email: 'github-actions[bot]@users.noreply.github.com' # 显式指定提交邮箱

0 commit comments

Comments
 (0)