diff --git a/.dockerignore b/.dockerignore index 1e9c8b5..7002228 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,4 @@ node_modules -dist coverage logs *.log diff --git a/Jenkinsfile b/Jenkinsfile index f813e7f..b1035a6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -156,7 +156,17 @@ pipeline { } steps { script { - runCmd("docker build -f dockerfile -t ${env.DOCKER_IMAGE} .") + if (isUnix()) { + runCmd(""" + test -f dist/main.js + docker build -f dockerfile -t ${env.DOCKER_IMAGE} . + """.stripIndent()) + } else { + runCmd('', """ + if not exist dist\\main.js exit /b 1 + docker build -f dockerfile -t ${env.DOCKER_IMAGE} . + """.stripIndent()) + } } } } diff --git a/README.md b/README.md index b8c0a74..7a693f1 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ pnpm start:dev ```bash pnpm start # 普通启动 -pnpm start:prod # 构建后按 production 环境启动 +pnpm start:prod # 按 production 环境运行已构建的 dist/main pnpm run build # Nest 构建 pnpm run lint # ESLint 检查 pnpm test # 单元测试 diff --git a/dockerfile b/dockerfile index 526ca62..fb581ec 100644 --- a/dockerfile +++ b/dockerfile @@ -1,21 +1,19 @@ -# 引用 Node 22 官方 Debian slim 镜像,避免非官方 tag 在镜像源里解析失败 FROM node:22-bookworm-slim -# 指定工作目录 WORKDIR /app -# 先拷贝依赖清单,利用 Docker 缓存加速依赖安装 +ENV NODE_ENV=production + COPY package.json pnpm-lock.yaml ./ -# 项目使用 pnpm-lock.yaml,镜像内也统一使用 pnpm 安装依赖 +# 生产镜像只安装运行依赖,dist 由 Jenkins Build stage 提前产出。 RUN corepack enable \ && corepack prepare pnpm@9 --activate \ - && pnpm install --frozen-lockfile + && pnpm install --prod --frozen-lockfile -# 拷贝业务代码 -COPY . . +# dist 由 Jenkins 的 Build stage 生成,这里只打包运行产物。 +COPY dist ./dist -# 声明暴露端口号 EXPOSE 48085 -CMD ["pnpm", "run", "start:prod"] +CMD ["node", "dist/main"] diff --git a/package.json b/package.json index 9069148..4739e00 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", - "start:prod": "nest build && cross-env NODE_ENV=production node dist/main", + "start:prod": "cross-env NODE_ENV=production node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\"", "lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest",