From bf1455fada8fd9b20e5f2fc936da1399e9d2deeb Mon Sep 17 00:00:00 2001 From: afoim Date: Wed, 13 Aug 2025 13:32:33 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E6=B7=BB=E5=8A=A0PR=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E7=9A=84GitHub=20Actions=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加新的工作流文件用于在PR创建/更新时自动构建并部署预览到Cloudflare Pages,同时将预览链接评论到PR中 --- .github/workflows/pr.yml | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/pr.yml diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 000000000..ae2831d61 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,58 @@ +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 code + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + + # 2. 安装 pnpm + - name: Install pnpm + uses: pnpm/action-setup@v3 + with: + version: 9 + + # 3. 安装依赖 + - name: Install dependencies + run: pnpm install --frozen-lockfile + + # 4. 构建站点 + - name: Build site + run: pnpm build # 构建产物目录确保是 ./dist 或自行修改 + + # 5. 安装 Wrangler CLI + - name: Install Wrangler CLI + run: pnpm add -g wrangler + + # 6. 部署到 Cloudflare Pages + - name: Deploy to Cloudflare Pages + run: | + BRANCH_NAME=pr-${{ github.event.pull_request.number }} + wrangler pages deploy ./dist \ + --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} \ + --branch=$BRANCH_NAME \ + --account-id=${{ secrets.CLOUDFLARE_ACCOUNT_ID }} \ + --api-token=${{ secrets.CLOUDFLARE_API_TOKEN }} + + # 7. 在 PR 评论区贴出预览链接 + - name: Comment Preview URL + uses: actions/github-script@v7 + with: + script: | + const prNumber = context.payload.pull_request.number; + const previewUrl = `https://${{ secrets.CLOUDFLARE_PROJECT_NAME }}--pr-${prNumber}.pages.dev`; + github.rest.issues.createComment({ + issue_number: prNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body: `✅ 预览已部署: ${previewUrl}` + });