feat: 构建优化+备份机制

- Docker 多阶段构建(减小镜像体积)
- HEALTHCHECK 健康检查
- 构建后等待健康状态而非日志匹配
- 数据库备份脚本(mysqldump/容器内双模式)
- 30天自动清理旧备份
This commit is contained in:
Agent
2026-07-14 23:24:18 +08:00
parent 0eef797cae
commit 0dfe699cee
2 changed files with 72 additions and 6 deletions
+20 -6
View File
@@ -53,18 +53,32 @@ BACKUP_IMAGE="$SERVICE_NAME:last-good"
cd "$WORKSPACE"
# 生成 Dockerfile
# 生成 Dockerfile(多阶段构建)
cat > Dockerfile <<'EOF'
# ---- 构建阶段 ----
FROM node:22-slim AS builder
WORKDIR /build
COPY backend/package*.json ./
RUN npm ci --only=production --registry=https://registry.npmmirror.com
# ---- 运行阶段 ----
FROM node:22-slim
WORKDIR /app
RUN apt-get update -qq && apt-get install -y -qq --no-install-recommends curl tzdata && \
rm -rf /var/lib/apt/lists/* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 只复制需要的文件
COPY --from=builder /build/node_modules ./node_modules
COPY backend/src ./src
COPY frontend/dist ./dist
COPY backend ./backend
COPY backend/package.json ./package.json
RUN cd backend && npm install --production && cd ..
EXPOSE 3000
ENV NODE_ENV=production
ENV TZ=Asia/Shanghai
CMD ["node", "backend/src/index.js"]
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/api/health',r=>{process.exit(r.statusCode===200?0:1)}).on('error',()=>process.exit(1))"
CMD ["node", "src/index.js"]
EOF
echo "构建 Docker 镜像: $IMAGE_NAME ..."
@@ -100,7 +114,7 @@ docker run -d \
echo "新容器已启动,等待应用启动(最多 ${HEALTH_WAIT} 秒)..."
for i in $(seq 1 $HEALTH_WAIT); do
if docker logs "$CONTAINER_NAME" 2>&1 | grep -qE "(running at|Database connected)"; then
if docker ps --filter "name=$CONTAINER_NAME" --format "{{.Status}}" | grep -q "(healthy)"; then
echo "✅ 应用启动成功!"
echo "🎉 部署完成!访问 http://110.42.189.169:${HOST_PORT}"
docker logs --tail 30 "$CONTAINER_NAME"