diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..6568e93 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,30 @@ +name: Deploy Worker + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: oven-sh/setup-bun@v1 + with: + bun-version: "1.3.9" + - name: Install dependencies + run: bun install + - name: Build admin + run: bun run --filter new-api-lite-admin build + - name: Apply D1 migrations (remote) + env: + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + run: bunx wrangler d1 migrations apply DB --remote --config apps/worker/wrangler.toml + - name: Deploy worker + env: + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + run: bunx wrangler deploy --config apps/worker/wrangler.toml diff --git a/README.md b/README.md index 67bd9f7..c8fe067 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # new-api-lite 简易版 new-api:Cloudflare Workers + D1 后端,Vite 静态管理台。 +管理台构建产物通过 Worker Static Assets 与 Worker 一起部署。 ## 目录结构 @@ -75,12 +76,19 @@ bun run fix ## D1 数据库 -在 `apps/worker/migrations/0001_init.sql` 定义表结构,使用 Wrangler 迁移: +在 `apps/worker/migrations/0001_init.sql` 定义表结构,使用 Wrangler 迁移。 +本地开发使用本地数据库,云端部署使用 `--remote`: ```bash bun run --filter new-api-lite-worker db:migrate ``` +云端(Cloudflare D1)请使用: + +```bash +bunx wrangler d1 migrations apply DB --remote +``` + ## 忘记管理员密码 管理员密码存储为哈希,可通过删除设置记录来重置,下一次登录会用输入的密码重新初始化: @@ -89,36 +97,78 @@ bun run --filter new-api-lite-worker db:migrate bunx wrangler d1 execute DB --command "DELETE FROM settings WHERE key = 'admin_password_hash';" ``` -## 生产部署 +## 云端部署(Cloudflare Workers + D1) -### 1) 准备 Cloudflare 资源 +说明: +- 管理台静态资源来自 `apps/admin/dist` +- `/api/*` 与 `/v1/*` 会优先走 Worker 逻辑 +- 部署顺序:先构建管理台,再部署 Worker +- 下述 `wrangler deploy` 与 `--remote` 均为云端部署/迁移(非本地模拟) -1. 创建 D1 数据库并回填 `apps/worker/wrangler.toml` 的 `database_id`: +### 1) 新建部署 + +1. 安装依赖: + +```bash +bun install +``` + +2. 创建 D1 数据库并回填 `apps/worker/wrangler.toml` 的 `database_id`: ```bash bunx wrangler d1 create new_api_lite ``` -2. 设置 Worker 变量与密钥(如 CORS_ORIGIN): +3. 设置 Worker 变量与密钥(如 CORS_ORIGIN): 其他变量可继续放在 `apps/worker/wrangler.toml` 的 `[vars]` 中,`CORS_ORIGIN` 需指向管理台域名。 -### 2) 执行生产迁移 - -```bash -bunx wrangler d1 migrations apply DB -``` - -### 3) 部署 Worker - -```bash -bun run --filter new-api-lite-worker deploy -``` - -### 4) 构建并部署 Admin +4. 构建管理台并上传静态资源: ```bash bun run --filter new-api-lite-admin build ``` -将 `apps/admin/dist` 发布到 Cloudflare Pages 或任意静态托管;如需指定 API 基址,请在构建前设置 `VITE_API_BASE`(例如 `apps/admin/.env.production`)。 +5. 执行远程迁移: + +```bash +bunx wrangler d1 migrations apply DB --remote +``` + +6. 部署 Worker: + +```bash +bun run --filter new-api-lite-worker deploy +``` + +### 2) 更新前端(仅管理台变更) + +```bash +bun run --filter new-api-lite-admin build +bun run --filter new-api-lite-worker deploy +``` + +### 3) 更新后端(仅 Worker 变更) + +```bash +bun run --filter new-api-lite-worker deploy +``` + +### 4) 数据库更新 + +1. 添加新的迁移文件到 `apps/worker/migrations/`。 +2. 执行远程迁移: + +```bash +bunx wrangler d1 migrations apply DB --remote +``` + +### 5) 自动部署(GitHub Actions) + +工作流默认在 `main` 分支推送时触发,步骤为:安装依赖 → 构建管理台 → 部署 Worker。 + +需要在仓库 Secrets 中配置: +- `CLOUDFLARE_API_TOKEN`:具有 Workers 与 D1 权限的 API Token +- `CLOUDFLARE_ACCOUNT_ID`:Cloudflare 账户 ID(若 token 无法自动识别账户) + +确保 `apps/worker/wrangler.toml` 中已填写 `database_id`。 diff --git a/apps/worker/wrangler.toml b/apps/worker/wrangler.toml index 47519d3..0e36c6b 100644 --- a/apps/worker/wrangler.toml +++ b/apps/worker/wrangler.toml @@ -11,3 +11,8 @@ PROXY_RETRY_DELAY_MS = "200" binding = "DB" database_name = "new_api_lite" database_id = "00000000-0000-0000-0000-000000000000" + +[assets] +directory = "../admin/dist" +not_found_handling = "single-page-application" +run_worker_first = ["/api/*", "/v1/*"] diff --git a/helloagents/CHANGELOG.md b/helloagents/CHANGELOG.md index add62c2..1c6fe65 100644 --- a/helloagents/CHANGELOG.md +++ b/helloagents/CHANGELOG.md @@ -6,6 +6,12 @@ - **[tooling]**: 修复 bun check 脚本名称(移除尾随空格) - 类型: 微调(无方案包) - 文件: package.json +- **[docs]**: 补充云端部署与本地迁移的区分说明 + - 类型: 微调(无方案包) + - 文件: README.md +- **[ci]**: 自动部署加入远程 D1 迁移步骤 + - 类型: 微调(无方案包) + - 文件: .github/workflows/deploy.yml - **[admin-ui]**: 渠道创建移除 ID 字段并校验名称唯一 - 类型: 微调(无方案包) - 文件: apps/admin/src/main.tsx:250-287, 664-829 @@ -55,6 +61,12 @@ - 类型: 微调(无方案包) - 文件: apps/worker/src/routes/proxy.ts +## [0.4.2] - 2026-02-16 + +### 变更 +- **[deployment]**: 管理台通过 Worker Static Assets 部署并补齐手动/自动部署流程 + - 方案: [202602161013_worker-assets-deploy](archive/2026-02/202602161013_worker-assets-deploy/) + ## [0.4.1] - 2026-02-15 ### 变更 diff --git a/helloagents/modules/admin-ui.md b/helloagents/modules/admin-ui.md index a367665..b2ebc0a 100644 --- a/helloagents/modules/admin-ui.md +++ b/helloagents/modules/admin-ui.md @@ -10,6 +10,7 @@ ## 行为规范 - Vite 构建静态文件 +- 构建产物 `apps/admin/dist` 由 Worker Static Assets 托管 - 使用 `hono/jsx/dom` + TSX 渲染管理台入口 - 入口文件为 `apps/admin/src/App.tsx`(`main.tsx` 已移除) - 按 `core/`(类型/常量/API)、`features/`(业务视图 + 布局/登录)组织前端模块