feat: Dockerfile + CI/CD deploy workflow + @prisma/client 移入 production deps
Some checks failed
Deploy API Server / build-and-deploy (push) Failing after 10m2s

This commit is contained in:
WangDL 2026-05-09 19:05:48 +08:00
parent ef7c1f1bc9
commit 07d6b889ef
4 changed files with 81 additions and 2 deletions

7
.dockerignore Normal file
View File

@ -0,0 +1,7 @@
node_modules
dist
.env
.git
*.md
test
服务器密钥

View File

@ -0,0 +1,45 @@
name: Deploy API Server
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t zhixi-api:latest .
- name: Stop old container
run: docker stop zhixi-api 2>/dev/null || true
- name: Remove old container
run: docker rm zhixi-api 2>/dev/null || true
- name: Start new container
run: |
docker run -d \
--name zhixi-api \
--network zhixi-net \
--restart unless-stopped \
-p 3001:3000 \
-e NODE_ENV=production \
-e PORT=3000 \
-e DATABASE_URL="mysql://zhixi_user:Zhixi@2026!App@mysql-zhixi:3306/zhixi" \
-e REDIS_HOST=redis-zhixi \
-e REDIS_PORT=6379 \
-e JWT_SECRET=98b1e7e377a40021ad7c46c55e467d2a218a89db7afc7c912780152ad64bdc45 \
-e AI_PROVIDER=mock \
-e ENABLE_SWAGGER=false \
zhixi-api:latest
- name: Wait for startup
run: sleep 5
- name: Health check
run: |
curl -f http://localhost:3001/health || (docker logs zhixi-api --tail 30 && exit 1)

27
Dockerfile Normal file
View File

@ -0,0 +1,27 @@
FROM node:22-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY tsconfig.json tsconfig.build.json nest-cli.json ./
COPY prisma ./prisma
COPY src ./src
RUN npx prisma generate
RUN npm run build
RUN npm prune --production
FROM node:22-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/package.json ./
EXPOSE 3000
CMD ["node", "dist/main.js"]

View File

@ -39,7 +39,8 @@
"passport-jwt": "^4.0.1",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.1"
"swagger-ui-express": "^5.0.1",
"@prisma/client": "^5.22.0"
},
"devDependencies": {
"@eslint/eslintrc": "^3.2.0",
@ -47,7 +48,6 @@
"@nestjs/cli": "^11.0.0",
"@nestjs/schematics": "^11.0.0",
"@nestjs/testing": "^11.0.1",
"@prisma/client": "^5.22.0",
"@types/bcryptjs": "^2.4.6",
"@types/express": "^5.0.0",
"@types/jest": "^30.0.0",