wangdl 98e442e666
Some checks failed
Deploy API Server / build-and-deploy (push) Failing after 23s
fix(M-AI-02-GATE): restore physical column names, composite UNIQUE, lifecycle spec, migrate deploy
Section 2: @map("errorMessage") + @map("completedAt") decouple logical from physical names.
  Forward-fix migration reverts physical columns to original names.
Section 3: jobType VARCHAR(64) → VARCHAR(32) restored.
Section 4: lifecycleStatus mapping restored to spec (pending→queued, processing→running,
  completed→succeeded, failed→failed). Enum: created|queued|running|retrying|succeeded|
  failed|cancel_requested|cancelled.
Section 5: idempotencyKey UNIQUE changed from single-column to composite
  (userId, jobType, idempotencyKey). MySQL allows multiple NULLs.
Section 6: triggerType backfill now documents evidence basis per record.
Section 7: CI deploy.yml uses prisma migrate deploy (not db push).
  Migration baseline established (28 rows in _prisma_migrations).

prisma migrate diff: No difference detected.
332 tests pass.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-20 12:39:56 +08:00

138 lines
5.3 KiB
YAML

name: Deploy API Server
on:
push:
branches: [main]
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
jobs:
build-and-deploy:
runs-on: prod
steps:
- name: Checkout latest code
run: |
if [ -d /tmp/api-server ]; then
cd /tmp/api-server && git fetch origin && git reset --hard origin/main
else
git clone http://10.2.0.7:3000/wangdl/api-server.git /tmp/api-server
fi
- name: Install dependencies
run: |
cd /tmp/api-server
npm ci
- name: Build
run: |
cd /tmp/api-server
npx prisma generate
npm run build
- name: Ensure infrastructure is ready
run: |
docker start mysql redis qdrant 2>/dev/null || true
sleep 2
- name: Resolve failed migrations
run: |
MYSQL_CMD="docker exec mysql mysql -u ${{ secrets.DB_USER }} -p${{ secrets.DB_PASSWORD }} ${{ secrets.DB_NAME }}"
FAILED=$($MYSQL_CMD -N -e \
"SELECT migration_name FROM _prisma_migrations WHERE logs LIKE '%failed%' LIMIT 1;" 2>/dev/null || true)
if [ -n "$FAILED" ]; then
echo "[deploy] Found failed migration: $FAILED, cleaning up..."
$MYSQL_CMD -e "DROP TABLE IF EXISTS AiUsageLog;" 2>/dev/null || true
$MYSQL_CMD -e "DROP TABLE IF EXISTS WaitlistEntry;" 2>/dev/null || true
$MYSQL_CMD -e "DROP TABLE IF EXISTS ModelRoute;" 2>/dev/null || true
$MYSQL_CMD -e "DROP TABLE IF EXISTS ProviderConfig;" 2>/dev/null || true
$MYSQL_CMD -e "DROP TABLE IF EXISTS FallbackEvent;" 2>/dev/null || true
$MYSQL_CMD -e "DROP TABLE IF EXISTS ViolationRecord;" 2>/dev/null || true
$MYSQL_CMD -e "DROP TABLE IF EXISTS UserDevice;" 2>/dev/null || true
$MYSQL_CMD -e "DROP TABLE IF EXISTS AccountDeletionRequest;" 2>/dev/null || true
$MYSQL_CMD -e "ALTER TABLE UploadedFile DROP COLUMN objectKey;" 2>/dev/null || true
$MYSQL_CMD -e "ALTER TABLE UploadedFile DROP COLUMN bucket;" 2>/dev/null || true
$MYSQL_CMD -e "DROP INDEX UploadedFile_objectKey_idx ON UploadedFile;" 2>/dev/null || true
$MYSQL_CMD -e "DELETE FROM _prisma_migrations WHERE migration_name = '$FAILED';"
echo "[deploy] Cleaned up failed migration $FAILED"
else
echo "[deploy] No failed migrations found"
fi
- name: Apply database migrations
run: |
cd /tmp/api-server
npx prisma migrate deploy
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
- name: Deploy NestJS API
run: |
sudo rsync -av --delete \
/tmp/api-server/dist/ /opt/zhixi/backend/dist/
sudo rsync -av --delete \
/tmp/api-server/node_modules/ /opt/zhixi/backend/node_modules/
sudo rsync -av \
/tmp/api-server/prisma/ /opt/zhixi/backend/prisma/
sudo rsync -av \
/tmp/api-server/package.json /opt/zhixi/backend/package.json
- name: Generate Prisma client
run: |
cd /opt/zhixi/backend && npx prisma generate
- name: Restart API service
run: |
sudo systemctl reset-failed zhixi-api 2>/dev/null || true
sudo systemctl restart zhixi-api
sleep 3
if curl -sf http://localhost:3000/api > /dev/null 2>&1; then
echo "[deploy] API health OK"
else
echo "[deploy] Health check failed — checking logs:"
sudo journalctl -u zhixi-api --no-pager -n 20
exit 1
fi
- name: Deploy NestJS Worker
run: |
cd /opt/zhixi/backend
sudo cp deploy/zhixi-nest-worker.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl reset-failed zhixi-nest-worker 2>/dev/null || true
sudo systemctl restart zhixi-nest-worker
sleep 5
echo "[deploy] Worker status: $(sudo systemctl is-active zhixi-nest-worker)"
echo "[deploy] Worker startup log:"
sudo journalctl -u zhixi-nest-worker --no-pager -n 10
- name: Worker 集成测试 (BullMQ/MySQL/Redis 真实验证)
run: |
cd /tmp/api-server
bash test/run-integration-tests.sh
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
REDIS_HOST: 127.0.0.1
REDIS_PORT: '6379'
REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }}
JWT_SECRET: test-integration-secret
INTERNAL_API_KEY: test-key
CREDENTIAL_ENCRYPTION_KEY: ${{ secrets.CREDENTIAL_ENCRYPTION_KEY }}
STORAGE_DRIVER: local
MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_ROOT_PASSWORD }}
timeout-minutes: 10
- name: Deploy RAG Worker
run: |
set -e
WORKER_DIR="/opt/zhixi/backend/rag-worker"
mkdir -p "$WORKER_DIR"
rsync -av --delete --exclude='.env' --exclude='__pycache__' \
/tmp/api-server/rag-worker/ "$WORKER_DIR/"
sudo cp "$WORKER_DIR/zhixi-worker.service" /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl restart zhixi-worker
sleep 5
sudo systemctl is-active zhixi-worker
echo "[deploy] zhixi-worker active OK"