ci(workflows): 优化 PR 预览部署流程

- 将工作流重命名为更准确的 deploy-preview
- 简化部署步骤,移除 pnpm 安装和构建步骤,由 Cloudflare Pages 处理
- 使用环境变量简化部署命令
- 改进预览 URL 评论的脚本逻辑
This commit is contained in:
afoim
2025-08-13 13:56:40 +08:00
parent bf1455fada
commit 7b7f4b970e

View File

@@ -10,49 +10,55 @@ jobs:
steps: steps:
# 1. 拉取 PR 分支代码(支持 fork # 1. 拉取 PR 分支代码(支持 fork
- name: Checkout PR code - name: Checkout PR branch
uses: actions/checkout@v4 uses: actions/checkout@v4
with: with:
ref: ${{ github.event.pull_request.head.sha }} ref: ${{ github.event.pull_request.head.ref }}
# 2. 安装 pnpm # 2. 安装 Node.js
- name: Install pnpm - name: Setup Node.js
uses: pnpm/action-setup@v3 uses: actions/setup-node@v4
with: with:
version: 9 node-version: 20
# 3. 安装依赖 # 3. 安装依赖
- name: Install dependencies - name: Install dependencies
run: pnpm install --frozen-lockfile run: npm install
# 4. 构建站点 # 4. 本地构建项目(生成 dist
- name: Build site - name: Build site
run: pnpm build # 构建产物目录确保是 ./dist 或自行修改 run: npm run build
# 5. 安装 Wrangler CLI # 5. 安装 Wrangler CLI(用于上传 dist
- name: Install Wrangler CLI - name: Install Wrangler CLI
run: pnpm add -g wrangler run: npm install -g wrangler
# 6. 部署到 Cloudflare Pages # 6. 上传 dist 目录到 Cloudflare Pages
- name: Deploy to Cloudflare Pages - name: Deploy dist to Cloudflare Pages
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_PROJECT_NAME: ${{ secrets.CLOUDFLARE_PROJECT_NAME }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: | run: |
BRANCH_NAME=pr-${{ github.event.pull_request.number }} BRANCH_NAME=pr-${PR_NUMBER}
wrangler pages deploy ./dist \ wrangler pages deploy ./dist \
--project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} \ --project-name=$CLOUDFLARE_PROJECT_NAME \
--branch=$BRANCH_NAME \ --branch=$BRANCH_NAME \
--account-id=${{ secrets.CLOUDFLARE_ACCOUNT_ID }} \ --account-id=$CLOUDFLARE_ACCOUNT_ID \
--api-token=${{ secrets.CLOUDFLARE_API_TOKEN }} --api-token=$CLOUDFLARE_API_TOKEN
# 7. 在 PR 评论区贴出预览链接 # 7. 在 PR 评论区发布预览链接
- name: Comment Preview URL - name: Comment preview URL on PR
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
script: | script: |
const prNumber = context.payload.pull_request.number; const prNumber = context.payload.pull_request.number;
const previewUrl = `https://${{ secrets.CLOUDFLARE_PROJECT_NAME }}--pr-${prNumber}.pages.dev`; const project = process.env.CLOUDFLARE_PROJECT_NAME;
github.rest.issues.createComment({ const url = `https://${project}--pr-${prNumber}.pages.dev`;
issue_number: prNumber, await github.rest.issues.createComment({
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
body: `✅ 预览已部署: ${previewUrl}` issue_number: prNumber,
body: `✅ 预览已部署:${url}`
}); });