drone中使用npm时如何实现依赖缓存
这里分享一下我的做法,更多资料可以参考官方文档:docs.drone.io/
说明:
上一期《docker+gitea+drone实现超轻量级的CI/CD实战》中有说到在部署我的vue项目时存在一个bug,缓存依赖没成功,这里补一下。
1、首先挂载宿主机磁盘空间
volumes:
- name: node_modules
host: path: /home/docker/drone/node/node_modules(宿主机空间)
2、其次在build步骤将镜像空间挂载
将/drone/src/node_modules挂载到node_modules
volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置
- name: node_modules
path: /drone/src/node_modules
kind: pipeline
type: docker
name: zkblog
steps:
# 构建
- name: build # 流水线名称
image: node:14-alpine # 定义创建容器的Docker镜像
volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置
- name: node-build
path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
- name: node_modules
path: /drone/src/node_modules
commands: # 定义在Docker容器中执行的shell命令
- pwd # 查看当前目录 `/drone/src`
- ls -alt
- npm config set registry https://registry.npm.taobao.org # 切换淘宝镜像
- npm install canvas@2.8.0 --ignore-scripts
- npm install # 安装依赖
- npm run build # 执行构建指令
- cp -r dist /app/build/
- cp Dockerfile /app/build/
- cp default.conf /app/build/
- cp run.sh /app/build/
# 部署
- name: deploy # 流水线名称
image: plugins/docker
volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置
- name: node-build
path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
- name: docker
path: /var/run/docker.sock # 挂载宿主机的docker
settings:
dockerfile: /app/build/Dockerfile
commands: # 定义在Docker容器中执行的shell命令
- cd /app/build
- chmod +x run.sh
- sh run.sh
- docker ps
volumes: # 定义流水线挂载目录,用于共享数据
- name: node-build
host:
path: /home/docker/drone/node/build # 从宿主机中挂载的目录
- name: docker
host:
path: /var/run/docker.sock
- name: node_modules
host:
path: /home/docker/drone/node/node_modules