build(docker): 重构 Dockerfile 并简化 CI 流程
Some checks failed
continuous-integration/drone/push Build is failing

- 重构 Dockerfile,使用多阶段构建以减小最终镜像大小
- 更新 .drone.yml,使用单一步骤完成构建和
This commit is contained in:
lixin 2025-01-10 14:37:00 +08:00
parent ab0f0a873a
commit d42c83c9e9
2 changed files with 30 additions and 59 deletions

View File

@ -3,37 +3,14 @@ type: docker
name: default
steps:
- name: git-clone
image: alpine/git
commands:
- git clone http://120.48.5.80:3000/lixin/automated_api.git
- cd automated_api
- name: setup-pnpm
image: node:22-alpine
commands:
- npm install -g pnpm
- pnpm config set registry https://registry.npmmirror.com
- name: install-deps
image: node:22-alpine
commands:
- pnpm install
volumes:
- name: node_modules
path: /app/node_modules
- name: build
image: node:22-alpine
commands:
- pnpm run build
- name: docker-build
image: plugins/docker
settings:
dockerfile: Dockerfile
repo: rz/automated_api
tags: latest
- name: build-and-push
image: plugins/docker
settings:
repo: your-registry/automated-api
tags:
- latest
- ${DRONE_COMMIT_SHA:0:8}
registry: your-registry

View File

@ -1,37 +1,31 @@
FROM node:lts-alpine
# 构建阶段
FROM node:18-alpine as builder
WORKDIR /app
# 配置alpine国内镜像加速
RUN sed -i "s@http://dl-cdn.alpinelinux.org/@https://repo.huaweicloud.com/@g" /etc/apk/repositories
COPY package.json .
COPY package-lock.json* .
# 安装tzdata,默认的alpine基础镜像不包含时区组件安装后可通过TZ环境变量配置时区
RUN apk add --no-cache tzdata
RUN npm install
# 设置时区为中国东八区这里的配置可以被docker-compose.yml或docker run时指定的时区覆盖
ENV TZ="Asia/Shanghai"
# 如果各公司有自己的私有源可以替换registry地址,如使用官方源注释下一行
RUN npm config set registry https://registry.npmmirror.com
# 安装 pnpm 和全局依赖
RUN npm install -g pnpm
RUN pnpm config set registry https://registry.npmmirror.com
# 安装开发期依赖
COPY package.json ./package.json
RUN pnpm install
# 构建项目
COPY . .
RUN pnpm run build
# 删除开发期依赖
RUN rm -rf node_modules && rm pnpm-lock.yaml
# 安装生产环境依赖
RUN pnpm install --production
RUN npm run build
# 运行阶段
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json .
COPY --from=builder /app/bootstrap.js .
RUN npm install --production
ENV NODE_ENV=production
ENV PORT=8001
# 如果端口更换,这边可以更新一下
EXPOSE 8001
CMD ["pnpm", "start"]
CMD ["node", "bootstrap.js"]