Repository navigation
ci: PR 预览改用 Cloudflare Workers Preview #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # PR 预览收尾:PR 关闭(合并或放弃)后删除对应的 Workers Preview | ||
| # | ||
| # 使用 pull_request_target 以便读取 secrets(来自 fork 的 PR 也能清理)。 | ||
| # 这里不检出、不执行 PR 中的任何代码,只用 PR 编号拼出 Preview 名称。 | ||
| name: Preview Cleanup | ||
| on: | ||
| pull_request_target: | ||
| types: [closed] | ||
| permissions: {} | ||
| jobs: | ||
| preview-cleanup: | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| WORKER_NAME: ${{ vars.CLOUDFLARE_WORKER_NAME || 'zig-course' }} | ||
| steps: | ||
| - name: Check Cloudflare credentials | ||
| id: cf | ||
| env: | ||
| CF_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| CF_ACCOUNT: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| run: | | ||
| if [ -n "$CF_TOKEN" ] && [ -n "$CF_ACCOUNT" ]; then | ||
| echo "ok=true" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "::notice::未配置 CLOUDFLARE_API_TOKEN / CLOUDFLARE_ACCOUNT_ID,跳过预览清理" | ||
| echo "ok=false" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| # 该 PR 可能从未部署过预览(例如没有改动文档),删除失败不视为错误 | ||
| - name: Delete Workers Preview | ||
| if: steps.cf.outputs.ok == 'true' | ||
| continue-on-error: true | ||
| uses: cloudflare/wrangler-action@v4 | ||
| with: | ||
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| wranglerVersion: "4" | ||
| command: preview delete --worker-name ${{ env.WORKER_NAME }} --name pr-${{ github.event.pull_request.number }} --skip-confirmation | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| # PR 预览第二阶段:把 Preview Build 产出的静态站点部署到 Cloudflare Pages,并在 PR 中回帖 | ||
| # PR 预览第二阶段:把 Preview Build 产出的静态站点部署为 Cloudflare Workers Preview,并在 PR 中回帖 | ||
| # | ||
| # 每个 PR 对应同一个 Worker 下名为 pr-<编号> 的 Preview,PR 更新时覆盖,PR 关闭后由 preview-cleanup.yml 删除。 | ||
| # | ||
| # workflow_run 始终使用默认分支上的工作流定义,在主仓库上下文中运行,可以读取 secrets, | ||
| # 因此即使是来自 fork 的 PR 也能获得预览。这里绝不检出或执行 PR 中的代码: | ||
| # - 构建产物只作为静态文件上传 | ||
| # - 元数据(PR 编号、提交)先做格式校验,再通过 GitHub API 交叉验证 | ||
| # | ||
| # 需要的配置(仓库 Settings → Secrets and variables → Actions): | ||
| # - secrets.CLOUDFLARE_API_TOKEN:权限为 Account → Cloudflare Pages → Edit | ||
| # - secrets.CLOUDFLARE_API_TOKEN:权限为 Account → Workers Scripts → Edit | ||
| # - secrets.CLOUDFLARE_ACCOUNT_ID | ||
| # - vars.CLOUDFLARE_PAGES_PROJECT(可选,默认 zig-course) | ||
| # - vars.CLOUDFLARE_WORKER_NAME(可选,默认 zig-course;Worker 不存在时会自动创建,无需先部署生产版本) | ||
| # 未配置 secrets 时只输出提示并跳过,不会让 CI 失败。 | ||
| name: Preview Deploy | ||
| on: | ||
|
|
@@ -28,7 +30,7 @@ jobs: | |
| runs-on: ubuntu-latest | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| PAGES_PROJECT: ${{ vars.CLOUDFLARE_PAGES_PROJECT || 'zig-course' }} | ||
| WORKER_NAME: ${{ vars.CLOUDFLARE_WORKER_NAME || 'zig-course' }} | ||
| steps: | ||
| - name: Download metadata | ||
| uses: actions/download-artifact@v8 | ||
|
|
@@ -104,23 +106,40 @@ jobs: | |
| run-id: ${{ github.event.workflow_run.id }} | ||
| github-token: ${{ github.token }} | ||
|
|
||
| - name: Deploy to Cloudflare Pages | ||
| # 不检出仓库,wrangler 配置直接在这里生成:只有静态资源,没有 Worker 脚本 | ||
| - name: Write wrangler config | ||
| if: steps.cf.outputs.ok == 'true' | ||
| run: | | ||
| cat > wrangler.jsonc <<EOF | ||
| { | ||
| "name": "$WORKER_NAME", | ||
| "compatibility_date": "2026-10-18", | ||
| "assets": { | ||
| "directory": "./dist", | ||
| "html_handling": "auto-trailing-slash", | ||
| "not_found_handling": "404-page" | ||
| }, | ||
| "previews": {} | ||
| } | ||
| EOF | ||
|
|
||
| - name: Deploy Workers Preview | ||
| id: deploy | ||
| if: steps.cf.outputs.ok == 'true' | ||
| uses: cloudflare/wrangler-action@v4 | ||
| with: | ||
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| wranglerVersion: "4" | ||
| command: pages deploy dist --project-name=${{ env.PAGES_PROJECT }} --branch=pr-${{ steps.meta.outputs.pr }} --commit-hash=${{ steps.meta.outputs.sha }} --commit-dirty=true | ||
| wranglerVersion: "4" # preview 命令及对应输出需要 >= 4.136.3 | ||
| command: preview --name pr-${{ steps.meta.outputs.pr }} --message ${{ steps.meta.outputs.sha }} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
# Inspect both workflows for a shared per-PR concurrency or cancellation rule.
rg -n -C 5 'concurrency:|group:|cancel-in-progress:|preview delete|preview --name' \
.github/workflows/preview-deploy.yml .github/workflows/preview-cleanup.ymlRepository: zigcc/zig-course Length of output: 2679 🏁 Script executed: nl -ba .github/workflows/preview-deploy.yml | sed -n '1,155p'
printf '\n--- preview-cleanup.yml ---\n'
nl -ba .github/workflows/preview-cleanup.yml | sed -n '1,100p'Repository: zigcc/zig-course Length of output: 8797 Prevent a closed PR from recreating its Preview.
Coordinate cleanup with deployment. Serialize cleanup after in-flight deployments, recheck the PR state immediately before deployment, and delete the Preview if the PR closes during deployment. 🤖 Prompt for AI Agents |
||
|
|
||
| - name: Comment on PR | ||
| if: steps.cf.outputs.ok == 'true' | ||
| env: | ||
| PR: ${{ steps.meta.outputs.pr }} | ||
| SHA: ${{ steps.meta.outputs.sha }} | ||
| ALIAS_URL: ${{ steps.deploy.outputs.pages-deployment-alias-url }} | ||
| DEPLOY_URL: ${{ steps.deploy.outputs.deployment-url }} | ||
| ALIAS_URL: ${{ steps.deploy.outputs.preview-url }} | ||
| DEPLOY_URL: ${{ steps.deploy.outputs.preview-deployment-url }} | ||
| RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | ||
| run: | | ||
| preview_url="${ALIAS_URL:-$DEPLOY_URL}" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Report deletion failures when the Preview exists.
If Cloudflare rejects the token or the delete request fails,
continue-on-error: truelets this job succeed while the Preview remains available. GitHub treats a failed step with this setting as successful. Ignore only a confirmed “Preview not found” result; let other failures fail the job so maintainers can retry cleanup. (docs.github.com)🤖 Prompt for AI Agents