Docker Drone CI/CD 用法举例

官方文档:http://docs.drone.io/getting-started/

官方文档:http://docs.drone.io/zh/getting-started/

Drone 本质就是在指定的 Docker 容器中执行命令。

与其他 CI/CD 类似,项目中必须包含 .drone.yml 文件来定义工作流,才能开始使用。

命令行工具

https://github.com/drone/drone-cli/releases

下载之后移入 PATH

用法举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
clone:
git:
image: plugins/git
#
# 克隆深度
#
depth: 50
recursive: true
submodule_override:
#
# 重写 git 子模块地址
#
# @link http://plugins.drone.io/drone-plugins/drone-git/
#
tests/resource: http://192.168.199.100:3000/khs1994/image.git

#
# 默认克隆到 /drone/src/github.com/username/hello-world
#

workspace:
base: /go
#
# 克隆到了 /go/src/github.com/octocat/hello-world
#
path: src/github.com/octocat/hello-world
#
# 克隆到 /go
#
path: .

pipeline:
backend:
#
# 每次构建时总是拉取镜像
#
pull: true
#
# 同一 group 会并行执行
#
group: build
image: golang
image: gcr.io/custom/golang
environment:
- CGO=0
- GOOS=linux
- GOARCH=amd64
commands:
- export PATH=$PATH:/go
# - export PATH=${PATH}:/go
# ${VAR} 这类变量不能被解析
- export PATH=$${PATH}:/go
- sleep 15
- go get
- go build
- go test
#
# 特定状态才构建
#
# @link http://docs.drone.io/conditional-steps/
#
when:
branch: master
event: [push, pull_request, tag, deployment]
#
# 指定平台
#
platform: linux/amd64
status: changed
volumes:
- /var/run/docker.sock:/var/run/docker.sock
volumes: [ /etc/ssl/certs:/etc/ssl/certs ]
privileged: true

frontend:
#
# 同一 group 会并行执行
#
group: build
image: node:6
commands:
- npm install
- npm test

publish:
#
# http://plugins.drone.io/drone-plugins/drone-docker/
#
image: plugins/docker
registry: registry.heroku.com
repo: foo/bar
tags: latest
when:
#
# $ drone deploy octocat/hello-world 24 staging
#
# 命令行执行 deploy 命令才会执行
#
event: deployment
environment: staging
# username: username
# password: password
#
# 密钥这里小写,实际系统引用的时候会自动变成大写
#
secrets: [ docker_username, docker_password ]
secrets:
- source: docker_prod_password
target: docker_password

notify:
image: plugins/slack
channel: developers
username: drone
when:
status: [ success, failure ]

services:
#
# 服务使用 mysql 作为 host,下同
#
mysql:
image: mysql
environment:
- MYSQL_DATABASE=test
- MYSQL_ALLOW_EMPTY_PASSWORD=yes

redis:
image: redis

# 只构建以下分支

branches: master

branches: [ master, dev]

branches:
include: [ master, feature/* ]

# 除了以下分支,都构建

branches:
exclude: [ develop, feature/* ]

跳过构建

commit 信息加上 [ci skip]

1
$ git commit -m "updated README [CI SKIP]"

矩阵构建

http://docs.drone.io/zh/matrix-builds/

1
2
3
4
5
6
7
8
9
10
11
12
pipeline:
build:
image: ${IMAGE}
commands:
- go build
- go test

matrix:
IMAGE:
- golang:1.7
- golang:1.8
- golang:latest

密钥

Docker 仓库相关密钥

构建过程需要私有仓库镜像,这个配置来使得 drone 能够拥有相关权限

1
2
3
4
5
6
$ drone registry add \
--repository username/hello-world \
--hostname gcr.io \
# --hostname docker.io \
--username <name> \
--password <value>

增加密钥

1
2
3
4
5
$ drone secret add \
-repository username/hello-world \
-image plugins/docker \
-name docker_username \
-value <value>

指定的事件才能使用这个密钥。

1
2
3
4
5
6
7
8
$ drone secret add \
-repository username/hello-world \
-image plugins/docker \
-event pull_request \
-event push \
-event tag \
-name docker_username \
-value <value>

系统内置变量

http://docs.drone.io/environment-reference/

0%