编程语言/Drone/drone-volume-cache

来自康健生活
Kangkk​(留言 | 贡献)2026年3月18日 (三) 16:39的版本 (→标题文本)

标题文本

drillster/drone-volume-cache: Drone plugin for caching to a locally mounted volume.

标题文本

- name: restore-cache # 把之前缓存好的数据取出
  image: drillster/drone-volume-cache
  settings:
    restore: true
    mount: # 缓存挂载的文件夹
      - ./.npm-cache
      - ./node_modules
  volumes:
    - name: cache
      path: /cache


kind: pipeline
type: docker #在docker runner中运行
name: web

#定义setups,每个setup有属于自己的name,最后会显示在Drone CI管理页面的侧边栏
steps:
- name: restore-cache # 把之前缓存好的数据取出
  image: drillster/drone-volume-cache
  settings:
    restore: true
    mount: # 缓存挂载的文件夹
      - ./.npm-cache
      - ./node_modules
  volumes:
    - name: cache
      path: /cache

- name: compile #编译
  image: node:12
  commands:
  - yarn config set registry https://registry.npm.taobao.org -g
  - yarn config set cache ./.npm-cache --global
  - yarn install
  - yarn run build

- name: build image #打成docker镜像
  image: docker
  failure: ignore
  volumes:
  - name: sock
    path: /var/run/docker.sock
  commands:
  - docker build -t localhost:v1.0 -f Dockerfile .
  - docker image prune -f --filter "dangling=true"   # 清理无用镜像

- name: rebuild-cache # 把依赖和 npm 缓存放到缓存里
  image: drillster/drone-volume-cache
  settings:
    rebuild: true
    mount:
      - ./.npm-cache
      - ./node_modules
  volumes:
    - name: cache
      path: /cache

- name: deploy #部署到服务器上
  image: docker
  failure: ignore
  volumes:
  - name: sock
    path: /var/run/docker.sock
  commands:
  - docker service ls|grep test || export SERVICE=down #先检查服务是否存在,存在更新,不存在创建
  - |
    if [ "$SERVICE" != "down" ]
    then
      docker service update --image test:v1.0 test_test
    else
      docker stack deploy -c deploy.yaml autocd-web
    fi            
  # 循环检测服务是否启动成功
  - |
    while true
    do
      docker service ps test_test|awk '{print $6}'|awk 'NR==2'|grep 'Running' || export SERVICE=down
      if [ "$SERVICE" == "down" ]
      then
          echo -e "\033[5;35;40m 正在启动中请稍后 ... \033[0m"
          export SERVICE=up
          continue
      else
          docker service logs -n 200 test_test
          sleep 3
          break
      fi
    done          

# 挂载宿主机文件到docker容器中      
volumes:
- name: sock
  host:
    path: /var/run/docker.sock
- name: cache
  host:
    path: /tmp/cache
  
# 创建触发器,绑定分支及事件及上一次成功时才运行
trigger:
  branch:
    - master
  event:
    - pull_request
    - push
  status:
    - success
    - failure

node:
  machine1:runner1


pipeline:
  # 加载缓存 node_modules
  restore-cache:
    image: drillster/drone-volume-cache
    restore: true
    mount:
      - ./node_modules
    volumes:
      - /tmp/cache:/cache
    when:
      # 触发事件类型[push], [pull_request]
      event: [push, pull_request]
      # 触发分支
      branch: [devlopment, test, master]

  # install
  install:
    image: node:12-alpine
    commands:
      # 删除文件 npm install
      - rm -rf package-lock.json
      - npm --registry https://registry.npm.taobao.org install
    when:
      event: [push, pull_request]
      branch: [devlopment, test, master]

  # 更新node_modules缓存
  rebuild-cache:
    image: drillster/drone-volume-cache
    rebuild: true
    mount:
      - ./node_modules
    volumes:
      - /tmp/cache:/cache
    when:
      event: [push, pull_request]
      branch: [devlopment, test, master]

  # push
  dev-push:
    image: appleboy/drone-scp
    host: xxx     # 可以写到secret中
    port: 22
    username: xxx
    password: xxx
    target: /app/vue-manage-server
    source: .
    rm: true
    when:
      event: [push, pull_request]
      branch: [devlopment]

  # push
  test-push:
    image: appleboy/drone-scp
    host: xxx     # 可以写到secret中
    port: 22
    username: xxx
    password: xxx
    target: /app/vue-manage-server
    source: .
    rm: true
    when:
      event: [push, pull_request]
      branch: [test]

  # push
  master-push:
    image: appleboy/drone-scp
    host: xxx     # 实际应用中,可以写到secret中
    port: 22
    username: xxx
    password: xxx
    target: /app/vue-manage-server
    source: .
    rm: true
    when:
      event: [push, pull_request]
      branch: [master]

  # run
  dev-run:
    image: appleboy/drone-ssh
    host: xxx
    username: xxx
    password: xxx
    port: 22
    command_timeout: 300s
    script:
      - cd /app/vue-manage-server
      - mkdir seresr
    when:
      event: [push, pull_request]
      branch: [devlopment]

  # run
  test-run:
    image: appleboy/drone-ssh
    host: xxx
    username: xxx
    password: xxx
    port: 22
    command_timeout: 300s
    script:
      - cd /app/vue-manage-server
      - mkdir seresr
    when:
      event: [push, pull_request]
      branch: [test]

  # run
  master-run:
    image: appleboy/drone-ssh
    host: xxx
    username: xxx
    password: xxx
    port: 22
    command_timeout: 300s
    script:
      - cd /app/vue-manage-server
      - docker build -t vue-manage-server .
      - docker run -d -p 4000:4000 vue-manage-server
    when:
      event: [push, pull_request]
      branch: [master]