Drone构建Laravel镜像优化

Drone构建Laravel镜像通过缓存避免重复composer提高构建速度

       最近优化了一下Drone构建Laravel流程,通过加入缓存,避免每次构建都composer扩展包,从而提高构建速度,下面跟大家分享构建配置。

       一、设置目录挂载

#设置挂载
volumes:
  #Go编译缓存
  - name: cache
    host:
      path: /home/data/drone/cache/program-manage
  #Docker环境
  - name: docker
    host:
      path: /var/run/docker.sock
  #Docker配置
  - name: docker-daemon
    host:
      path: /etc/docker/daemon.json

       二、构建加载缓存

       每次构建前,我们可以通过提前加载缓存,达到不用重复composer包

#加载缓存
- name: cache
  image: drillster/drone-volume-cache
  volumes:
  - name: cache
    path: /cache
  settings:
    restore: true
    mount:
    - ./vendor

       三、重构缓存

       为了保持缓存新鲜度,我们每次构建完,都重新构建缓存,达到扩展包是最新的

#构建缓存
- name: rebuild
  image: drillster/drone-volume-cache
  volumes:
  - name: cache
    path: /cache
  settings:
    rebuild: true
    mount:
    - ./vendor

       四、完整配置

       下面是完整的配置,我们需要提前在宿主服务器创建目录/home/data/drone/cache/program-manage,用于存放每次构建缓存。

kind: pipeline
type: docker
name: program-manage

#设置挂载
volumes:
  #Go编译缓存
  - name: cache
    host:
      path: /home/data/drone/cache/program-manage
  #Docker环境
  - name: docker
    host:
      path: /var/run/docker.sock
  #Docker配置
  - name: docker-daemon
    host:
      path: /etc/docker/daemon.json
#构建步骤
steps:
#设置缓存
- name: clean
  image: docker:19.03.1
  pull: if-not-exists
  network_mode: host
  # 将宿主机的 docker和配置挂载到运行的 docker 容器中,那么在容器中运行 docker 命令时,等同于在宿主机中运行该docker 命令
  volumes:
    - name: docker
      path: /var/run/docker.sock
    - name: images
      path: /images
    - name: docker-daemon
      path: /etc/docker/daemon.json
#加载缓存
- name: cache
  image: drillster/drone-volume-cache
  volumes:
  - name: cache
    path: /cache
  settings:
    restore: true
    mount:
    - ./vendor
#安装composer扩展
- name: backend
  pull: if-not-exists
  image: hongzhuangxian/php7.3-diy
  commands:
    - composer install --prefer-dist
#推送镜像至镜像仓库
- name: publish
  image: plugins/docker
  pull: if-not-exists
  mirrors:       #镜像仓库加速地址,不需要加速,可以直接删除
    from_secret: registry_mirrors
  settings:
    purge: false
    registry:   #镜像仓库域名
      from_secret: registry
    repo:        #镜像仓库详细地址
      from_secret: repo
    use_cache: true
    tags:
      - latest
    username:    #镜像仓库账户
      from_secret: registry_user_name
    password:    #镜像仓库密码
      from_secret: registry_password
    volumes:
      - name: docker
        path: /var/run/docker.sock
      - name: cache
        path: /cache
#构建缓存
- name: rebuild
  image: drillster/drone-volume-cache
  volumes:
  - name: cache
    path: /cache
  settings:
    rebuild: true
    mount:
    - ./vendor
#部署服务
- name: ssh commands
  pull: if-not-exists
  image: appleboy/drone-ssh
  settings:
    host:
      from_secret: ssh_ip
    port: 22
    username:
      from_secret: ssh_user_name
    password:
      from_secret: ssh_password
    script:
      - cd /home/php/programManage
      - docker-compose pull && docker-compose up --force-recreate -d
#规定master分支,触发构建
branches: master

0条评论

发表评论