Files
fuwari/.github/workflows/pr.yml
afoim 7b7f4b970e ci(workflows): 优化 PR 预览部署流程
- 将工作流重命名为更准确的 deploy-preview
- 简化部署步骤,移除 pnpm 安装和构建步骤,由 Cloudflare Pages 处理
- 使用环境变量简化部署命令
- 改进预览 URL 评论的脚本逻辑
2025-08-13 13:56:40 +08:00

65 lines
2.0 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: PR Preview Deploy
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# 1. 拉取 PR 分支代码(支持 fork
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
# 2. 安装 Node.js
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
# 3. 安装依赖
- name: Install dependencies
run: npm install
# 4. 本地构建项目(生成 dist
- name: Build site
run: npm run build
# 5. 安装 Wrangler CLI用于上传 dist
- name: Install Wrangler CLI
run: npm install -g wrangler
# 6. 上传 dist 目录到 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: |
BRANCH_NAME=pr-${PR_NUMBER}
wrangler pages deploy ./dist \
--project-name=$CLOUDFLARE_PROJECT_NAME \
--branch=$BRANCH_NAME \
--account-id=$CLOUDFLARE_ACCOUNT_ID \
--api-token=$CLOUDFLARE_API_TOKEN
# 7. 在 PR 评论区发布预览链接
- name: Comment preview URL on PR
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const project = process.env.CLOUDFLARE_PROJECT_NAME;
const url = `https://${project}--pr-${prNumber}.pages.dev`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `✅ 预览已部署:${url}`
});