name: Deploy SPA CF Workers[Worker一体化部署] on: push: branches: [main, master] paths: - "apps/admin/**" - "apps/worker/**" workflow_dispatch: inputs: from_panel: description: "是否由部署控制面板触发 / triggered from deployment control panel" required: false default: "false" repository_dispatch: types: [deploy-spa-button] jobs: check-config: runs-on: ubuntu-latest outputs: should_deploy: ${{ steps.check.outputs.should_deploy }} steps: - name: 📥 检出代码 uses: actions/checkout@v4 - name: 🔍 检查部署配置 id: check run: | if [[ "${{ github.event_name }}" == "repository_dispatch" ]]; then echo "should_deploy=true" >> $GITHUB_OUTPUT echo "✅ 手动触发(repository_dispatch),忽略开关,允许部署" exit 0 fi if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.from_panel }}" != "true" ]]; then echo "should_deploy=true" >> $GITHUB_OUTPUT echo "✅ 手动触发(workflow_dispatch),忽略开关,允许部署" exit 0 fi SPA_ENABLED="${{ vars.SPA_DEPLOY }}" [ -z "$SPA_ENABLED" ] && SPA_ENABLED="true" if [ "$SPA_ENABLED" = "true" ]; then echo "should_deploy=true" >> $GITHUB_OUTPUT echo "✅ SPA 自动部署已开启,允许部署" else echo "should_deploy=false" >> $GITHUB_OUTPUT echo "⏸️ SPA 自动部署已关闭,跳过部署" fi deploy-spa: needs: check-config if: needs.check-config.outputs.should_deploy == 'true' 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: Check if deploy button trigger id: check-deploy-button run: | if [[ "${{ github.event_name }}" == "repository_dispatch" && "${{ github.event.action }}" == "deploy-spa-button" ]]; then echo "is_deploy_button=true" >> $GITHUB_OUTPUT else echo "is_deploy_button=false" >> $GITHUB_OUTPUT fi - name: Install dependencies run: bun install - name: Build admin run: bun run --filter new-api-lite-admin build - name: Verify admin build run: | if [ ! -d "apps/admin/dist" ]; then echo "❌ 管理台构建失败:dist 目录不存在" exit 1 fi if [ ! -f "apps/admin/dist/index.html" ]; then echo "❌ 管理台构建失败:index.html 不存在" exit 1 fi echo "✅ 管理台构建产物已就绪" - name: Disable wrangler telemetry run: bunx wrangler telemetry disable - name: Create or verify D1 Database 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=="new_api_lite") | .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) 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=="new_api_lite") | .uuid') if [ -z "$DATABASE_ID" ]; then echo "❌ 无法创建或找到 D1 数据库" exit 1 fi fi 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) 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) env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} run: bunx wrangler deploy --config apps/worker/wrangler.toml - name: Display Success Information if: steps.check-deploy-button.outputs.is_deploy_button == 'true' && success() run: | echo "====================================================" echo "🎉 SPA Worker 已成功部署到 Cloudflare Workers!" echo "====================================================" echo "后续步骤:" echo "1. 访问您的 Worker URL" echo "2. 登录后立刻修改管理员密码" echo "3. 配置相关系统参数" echo "====================================================" - name: Notify deployment status if: always() run: | if [ "${{ job.status }}" == "success" ]; then echo "✅ SPA Worker 部署成功!" else echo "❌ SPA Worker 部署失败!" fi