Drone 缓存

2,371 阅读1分钟

Drone Cache

Drone 是使用容器来执行脚本,编译环境的,容器用完。对于 Java 或者 ReactVue 之类的需要下载很多包的项目,如果不把相应的包缓存起来,每次 build 就会花去很长时间。

Drone volume

个人推荐使用 Drone volume 的方式去实现,比较简单


kind: pipeline
type: docker
name: drone-test-ci

steps:
  - name: test
    image: node
    volumes:
      - name: cache
        path: /drone/src/node_modules
    commands:
      - mkdir -p ./node_modules
      - export NODE_MODULES_PATH=`pwd`/node_modules
      - npm config set registry https://registry.npm.taobao.org
      - npm i
      - echo $NODE_MODULES_PATH

volumes:
- name: cache
  host:
    path: /var/lib/npm/cache

trigger:
  event:
    - pull_request
    - push

drone-volume-cache

在研究 Drone volume 之前,研究这个 plugin 研究了好久才搞定。这个插件一定要使用相对路径才可,不是很推荐,比较麻烦

kind: pipeline
type: docker
name: ci

steps:
  - name: restore-cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /cache
    settings:
      restore: true
      mount:
        - ./.m2
  - name: flow-test
    image: maven:3.8.4-jdk-11-slim
    environment:
      REPO_ID: maven-public
      REPO_USERNAME:
        from_secret: nexus_download_user
      REPO_PASSWORD:
        from_secret: nexus_download_pwd
    commands:
      - mkdir -p ./.m2
      - export LOCAL_REPOSITORY=`pwd`/.m2
      - echo '<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"><localRepository>'$LOCAL_REPOSITORY'</localRepository><servers><server><id>'$REPO_ID'</id><username>'$REPO_USERNAME'</username><password>'$REPO_PASSWORD'</password></server></servers></settings>' > settings.xml
      - mvn -s settings.xml clean test
  - name: rebuild-cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /cache
    settings:
      rebuild: true
      mount:
        - ./.m2

volumes:
  - name: cache
    host:
      path: /tmp/cache

trigger:
  event:
    - pull_request
    - push