ci: 优化 Jenkins 构建产物打包

This commit is contained in:
sunlei 2026-05-16 03:25:22 +08:00
parent cbac76ea3d
commit e331b26314
5 changed files with 20 additions and 13 deletions

View File

@ -1,5 +1,4 @@
node_modules
dist
coverage
logs
*.log

12
Jenkinsfile vendored
View File

@ -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())
}
}
}
}

View File

@ -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 # 单元测试

View File

@ -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"]

View File

@ -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",