ci(drone): Refactor deployment pipeline with Docker and containerization
Some checks failed
continuous-integration/drone/push Build is failing

- Updated .drone.yml to use Docker plugin for building and pushing images
- Simplified deployment steps by using Docker run command
- Added Docker volume mounting for persistent data
- Streamlined Dockerfile to improve build process
- Configured image registry and tagging strategy
This commit is contained in:
lixin 2025-01-25 10:17:08 +08:00
parent 6bd32f5669
commit 29a51d37c8
2 changed files with 52 additions and 60 deletions

View File

@ -2,7 +2,6 @@ kind: pipeline
type: docker
name: nodejs-deploy
# 使用国内镜像加速
volumes:
- name: node_modules
host:
@ -12,44 +11,48 @@ volumes:
path: /usr/local/blackend/pnpm_cache
steps:
- name: setup
image: node:22-alpine
depends_on: [clone]
- name: build-docker
image: plugins/docker
settings:
dockerfile: Dockerfile
repo: your-registry/nodejs-app
tags:
- latest
- ${DRONE_COMMIT_SHA:0:8}
registry: registry.cn-hangzhou.aliyuncs.com # 替换为您的镜像仓库地址
username:
from_secret: docker_username
password:
from_secret: docker_password
volumes:
- name: node_modules
path: /drone/src/node_modules
- name: pnpm_cache
path: /root/.local/share/pnpm/store
commands:
# 配置镜像源
- npm config set registry https://registry.npmmirror.com
- npm install -g pnpm
- pnpm config set registry https://registry.npmmirror.com
# 安装依赖
- pnpm install
- name: build
image: node:22-alpine
depends_on: [setup]
volumes:
- name: node_modules
path: /drone/src/node_modules
commands:
# 构建项目
- pnpm run build
- name: deploy
image: node:22-alpine
depends_on: [build]
volumes:
- name: node_modules
path: /drone/src/node_modules
image: docker
commands:
# 启动服务
- pnpm start
environment:
NODE_ENV: production
PORT: 8001
- docker stop nodejs-app || true
- docker rm nodejs-app || true
- >
docker run -d
--name nodejs-app
--restart always
-p 8001:8001
-v /usr/local/blackend/node_modules:/app/node_modules
-v /usr/local/blackend/pnpm_cache:/root/.local/share/pnpm/store
-e NODE_ENV=production
-e PORT=8001
your-registry/nodejs-app:latest
volumes:
- name: docker_sock
path: /var/run/docker.sock
volumes:
- name: docker_sock
host:
path: /var/run/docker.sock
trigger:
branch:

View File

@ -1,37 +1,26 @@
# 构建阶段
1. 首先创建 `Dockerfile`
```dockerfile
FROM node:22-alpine
WORKDIR /app
# # 配置alpine国内镜像加速
# RUN sed -i "s@http://dl-cdn.alpinelinux.org/@https://repo.huaweicloud.com/@g" /etc/apk/repositories
# 安装 pnpm
RUN npm config set registry https://registry.npmmirror.com && \
npm install -g pnpm && \
pnpm config set registry https://registry.npmmirror.com
# 安装tzdata,默认的alpine基础镜像不包含时区组件安装后可通过TZ环境变量配置时区
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
# 安装开发期依赖
COPY package.json ./package.json
RUN npm install -g pnpm
RUN pnpm config set registry https://registry.npmmirror.com
RUN pnpm install
# 构建项目
# 复制项目文件
COPY . .
RUN pnpm run build
# 删除开发期依赖
# RUN rm -rf node_modules && rm package-lock.json
# # 安装生产环境依赖
# RUN pnpm install --production
# 如果端口更换,这边可以更新一下
# 安装依赖
RUN pnpm install
# 构建项目
RUN pnpm run build
EXPOSE 8001
CMD ["npm", "run", "start"]
CMD ["pnpm", "start"]
```