ci(drone): Refactor deployment pipeline with Docker and containerization
Some checks failed
continuous-integration/drone/push Build is failing
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:
parent
6bd32f5669
commit
29a51d37c8
65
.drone.yml
65
.drone.yml
@ -2,7 +2,6 @@ kind: pipeline
|
|||||||
type: docker
|
type: docker
|
||||||
name: nodejs-deploy
|
name: nodejs-deploy
|
||||||
|
|
||||||
# 使用国内镜像加速
|
|
||||||
volumes:
|
volumes:
|
||||||
- name: node_modules
|
- name: node_modules
|
||||||
host:
|
host:
|
||||||
@ -12,44 +11,48 @@ volumes:
|
|||||||
path: /usr/local/blackend/pnpm_cache
|
path: /usr/local/blackend/pnpm_cache
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: build-docker
|
||||||
image: node:22-alpine
|
image: plugins/docker
|
||||||
depends_on: [clone]
|
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:
|
volumes:
|
||||||
- name: node_modules
|
- name: node_modules
|
||||||
path: /drone/src/node_modules
|
path: /drone/src/node_modules
|
||||||
- name: pnpm_cache
|
- name: pnpm_cache
|
||||||
path: /root/.local/share/pnpm/store
|
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
|
- name: deploy
|
||||||
image: node:22-alpine
|
image: docker
|
||||||
depends_on: [build]
|
|
||||||
volumes:
|
|
||||||
- name: node_modules
|
|
||||||
path: /drone/src/node_modules
|
|
||||||
commands:
|
commands:
|
||||||
# 启动服务
|
- docker stop nodejs-app || true
|
||||||
- pnpm start
|
- docker rm nodejs-app || true
|
||||||
environment:
|
- >
|
||||||
NODE_ENV: production
|
docker run -d
|
||||||
PORT: 8001
|
--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:
|
trigger:
|
||||||
branch:
|
branch:
|
||||||
|
45
Dockerfile
45
Dockerfile
@ -1,37 +1,26 @@
|
|||||||
# 构建阶段
|
|
||||||
|
1. 首先创建 `Dockerfile`:
|
||||||
|
|
||||||
|
```dockerfile
|
||||||
FROM node:22-alpine
|
FROM node:22-alpine
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# # 配置alpine国内镜像加速
|
# 安装 pnpm
|
||||||
# RUN sed -i "s@http://dl-cdn.alpinelinux.org/@https://repo.huaweicloud.com/@g" /etc/apk/repositories
|
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 . .
|
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
|
EXPOSE 8001
|
||||||
|
|
||||||
CMD ["npm", "run", "start"]
|
CMD ["pnpm", "start"]
|
||||||
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user