{
  "id": "create-a-github-actions-deploy-workflow",
  "type": "prompts",
  "category": "prompts",
  "locale": "zh",
  "url": "/zh/prompts/create-a-github-actions-deploy-workflow",
  "title": "创建GitHub Actions部署工作流的提示词",
  "description": "AI代理提示词，用于生成一个包含类型检查、测试、构建和部署步骤的Next.js生产级GitHub Actions CI/CD工作流。",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "TypeScript",
    "Cloudflare"
  ],
  "tags": [
    "deploy",
    "nextjs",
    "typescript",
    "cloudflare"
  ],
  "difficulty": "medium",
  "updated": "2026-06-08",
  "markdown": "将此提示词提供给您的代理，以生成一个经过实战检验的GitHub Actions工作流文件，\n该文件在执行类型检查、测试和生产构建后进行部署——无需\n提交秘密信息或使用已弃用的操作版本。\n\n## 主要提示词\n\n```txt title=\"Main Prompt\"\nYou are working in a Next.js 15 project that uses TypeScript and Bun as the package manager.\nDeployments go to Cloudflare Pages.\n\nTask: create a GitHub Actions CI/CD workflow at `.github/workflows/deploy.yml`.\n\nRequirements:\n- Trigger on: `push` to `main` and `pull_request` targeting `main`.\n- On pull_request: run only the `ci` job (lint, typecheck, build).\n- On push to main: run `ci` then `deploy` (deploy only if ci passes).\n- `ci` job:\n  - Runner: `ubuntu-latest`.\n  - Steps: checkout, setup Bun (`oven-sh/setup-bun@v2`), `bun install --frozen-lockfile`,\n    `bun run typecheck`, `bun run build`.\n  - Cache: use `actions/cache@v4` to cache `node_modules` keyed on `bun.lock` hash.\n- `deploy` job:\n  - needs: [ci]\n  - if: `github.event_name == 'push'`\n  - Deploy to Cloudflare Pages using `cloudflare/wrangler-action@v3`.\n  - Secrets used: `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID` (from GitHub secrets — do NOT\n    hard-code values).\n  - Set `command: pages deploy .next --project-name=my-app --branch=main`.\n- Add a concurrency group to cancel in-progress runs on the same branch.\n- Do NOT pin to `@v1` of any action — use the versions specified above.\n- Do NOT store any secret value in the YAML file.\n\nStop and show the complete workflow YAML before writing the file.\n```\n\n## 实施说明\n\n- `oven-sh/setup-bun@v2` 从 `.bun-version` 或 `package.json#engines.bun` 安装指定的Bun版本\n  （如果存在）——确保这些文件之一存在，或者通过 `bun-version: '1.x'` 固定版本。\n- `--frozen-lockfile` 防止CI静默升级依赖项；此处失败意味着 `bun.lock`\n  在本地不同步。\n- 使用 `wrangler-action@v3` 的Cloudflare Pages要求项目首先在Cloudflare\n  仪表板中存在——在YAML内部的注释中记录此先决条件。\n- 并发：使用 `group: ${{ github.workflow }}-${{ github.ref }}` 配合 `cancel-in-progress: true`。\n\n## 预期的文件更改\n\n```txt\n.github/workflows/deploy.yml    (new)\n```\n\n## 验收标准\n\n- 向 `main` 分支的PR仅触发 `ci` 作业并发布状态检查。\n- 合并到 `main` 分支按顺序触发 `ci` 然后 `deploy`。\n- YAML文件中不出现任何秘密信息。\n- 对同一分支的第二次推送将取消前一次正在运行的作业。\n\n## 测试命令\n\n```bash\n# validate YAML syntax locally\nbun x @actions/validator .github/workflows/deploy.yml || true\n# or use actionlint\ndocker run --rm -v \"$(pwd):/repo\" rhysd/actionlint:latest /repo/.github/workflows/deploy.yml\n```\n\n## 常见的AI错误\n\n- 硬编码 `CLOUDFLARE_API_TOKEN` 的值，而不是使用 `${{ secrets.CLOUDFLARE_API_TOKEN }}`。\n- 使用已弃用的 `actions/checkout@v2` 或 `actions/cache@v2` 而不是v4。\n- 在pull_request事件上运行部署作业，导致从fork部署。\n- 部署作业缺少 `needs: [ci]`，导致即使测试失败也能部署。\n\n## 修复提示词\n\n```txt title=\"Fix Prompt\"\nThe workflow deploys even when CI fails, or secrets are exposed. Fix in order:\n1. Add `needs: [ci]` to the `deploy` job.\n2. Add `if: github.event_name == 'push' && github.ref == 'refs/heads/main'` to the deploy job.\n3. Replace any hard-coded token values with `${{ secrets.CLOUDFLARE_API_TOKEN }}`.\n4. Upgrade any @v1/@v2 action references to the versions specified in the original prompt.\nShow only the corrected YAML diff.\n```"
}