本文会详细介绍如何编写一个Drone的pipeline文件
Pipeline是什么?
pipeline的意思就是流水线,持续集成其实就是一个流水线工作,指定一系列的动作,然后让程序按顺序和条件去执行。所以写好一个pipeline就很重要。本文会介绍比较常用的pipeline流水线模式。
编写Pipeline
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://registry.npm.taobao.org
- 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文件,我们定义了三个步骤:
- 取出缓存中的内容
- 使用yarn构建
- 将新的缓存放回去
当pipeline被触发的时候,就会安装上面的步骤,依序执行。
多个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里面加上限制即可。也就是注释了的那几段。
构建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官方给出的,有兴趣的,可以自己研究下这个镜像。主要的参数都有注释说明,还有其他问题可以看看官方的这个插件文档。
通知推送
构建成功还是失败,肯定是需要来个钉钉、邮件、短信、微信之类的推送下对吧。
这里以钉钉为例:
---
kind: pipeline
name: default
steps:
- name: dingtalk
image: lddsb/drone-dingtalk-message
settings:
#钉钉的webhook token
token:
from_secret: dingding_token
#推送的文本类型
type: markdown
#消息颜色
message_color: true
#分享链接
sha_link: true
when:
status:
- failure
这个钉钉推送是lddsb制作的插件,当然如果想自己自定短语之类的,可以自己做个,比如你有自己的企业IM,需要做个推送,就可以自己写脚本,然后做成一个插件,然后就可以吧镜像换成自己的插件,来做消息推送了。
结尾
上面大部分的pipeline配置,里面用到的镜像都是官方收录的镜像,以及一些系统环境镜像。Drone的灵活在于可以自己轻松的定制镜像来实现各种各样的需求。以后有空,可能还会出一个Drone的镜像定制教程,实现自己写插件。