# 创建GitHub Actions部署工作流的提示词

> AI代理提示词，用于生成一个包含类型检查、测试、构建和部署步骤的Next.js生产级GitHub Actions CI/CD工作流。

**Type:** Prompt  
**Tools:** Cursor, Claude Code, Codex, Windsurf  
**Stack:** Next.js, TypeScript, Cloudflare  
**Difficulty:** medium  
**Updated:** 2026-06-08

---

将此提示词提供给您的代理，以生成一个经过实战检验的GitHub Actions工作流文件，
该文件在执行类型检查、测试和生产构建后进行部署——无需
提交秘密信息或使用已弃用的操作版本。

## 主要提示词

```txt title="Main Prompt"
You are working in a Next.js 15 project that uses TypeScript and Bun as the package manager.
Deployments go to Cloudflare Pages.

Task: create a GitHub Actions CI/CD workflow at `.github/workflows/deploy.yml`.

Requirements:
- Trigger on: `push` to `main` and `pull_request` targeting `main`.
- On pull_request: run only the `ci` job (lint, typecheck, build).
- On push to main: run `ci` then `deploy` (deploy only if ci passes).
- `ci` job:
  - Runner: `ubuntu-latest`.
  - Steps: checkout, setup Bun (`oven-sh/setup-bun@v2`), `bun install --frozen-lockfile`,
    `bun run typecheck`, `bun run build`.
  - Cache: use `actions/cache@v4` to cache `node_modules` keyed on `bun.lock` hash.
- `deploy` job:
  - needs: [ci]
  - if: `github.event_name == 'push'`
  - Deploy to Cloudflare Pages using `cloudflare/wrangler-action@v3`.
  - Secrets used: `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID` (from GitHub secrets — do NOT
    hard-code values).
  - Set `command: pages deploy .next --project-name=my-app --branch=main`.
- Add a concurrency group to cancel in-progress runs on the same branch.
- Do NOT pin to `@v1` of any action — use the versions specified above.
- Do NOT store any secret value in the YAML file.

Stop and show the complete workflow YAML before writing the file.
```

## 实施说明

- `oven-sh/setup-bun@v2` 从 `.bun-version` 或 `package.json#engines.bun` 安装指定的Bun版本
  （如果存在）——确保这些文件之一存在，或者通过 `bun-version: '1.x'` 固定版本。
- `--frozen-lockfile` 防止CI静默升级依赖项；此处失败意味着 `bun.lock`
  在本地不同步。
- 使用 `wrangler-action@v3` 的Cloudflare Pages要求项目首先在Cloudflare
  仪表板中存在——在YAML内部的注释中记录此先决条件。
- 并发：使用 `group: ${{ github.workflow }}-${{ github.ref }}` 配合 `cancel-in-progress: true`。

## 预期的文件更改

```txt
.github/workflows/deploy.yml    (new)
```

## 验收标准

- 向 `main` 分支的PR仅触发 `ci` 作业并发布状态检查。
- 合并到 `main` 分支按顺序触发 `ci` 然后 `deploy`。
- YAML文件中不出现任何秘密信息。
- 对同一分支的第二次推送将取消前一次正在运行的作业。

## 测试命令

```bash
# validate YAML syntax locally
bun x @actions/validator .github/workflows/deploy.yml || true
# or use actionlint
docker run --rm -v "$(pwd):/repo" rhysd/actionlint:latest /repo/.github/workflows/deploy.yml
```

## 常见的AI错误

- 硬编码 `CLOUDFLARE_API_TOKEN` 的值，而不是使用 `${{ secrets.CLOUDFLARE_API_TOKEN }}`。
- 使用已弃用的 `actions/checkout@v2` 或 `actions/cache@v2` 而不是v4。
- 在pull_request事件上运行部署作业，导致从fork部署。
- 部署作业缺少 `needs: [ci]`，导致即使测试失败也能部署。

## 修复提示词

```txt title="Fix Prompt"
The workflow deploys even when CI fails, or secrets are exposed. Fix in order:
1. Add `needs: [ci]` to the `deploy` job.
2. Add `if: github.event_name == 'push' && github.ref == 'refs/heads/main'` to the deploy job.
3. Replace any hard-coded token values with `${{ secrets.CLOUDFLARE_API_TOKEN }}`.
4. Upgrade any @v1/@v2 action references to the versions specified in the original prompt.
Show only the corrected YAML diff.
```