ci(deploy): 支持 init 部署并统一 D1 初始化

This commit is contained in:
lsy committed 2026-02-22 00:08:26 +08:00
1 parent 7a6d51cc9c
commit a5be552a67
11 files changed
+206 -24

No files matched your search

+129 -5
View File
@@ -11,7 +11,11 @@ on:
from_panel:
description: "是否由部署控制面板触发 / triggered from deployment control panel"
required: false
default: "false"
default: "auto"
deploy_action:
description: "部署动作:update/init(默认 update)"
required: false
default: "update"
deploy_target:
description: "部署目标:frontend/backend/both/auto(默认 auto)"
required: false
@@ -28,6 +32,7 @@ jobs:
runs-on: ubuntu-latest
outputs:
should_deploy: ${{ steps.check.outputs.should_deploy }}
deploy_action: ${{ steps.plan.outputs.deploy_action }}
deploy_target: ${{ steps.plan.outputs.deploy_target }}
apply_migrations: ${{ steps.plan.outputs.apply_migrations }}
deploy_ui: ${{ steps.plan.outputs.deploy_ui }}
@@ -98,6 +103,7 @@ jobs:
id: plan
run: |
event="${{ github.event_name }}"
deploy_action="${{ github.event.inputs.deploy_action }}"
deploy_target="${{ github.event.inputs.deploy_target }}"
apply_migrations="${{ github.event.inputs.apply_migrations }}"
@@ -108,14 +114,24 @@ jobs:
if [[ -n "${{ github.event.client_payload.apply_migrations }}" ]]; then
apply_migrations="${{ github.event.client_payload.apply_migrations }}"
fi
if [[ -n "${{ github.event.client_payload.deploy_action }}" ]]; then
deploy_action="${{ github.event.client_payload.deploy_action }}"
fi
fi
if [[ -z "$deploy_action" ]]; then
deploy_action="update"
fi
if [[ -z "$deploy_target" ]]; then
deploy_target="auto"
fi
if [[ -z "$apply_migrations" ]]; then
apply_migrations="auto"
fi
if [[ "$deploy_action" == "init" ]]; then
deploy_target="both"
apply_migrations="true"
fi
ui_changed="${{ steps.changes.outputs.ui_changed }}"
worker_changed="${{ steps.changes.outputs.worker_changed }}"
@@ -161,15 +177,122 @@ jobs:
fi
fi
echo "deploy_action=$deploy_action" >> $GITHUB_OUTPUT
echo "deploy_target=$deploy_target" >> $GITHUB_OUTPUT
echo "apply_migrations=$apply_migrations" >> $GITHUB_OUTPUT
echo "deploy_ui=$deploy_ui" >> $GITHUB_OUTPUT
echo "deploy_worker=$deploy_worker" >> $GITHUB_OUTPUT
echo "should_migrate=$should_migrate" >> $GITHUB_OUTPUT
init-spa:
needs: check-config
if: needs.check-config.outputs.should_deploy == 'true' && needs.check-config.outputs.deploy_action == 'init'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: "1.3.9"
- name: Show init plan
run: |
echo "deploy_action=${{ needs.check-config.outputs.deploy_action }}"
echo "deploy_target=${{ needs.check-config.outputs.deploy_target }}"
echo "apply_migrations=${{ needs.check-config.outputs.apply_migrations }}"
echo "deploy_ui=${{ needs.check-config.outputs.deploy_ui }}"
echo "deploy_worker=${{ needs.check-config.outputs.deploy_worker }}"
echo "should_migrate=${{ needs.check-config.outputs.should_migrate }}"
- name: Check if D1 database exists
id: check-db
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
echo "检查 D1 数据库是否已存在..."
DATABASE_LIST=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
EXISTING_DB=$(echo "$DATABASE_LIST" | jq -r '.[] | select(.name=="api-worker") | .uuid')
if [ -n "$EXISTING_DB" ]; then
echo "db_exists=true" >> $GITHUB_OUTPUT
echo "✅ D1 已存在,跳过初始化"
else
echo "db_exists=false" >> $GITHUB_OUTPUT
fi
- name: Install dependencies
if: steps.check-db.outputs.db_exists != 'true'
run: bun install
- name: Build UI
if: steps.check-db.outputs.db_exists != 'true' && (needs.check-config.outputs.deploy_ui == 'true' || needs.check-config.outputs.deploy_worker == 'true')
run: bun run --filter api-worker-ui build
- name: Verify UI build
if: steps.check-db.outputs.db_exists != 'true' && (needs.check-config.outputs.deploy_ui == 'true' || needs.check-config.outputs.deploy_worker == 'true')
run: |
if [ ! -d "apps/ui/dist" ]; then
echo "❌ UI 构建失败:dist 目录不存在"
exit 1
fi
if [ ! -f "apps/ui/dist/index.html" ]; then
echo "❌ UI 构建失败:index.html 不存在"
exit 1
fi
echo "✅ UI 构建产物已就绪"
- name: Disable wrangler telemetry
if: steps.check-db.outputs.db_exists != 'true'
run: bunx wrangler telemetry disable
- name: Create D1 database (init only)
if: steps.check-db.outputs.db_exists != 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
echo "创建 D1 数据库..."
CREATE_OUTPUT=$(bunx wrangler d1 create api-worker --config apps/worker/wrangler.toml 2>&1)
if echo "$CREATE_OUTPUT" | grep -q "database_id"; then
DATABASE_ID=$(echo "$CREATE_OUTPUT" | grep -oP "database_id = \"\K[^\"]+")
if [ -z "$DATABASE_ID" ]; then
DATABASE_ID=$(echo "$CREATE_OUTPUT" | grep -oP "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})")
fi
else
LIST_OUTPUT=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
DATABASE_ID=$(echo "$LIST_OUTPUT" | jq -r '.[] | select(.name=="api-worker") | .uuid')
fi
if [ -z "$DATABASE_ID" ]; then
echo "❌ 无法创建或找到 D1 数据库"
exit 1
fi
sed -i -E "s/(database_id = \")([^\"]+)(\")/\\1$DATABASE_ID\\3/" apps/worker/wrangler.toml
echo "database_id=$DATABASE_ID" >> $GITHUB_ENV
- name: Apply D1 migrations (remote)
if: steps.check-db.outputs.db_exists != 'true'
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 to Cloudflare Workers (SPA Mode)
if: steps.check-db.outputs.db_exists != 'true' && (needs.check-config.outputs.deploy_worker == 'true' || needs.check-config.outputs.deploy_ui == 'true')
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: bunx wrangler deploy --config apps/worker/wrangler.toml
deploy-spa:
needs: check-config
if: needs.check-config.outputs.should_deploy == 'true'
if: needs.check-config.outputs.should_deploy == 'true' && needs.check-config.outputs.deploy_action != 'init'
runs-on: ubuntu-latest
steps:
@@ -183,6 +306,7 @@ jobs:
- name: Show deploy plan
run: |
echo "deploy_action=${{ needs.check-config.outputs.deploy_action }}"
echo "deploy_target=${{ needs.check-config.outputs.deploy_target }}"
echo "apply_migrations=${{ needs.check-config.outputs.apply_migrations }}"
echo "ui_changed=${{ needs.check-config.outputs.ui_changed }}"
@@ -232,13 +356,13 @@ jobs:
run: |
echo "检查并创建 D1 数据库..."
DATABASE_LIST=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
EXISTING_DB=$(echo "$DATABASE_LIST" | jq -r '.[] | select(.name=="new_api_lite") | .uuid')
EXISTING_DB=$(echo "$DATABASE_LIST" | jq -r '.[] | select(.name=="api-worker") | .uuid')
if [ -n "$EXISTING_DB" ]; then
echo "✅ 找到现有 D1 数据库: $EXISTING_DB"
DATABASE_ID=$EXISTING_DB
else
CREATE_OUTPUT=$(bunx wrangler d1 create new_api_lite --config apps/worker/wrangler.toml 2>&1)
CREATE_OUTPUT=$(bunx wrangler d1 create api-worker --config apps/worker/wrangler.toml 2>&1)
if echo "$CREATE_OUTPUT" | grep -q "database_id"; then
DATABASE_ID=$(echo "$CREATE_OUTPUT" | grep -oP "database_id = \"\K[^\"]+")
@@ -247,7 +371,7 @@ jobs:
fi
else
LIST_OUTPUT=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
DATABASE_ID=$(echo "$LIST_OUTPUT" | jq -r '.[] | select(.name=="new_api_lite") | .uuid')
DATABASE_ID=$(echo "$LIST_OUTPUT" | jq -r '.[] | select(.name=="api-worker") | .uuid')
if [ -z "$DATABASE_ID" ]; then
echo "❌ 无法创建或找到 D1 数据库"
+3 -2
View File
@@ -47,7 +47,7 @@
- Checkout → 安装 Bun 1.3.9 → `bun install`
- 构建管理台(`apps/ui/dist`),并校验产物
- 关闭 wrangler telemetry
- 创建/校验 D1(`new_api_lite`),在 CI 内写入 `apps/worker/wrangler.toml` 的 `database_id`
- 创建/校验 D1(`api-worker`),在 CI 内写入 `apps/worker/wrangler.toml` 的 `database_id`
- 按需执行远程迁移
- `wrangler deploy` 部署 Worker
@@ -86,7 +86,7 @@ bun run dev:worker
- 日志保留天数(默认 30)
- 会话时长(小时,默认 12)
- 管理员密码(首次登录自动初始化,可在系统设置中修改)
- 管理员密码(首次登录在登录页输入密码将自动设置,可在系统设置中修改)
### 3) 启动管理台 UI
@@ -181,3 +181,4 @@ bun run --filter api-worker db:migrate
- `ALL /v1/*` 转发到渠道 `base_url + 请求路径`
- 当请求路径为 `/v1/responses` 且上游 400/404 时自动回退到 `/responses`
- 流式请求会自动补齐 `stream_options.include_usage = true` 以收集用量
+7
View File
@@ -3,10 +3,16 @@ CREATE TABLE IF NOT EXISTS channels (
name TEXT NOT NULL,
base_url TEXT NOT NULL,
api_key TEXT NOT NULL,
type INTEGER NOT NULL DEFAULT 1,
group_name TEXT,
priority INTEGER NOT NULL DEFAULT 0,
weight INTEGER NOT NULL DEFAULT 1,
status TEXT NOT NULL DEFAULT 'active',
rate_limit INTEGER DEFAULT 0,
models_json TEXT,
metadata_json TEXT,
test_time INTEGER,
response_time_ms INTEGER,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
@@ -16,6 +22,7 @@ CREATE TABLE IF NOT EXISTS tokens (
name TEXT NOT NULL,
key_hash TEXT NOT NULL,
key_prefix TEXT NOT NULL,
token_plain TEXT,
quota_total INTEGER,
quota_used INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'active',
@@ -1 +0,0 @@
ALTER TABLE tokens ADD COLUMN token_plain TEXT;
@@ -1,6 +0,0 @@
ALTER TABLE channels ADD COLUMN type INTEGER NOT NULL DEFAULT 1;
ALTER TABLE channels ADD COLUMN group_name TEXT;
ALTER TABLE channels ADD COLUMN priority INTEGER NOT NULL DEFAULT 0;
ALTER TABLE channels ADD COLUMN metadata_json TEXT;
ALTER TABLE channels ADD COLUMN test_time INTEGER;
ALTER TABLE channels ADD COLUMN response_time_ms INTEGER;
@@ -1,3 +0,0 @@
ALTER TABLE usage_logs ADD COLUMN first_token_latency_ms INTEGER;
ALTER TABLE usage_logs ADD COLUMN stream INTEGER;
ALTER TABLE usage_logs ADD COLUMN reasoning_effort TEXT;
+2 -1
View File
@@ -9,7 +9,7 @@ PROXY_RETRY_DELAY_MS = "200"
[[d1_databases]]
binding = "DB"
database_name = "new_api_lite"
database_name = "api-worker"
database_id = "00000000-0000-0000-0000-000000000000"
[assets]
@@ -19,3 +19,4 @@ not_found_handling = "single-page-application"
run_worker_first = true
+33 -3
View File
@@ -3,6 +3,7 @@
## [Unreleased]
### 微调
- **[admin-ui]**: 处理剪贴板异常时忽略未使用的错误变量
- 类型: 微调(无方案包)
- 文件: apps/ui/src/App.tsx:500
@@ -85,15 +86,31 @@
- 类型: 微调(无方案包)
- 文件: apps/worker/src/routes/proxy.ts
## [0.4.10] - 2026-02-21
### 变更
- **[ci]**: D1 数据库名称调整为 `api-worker`
- 方案: [202602212345_db-name-api-worker](archive/2026-02/202602212345_db-name-api-worker/)
## [0.4.9] - 2026-02-21
### 变更
- **[ci]**: 部署流程新增 init 动作并仅首次初始化
- 方案: [202602212329_deploy-init-action](archive/2026-02/202602212329_deploy-init-action/)
## [0.4.8] - 2026-02-21
### 变更
- **[ci]**: 部署流程支持按变更范围选择前端/后端并默认按迁移变更执行
- 方案: [202602211446_deploy-workflow-auto](archive/2026-02/202602211446_deploy-workflow-auto/)
## [0.4.7] - 2026-02-21
### 变更
- **[tooling]**: UI 目录从 apps/admin 迁移为 apps/ui 并同步配置
- 方案: [202602211405_rename-admin-ui](archive/2026-02/202602211405_rename-admin-ui/)
- **[ci]**: 部署流程改用 apps/ui 与 api-worker-ui
@@ -104,6 +121,7 @@
## [0.4.6] - 2026-02-21
### 变更
- **[tooling]**: 统一工作区包名与脚本为 api-worker / api-worker-ui
- 方案: [202602161825_rename-api-worker-ui](archive/2026-02/202602161825_rename-api-worker-ui/)
- **[worker]**: Worker 部署名称更新为 api-worker
@@ -114,12 +132,14 @@
## [0.4.5] - 2026-02-16
### 变更
- **[admin-ui]**: 令牌管理改为列表视图并支持分页弹窗创建
- 方案: [202602161600_token-list-ui](archive/2026-02/202602161600_token-list-ui/)
## [0.4.4] - 2026-02-16
### 修复
- **[admin-ui]**: 使用日志默认每页 50 条并修正本地时间显示
- 方案: [202602161433_usage-log-fixes](archive/2026-02/202602161433_usage-log-fixes/)
- **[usage/proxy]**: 使用日志补充首 token 延迟、流式与推理强度记录
@@ -128,18 +148,21 @@
## [0.4.3] - 2026-02-16
### 变更
- **[admin-ui]**: 使用日志支持分页与指标拆分展示
- 方案: [202602161355_usage-view-metrics](archive/2026-02/202602161355_usage-view-metrics/)
## [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
### 变更
- **[admin-ui]**: 管理台入口拆分为模块、扁平化 features,并将 AppShell 调整为 AppLayout
- 方案: [202602152325_admin-ui-modularize](archive/2026-02/202602152325_admin-ui-modularize/)
- 决策: admin-ui-modularize#D001(功能域拆分)
@@ -147,18 +170,21 @@
## [0.4.0] - 2026-02-15
### 新增
- **[channels]**: New API 标签批量权重/启用/停用接口
- 方案: [202602152211_newapi-tag-sync](archive/2026-02/202602152211_newapi-tag-sync/)
## [0.3.1] - 2026-02-15
### 修复
- **[proxy]**: 增强 usage 解析以修复使用日志与数据面板 token 统计为 0
- 方案: [202602151843_fix-usage-tokens](archive/2026-02/202602151843_fix-usage-tokens/)
## [0.3.0] - 2026-02-15
### 变更
- **[admin-ui]**: 管理台改为 Hono + TSX DOM 渲染并接入 Tailwind v4
- 方案: [202602151628_admin-ui-hono-tsx-tailwind](archive/2026-02/202602151628_admin-ui-hono-tsx-tailwind/)
- 决策: admin-ui-hono-tsx-tailwind#D001(采用 Hono JSX DOM + Tailwind)
@@ -166,12 +192,14 @@
## [0.2.1] - 2026-02-15
### 变更
- **[tooling]**: 切换为 Bun 作为包管理器,补充部署说明与 fix 命令
- 方案: [202602150153_bun-tooling](archive/2026-02/202602150153_bun-tooling/)
## [0.2.0] - 2026-02-15
### 新增
- **[channels/auth/models]**: 新增 New API 兼容渠道管理接口、用户模型接口与管理员令牌鉴权
- 方案: [202602150127_newapi-channel-compat](archive/2026-02/202602150127_newapi-channel-compat/)
- 决策: newapi-channel-compat#D001(新增兼容层并保留扩展字段)
@@ -179,22 +207,24 @@
## [0.1.0] - 2026-02-14
### 新增
- **[核心服务]**: 初始化 Worker + D1 后端与 Vite 管理台,提供渠道/模型/令牌/日志/面板与 OpenAI 兼容代理
- 方案: [202602142217_new-api-lite](archive/2026-02/202602142217_new-api-lite/)
- 决策: new-api-lite#D001(单 Worker + Hono), new-api-lite#D002(Vite + Pages), new-api-lite#D003(Token 默认全渠道), new-api-lite#D004(日志保留可配置)
### 修复
- **[{模块名}]**: {修复描述}
- 方案: [{YYYYMMDDHHMM}_{fix}](archive/{YYYY-MM}/{YYYYMMDDHHMM}_{fix}/)
- 方案: [{YYYYMMDDHHMM}\_{fix}](archive/{YYYY-MM}/{YYYYMMDDHHMM}_{fix}/)
### 微调
- **[{模块名}]**: {微调描述}
- 类型: 微调(无方案包)
- 文件: {文件路径}:{行号范围}
### 回滚
- **[{模块名}]**: 回滚至 {版本/提交}
- 原因: {回滚原因}
- 方案: [{原方案包}](archive/{YYYY-MM}/{原方案包}/)
+6 -3
View File
@@ -16,9 +16,9 @@
## 知识库状态
```yaml
最后更新: 2026-02-21 14:18
模块数量: 9
待执行方案: 0
最后更新: 2026-02-21 23:47
模块数量: 10
待执行方案: 9
```
## 读取指引
@@ -36,3 +36,6 @@
```
+2
View File
@@ -15,6 +15,7 @@
| dashboard | 数据面板统计 | 🚧 | [dashboard.md](./dashboard.md) |
| settings | 系统配置 | 🚧 | [settings.md](./settings.md) |
| admin-ui | 管理台前端 | 🚧 | [admin-ui.md](./admin-ui.md) |
| deploy-workflow | GitHub Actions 部署流程 | 🚧 | [deploy-workflow.md](./deploy-workflow.md) |
## 模块依赖关系
@@ -24,6 +25,7 @@ proxy → channels, tokens, usage
dashboard → usage, tokens
models → channels
usage → settings
deploy-workflow → admin-ui, worker
```
## 状态说明
+24
View File
@@ -0,0 +1,24 @@
# deploy-workflow
## 职责
GitHub Actions 部署流程,负责区分 update/init 动作,控制 UI/Worker 构建、D1 初始化与部署。
## 行为规范
### 初始化部署
**条件**: workflow_dispatch/repository_dispatch 且 `deploy_action=init`,`should_deploy=true`,D1 数据库不存在
**行为**: 创建 D1 数据库 → 远程迁移 → 部署 Worker(含 UI 构建)
**结果**: 完成首次初始化部署
### 常规更新
**条件**: `deploy_action=update` 或 push 自动触发
**行为**: 按变更范围选择前端/后端部署,必要时执行迁移
**结果**: 日常更新发布
## 依赖关系
```yaml
依赖: admin-ui, worker
被依赖: -
```