From 07d6b889ef61eba7e224d70fc5bd5f9db4fba7e8 Mon Sep 17 00:00:00 2001 From: WangDL Date: Sat, 9 May 2026 19:05:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Dockerfile=20+=20CI/CD=20deploy=20workf?= =?UTF-8?q?low=20+=20@prisma/client=20=E7=A7=BB=E5=85=A5=20production=20de?= =?UTF-8?q?ps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 7 ++++++ .gitea/workflows/deploy.yml | 45 +++++++++++++++++++++++++++++++++++++ Dockerfile | 27 ++++++++++++++++++++++ package.json | 4 ++-- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/deploy.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..db71cf6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +node_modules +dist +.env +.git +*.md +test +服务器密钥 diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..6dfd0d7 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -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) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..96f2490 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/package.json b/package.json index 30ef1b5..d07c240 100644 --- a/package.json +++ b/package.json @@ -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",