ci(deploy): 支持 init 部署并统一 D1 初始化
This commit is contained in:
1 parent
7a6d51cc9c
commit
a5be552a67
11 files changed
+206
-24
No files matched your search
@@ -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 数据库"
|
||||
|
||||
Reference in new issue
Block a user