build(docker): 重构 Dockerfile 并简化 CI 流程
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
- 重构 Dockerfile,使用多阶段构建以减小最终镜像大小 - 更新 .drone.yml,使用单一步骤完成构建和
This commit is contained in:
parent
ab0f0a873a
commit
d42c83c9e9
39
.drone.yml
39
.drone.yml
@ -3,37 +3,14 @@ type: docker
|
|||||||
name: default
|
name: default
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: git-clone
|
- name: build-and-push
|
||||||
image: alpine/git
|
image: plugins/docker
|
||||||
commands:
|
settings:
|
||||||
- git clone http://120.48.5.80:3000/lixin/automated_api.git
|
repo: your-registry/automated-api
|
||||||
- cd automated_api
|
tags:
|
||||||
|
- latest
|
||||||
- name: setup-pnpm
|
- ${DRONE_COMMIT_SHA:0:8}
|
||||||
image: node:22-alpine
|
registry: your-registry
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
48
Dockerfile
48
Dockerfile
@ -1,37 +1,31 @@
|
|||||||
FROM node:lts-alpine
|
# 构建阶段
|
||||||
|
FROM node:18-alpine as builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# 配置alpine国内镜像加速
|
COPY package.json .
|
||||||
RUN sed -i "s@http://dl-cdn.alpinelinux.org/@https://repo.huaweicloud.com/@g" /etc/apk/repositories
|
COPY package-lock.json* .
|
||||||
|
|
||||||
# 安装tzdata,默认的alpine基础镜像不包含时区组件,安装后可通过TZ环境变量配置时区
|
RUN npm install
|
||||||
RUN apk add --no-cache tzdata
|
|
||||||
|
|
||||||
# 设置时区为中国东八区,这里的配置可以被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 . .
|
COPY . .
|
||||||
RUN pnpm run build
|
|
||||||
|
|
||||||
# 删除开发期依赖
|
RUN npm run build
|
||||||
RUN rm -rf node_modules && rm pnpm-lock.yaml
|
|
||||||
# 安装生产环境依赖
|
# 运行阶段
|
||||||
RUN pnpm install --production
|
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
|
EXPOSE 8001
|
||||||
|
|
||||||
CMD ["pnpm", "start"]
|
CMD ["node", "bootstrap.js"]
|
Loading…
Reference in New Issue
Block a user