Drone开源持续集成工具——Pipeline篇

本文会详细介绍如何编写一个 Drone 的 pipeline 文件。
pipeline 的意思就是流水线,持续集成其实就是一个流水线工作,指定一系列的动作,然后让程序按顺序和条件去执行。所以写好一个 pipeline 就很重要。本文会介绍比较常用的 pipeline 流水线模式。

1. Drone 的 Pipeline 语法

Drone 的 pipeline 语法和 k8s 非常的相似,也是 yaml 文件。这里贴上 nodejs 的构建示例来作为展示 (附带注释讲解):

#分隔符
---
#定义类型
kind: pipeline
#定义pipeline名字
name: default
#定义步骤,固定语法
steps:
#定义步骤的名字
- name: restore-cache
#定义该步骤用到的镜像
image: drillster/drone-volume-cache
#挂载缓存
volumes:
#挂载的名字
- name: cache
#挂载路径
path: /cache
#该步骤的设置(就是容器的环境变量)
settings:
restore: true
mount:
- ./node_modules
#定义下一个步骤
- name: build
#所用到的镜像
image: node:8.15
#镜像运行时候的命令(就是docker里面的command)
commands:
- yarn config set registry https:
- yarn install
- yarn run build
#缓存相关的
- name: rebuild-cache
image: drillster/drone-volume-cache
volumes:
- name: cache
path: /cache
settings:
rebuild: true
mount:
- ./node_modules
#当对应条件的时候才会执行
when:
#pipeline状态为成功和失败的时候。
status:
- success
- failure

#设定挂载的路径
volumes:
- name: cache
host:
path: /tmp/cache
- name: docker
host:
path: /var/run/docker.sock

通过上面的 yaml 文件,我们定义了三个步骤:

  1. 取出缓存中的内容
  2. 使用 yarn 构建
  3. 将新的缓存放回去

当 pipeline 被触发的时候,就会安装上面的步骤,依序执行。

2. 多个 Pipeline 的执行

通常我们不止一个流水线,可能需要并行执行,执行完一个流水线后再执行其他的流水线。Drone 一样可以做到,只需要加个分隔符即可。这和 k8s 里面是一样的,这里用 nodejs 项目构建同时执行代码扫描来做个例子:

kind: pipeline
name: build

steps:
- name: build
image: node:8.15
commands:
- yarn config set registry https://registry.npm.taobao.org
- yarn install
- yarn run build


kind: pipeline
name: scan

steps:
- name: code-analysis
image: aosapps/drone-sonar-plugin
settings:
sonar_host: http://sonar.asoco.com.cn
sonar_token:
#从密文获取(drone的secret设置)
from_secret: sonar_token

上面的例子就是同时执行,只需要写 2 个 pipeline,然后用 ---
分割开即可。
但是我们很多时候是需要为后一个 pipeline 设定条件的,比如我们规定构建成功后再做代码扫描,因为构建不成功,也就没有做代码扫描的必要了对吧。这里用上面的例子:

kind: pipeline
name: build

steps:
- name: build
image: node:8.15
commands:
- yarn config set registry https://registry.npm.taobao.org
- yarn install
- yarn run build


kind: pipeline
name: scan

steps:
- name: code-analysis
image: aosapps/drone-sonar-plugin
settings:
sonar_host: http://sonar.asoco.com.cn
sonar_token:
from_secret: sonar_token

#只有上一个pipeline成功才会触发
trigger:
status:
- success

#在指定的pipeline完成之后进行验证
depends_on:
- build

可以看到,再上面,我们只需要再需要加入条件的 pipeline 里面加上限制即可。也就是注释了的那几段。

3. 构建 docker 镜像

由于 drone 是基于原生的 docker 来做持续集成的。所以构建 docker 镜像实际上就是用了 docker in docker。官方也直接给了个示例。这里我依然用 nodejs 的项目作为例子

kind: pipeline
name: default

steps:
- name: build
image: node:8.15
commands:
- yarn config set registry https://registry.npm.taobao.org
- yarn install
- yarn run build

- name: publish
image: plugins/docker
settings:
mirror: https://docker.mirrors.ustc.edu.cn
# 注册仓库(可以是harbor之类的地址)
registry: registry.cn-hangzhou.aliyuncs.com
# docker仓库地址
repo: registry.cn-hangzhou.aliyuncs.com/lm93129/drone-test
#自动给镜像打tag,这里是根据推送上来的git信息来自动打tag的
auto_tag: true
#镜像仓库的账户密码
password:
from_secret: docker_password
username:
from_secret: docker_username
purge: true
#运行该步骤的条件
when:
branch:
- master

这里 publish 就是构建镜像并且推送镜像到镜像仓库了。plugins/docker 这个镜像是固定的,drone 官方给出的,有兴趣的,可以自己研究下这个镜像。主要的参数都有注释说明,还有其他问题可以看看官方的这个插件文档。

4. 通知推送

构建成功还是失败,肯定是需要来个钉钉、邮件、短信、微信之类的推送下对吧。
这里以钉钉为例:

---
kind: pipeline
name: default

steps:
- name: dingtalk
image: lddsb/drone-dingtalk-message
settings:

token:
from_secret: dingding_token

type: markdown

message_color: true

sha_link: true
when:
status:
- failure

这个钉钉推送是 lddsb 制作的插件,当然如果想自己自定短语之类的,可以自己做个,比如你有自己的企业 IM,需要做个推送,就可以自己写脚本,然后做成一个插件,然后就可以吧镜像换成自己的插件,来做消息推送了。
上面大部分的 pipeline 配置,里面用到的镜像都是官方收录的镜像,以及一些系统环境镜像。Drone 的灵活在于可以自己轻松的定制镜像来实现各种各样的需求。以后有空,可能还会出一个 Drone 的镜像定制教程,实现自己写插件。