CI/CD with drone

drone是前google员工2013年开源的一个CI/CD工具,现在已经拉了投资商业化了。企业版是付费的,我们用的是免费版本。

drone引入pipline的概念,整个build过程由多个stage组成,每一个stage都是docker。

  • 1.各stage间可以通过共享宿主机的磁盘目录, 实现build阶段的数据共享和缓存基础数据, 实现加速下次build的目标
  • 2.各stage也可以共享宿主机的docker环境,实现共享宿主机的docker image, 不用每次build都重新拉取base image,减少build时间
  • 3.可以并发运行。多个build可以并发运行,单机并发数量由服务器cpu数决定。

由开发者负责打包image和流程控制。Docker-in-docker, 这一点非常重要,一切都在掌握之中。相比jenkins的好处是,所有的image都是开发者提供,不需要运维参与在CI服务器上部署各种语言编译需要的环境。 是DevOps的最佳实践!

drone可以用docker-compose的方式,单机部署。为了节省服务器成本,我们在一台云主机上部署了两套drone,分别是drone0.5对应gitlab的private项目,drone0.7对应github的public项目。

drone secret需要跟环境变量里面的DRONE_HOST和DRONE_TOKEN绑定,所以只能用于drone0.5和drone0.7中的一个!

drone0.5是2017年3月发布的,如果使用了drone secret的话,每次修改了drone.xml都需要在drone的host上执行drone sign重新签名,非常麻烦。所以gitlab相关的private项目都是用drone0.5维护,drone.yml里面明文保存敏感信息(包括用户名、密码、主机名等),没有用drone secret.
drone0.7是2017年5月发布的,已经去掉了drone sign的限制。所以github的public项目因为安全考虑,加上了drone secret,drone.yml里面没有明文保存的敏感信息.


1. drone host:

# docker ps
CONTAINER ID   IMAGE            COMMAND          PORTS                                     NAMES
095717b1c348   drone/drone:0.7  "/drone agent"   8002->8000/tcp          dronegithub_drone-agent-github_1
fccf7fa84b7c   drone/drone:0.7  "/drone server"  8001->8000/tcp          dronegithub_drone-server-github_1
bfd48a1032f0   drone/drone:0.5  "/drone agent"   8000/tcp                dronegitlab_drone-agent_1
09e9520b8c96   drone/drone:0.5  "/drone server"  8000->8000/tcp          dronegitlab_drone-server_1

CI/CD with drone_第1张图片

2. web-im的.drome.yml:

workspace:
  base: /data/apps/opt
  path: web-im


pipeline:

  restore-cache:
    image: drillster/drone-volume-cache
    restore: true
    mount:
      - node_modules
      - tag
    volumes:
      - /data/apps/opt/web-im:/cache

  build:
    image: node:7.8
    privileged: true
    commands:
      - npm run build
      - mkdir -p publish/demo/javascript
      - cp -r demo/images publish/demo
      - cp -r demo/stylesheet publish/demo
      - cp -r demo/javascript/dist publish/demo/javascript/
      - cp -r demo/javascript/src publish/demo/javascript/
      - mkdir publish/sdk
      - cp -r sdk/dist publish/sdk
      - cp -r sdk/src publish/sdk
      - cp sdk/*.* publish/sdk
      - cp -r webrtc  publish
      - cp favicon.ico publish/
      - cp index.html publish/
      - cp CHANGELOG.md publish/
      - cp package.json publish/
      - cp webpack.config.js publish/
      - cp README.md publish/
      - cp .babelrc publish/
      - cp -rf publish image/docker/webim/webim
      - echo 'build success'
    when:
      branch: [ dev, online ]


  dockerize-latest:
    image: plugins/docker
    environment:
      - DOCKER_LAUNCH_DEBUG=true
    debug: true
    repo: docker-registry-cn.easemob.com/kubernetes/im/webim
    tags: latest
    registry: docker-registry-cn.easemob.com
    secrets: [ docker_username, docker_password ]
    dockerfile: image/docker/webim/Dockerfile
    context: image/docker/webim/
    when:
      branch: dev

  deploy-latest:
    image: docker-registry-cn.easemob.com/kubernetes/im/webim-deploy:latest
    pull: true
    environment:
      - DOCKER_LAUNCH_DEBUG=true
      - TAG=latest
    secrets: [ ssh_key, jumpserver_host, jumpserver_port, sandbox_host ]
    debug: true
    when:
      branch: dev

  dockerize-online:
    image: plugins/docker
    environment:
      - DOCKER_LAUNCH_DEBUG=true
    debug: true
    repo: docker-registry-cn.easemob.com/kubernetes/im/webim
    tags: ${DRONE_COMMIT:0:7}
    registry: docker-registry-cn.easemob.com
    secrets: [ docker_username, docker_password ]
    dockerfile: image/docker/webim/Dockerfile
    context: image/docker/webim/
    when:
      branch: online

  deploy-online:
    image: docker-registry-cn.easemob.com/kubernetes/im/webim-online:latest
    pull: true
    environment:
      - DOCKER_LAUNCH_DEBUG=true
      - TAG=${DRONE_COMMIT:0:7}
    secrets: [ ssh_key, jumpserver_host, jumpserver_port, online_host ]
    debug: true
    when:
      branch: online

  rollback-online:
    image: docker-registry-cn.easemob.com/kubernetes/im/webim-rollback:latest
    pull: true
    environment:
      - DOCKER_LAUNCH_DEBUG=true
    secrets: [ ssh_key, jumpserver_host, jumpserver_port, online_host ]
    debug: true
    when:
      branch: rollback

  rebuild-cache:
      image: drillster/drone-volume-cache
      rebuild: true
      mount:
        - node_modules
        - tag
      volumes:
        - /data/apps/opt/web-im:/cache

  notify:
    image: drillster/drone-email
    port: 25
    secrets: [ plugin_host, plugin_from, plugin_username, plugin_password ]
    when:
      status:  [ failure, success ]

dev分支提交,会自动build,打包image,push到harbor仓库,更新沙箱,发邮件通知
online分支提交,会自动build,打包image,push到harbor仓库,更新线上,更新image tag(线上回滚需要),发邮件通知
rollback分支提交,会使用image tag,回滚到上一次的线上版本, 发邮件通知

CI/CD with drone_第2张图片

drone的官方插件在实际使用中有些小问题,一个是pluglins/docker没法缓存base image,每次build都需要重新拉取。非常浪费时间。二是官方的邮件插件有时会超时。我们都通过自定义的插件给解决了!现在我们组负责的项目,一共有5个微服务都是通过drone来控制CI/CD的,平均每天有十次左右git提交,CI/CD自动化,很快就能收到worktile的部署结果通知,非常方便!

你可能感兴趣的:(docker)