From 29a51d37c831086935d0966c5b977ecb8db42685 Mon Sep 17 00:00:00 2001 From: lixin <2967839139@qq.com> Date: Sat, 25 Jan 2025 10:17:08 +0800 Subject: [PATCH] ci(drone): Refactor deployment pipeline with Docker and containerization - 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 --- .drone.yml | 65 ++++++++++++++++++++++++++++-------------------------- Dockerfile | 47 +++++++++++++++------------------------ 2 files changed, 52 insertions(+), 60 deletions(-) diff --git a/.drone.yml b/.drone.yml index 21cdd55..aa01364 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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: diff --git a/Dockerfile b/Dockerfile index c62130a..7b86c89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,37 +1,26 @@ -# 构建阶段 -FROM node:22-alpine + +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"] \ No newline at end of file +CMD ["pnpm", "start"] +```