Fork me on GitHub

基于Gogs+Drone搭建的私有CI/CD平台

本文介绍如何打造基于Docker的私有CI/CD平台

前言

目前体验过或者使用过的自动化构建工具有Jenkins,Travis-ci,Gitlab-CI,最近又发现了Drone。它是一款使用Go开发的开源的CI自动构建平台,能够单独部署,支持常见的Git仓库,例如Github,Gitlab, Bitbucket以及Gogs等。
本文带大家使用Gogs+Drone打造基于Docker的私有CI/CD平台,本文有不足或者认识不到位的地方欢迎大家批评指正。

环境说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
# uname -r
3.10.0-327.el7.x86_64
# uname -m
x86_64
# docker version
Client:
Version: 17.06.2-ce
API version: 1.30
Go version: go1.8.3
Git commit: cec0b72
Built: Tue Sep 5 19:59:06 2017
OS/Arch: linux/amd64

Server:
Version: 17.06.2-ce
API version: 1.30 (minimum version 1.12)
Go version: go1.8.3
Git commit: cec0b72
Built: Tue Sep 5 20:00:25 2017
OS/Arch: linux/amd64
Experimental: false

部署Docker

安装Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# cd /etc/yum.repos.d/ && rm -f *.repo
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# yum remove -y docker docker-common docker-selinux docker-engine
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum-config-manager --enable docker-ce-edge
# yum list docker-ce --showduplicates | sort -r |grep stable
# yum install -y docker-ce-17.06.2.ce
# systemctl start docker
# systemctl enable docker
# mkdir -p /etc/docker
# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://k9lgui7f.mirror.aliyuncs.com"] # 为了后期方便添加阿里云的加速镜像站
}
EOF
# systemctl daemon-reload
# systemctl restart docker

测试Docker服务正常

例行跑个hello world测试下Docker服务是否正常

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
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

安装docker-compose

1
# yum install -y docker-compose

部署Gogs

Gogs简介

Gogs,一款极易搭建的自助Git服务。
优势如下:

  • 易安装:除了可以根据操作系统平台下载二进制运行,还可以通过Docker或Vagrant,以及包管理安装。
  • 跨平台:任何Go语言支持的平台都可以运行Gogs,包括Windows、Mac、Linux以及ARM。
  • 轻量级:一个廉价的树莓派的配置足以满足 Gogs 的最低系统硬件要求。有些用户甚至还将 Gogs 运行在 NAS 设备上。

安装MySQL

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
# docker run --name gogs-mysql --restart=always -v /opt/mysql/mysqlVolume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7.19
Unable to find image 'mysql:5.7.19' locally
5.7.19: Pulling from library/mysql
85b1f47fba49: Pull complete
27dc53f13a11: Pull complete
095c8ae4182d: Pull complete
0972f6b9a7de: Pull complete
1b199048e1da: Pull complete
159de3cf101e: Pull complete
963d934c2fcd: Pull complete
f4b66a97a0d0: Pull complete
f34057997f40: Pull complete
ca1db9a06aa4: Pull complete
0f913cb2cc0c: Pull complete
Digest: sha256:bfb22e93ee87c6aab6c1c9a4e7cdc68e9cb9b64920f28fa289f9ffae9fe8e173
Status: Downloaded newer image for mysql:5.7.19
406c74b329f91e6eab27a8e338a8d06ca41a877701348d5f1972127356c07c16
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.19 3e3878acd190 2 months ago 412MB
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6984a3ea514c mysql:5.7.19 "docker-entrypoint..." 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp gogs-mysql
# yum install -y mysql
# mysql -uroot -p123456 -h 127.0.0.1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.19 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select version();
+-----------+
| version() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.00 sec)

MySQL [(none)]> \q
Bye

创建Gogs及drone数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# mysql -uroot -p123456 -h 127.0.0.1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.19 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> create database drone;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> \q
Bye

安装Gogs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# docker pull gogs/gogs
# mkdir -p /var/gogs
# docker run --name=gogs --restart=always --link gogs-mysql:db -p 10022:22 -p 10080:3000 -v /var/gogs:/data gogs/gogs
usermod: no changes
Jan 6 13:01:29 syslogd started: BusyBox v1.25.1
Jan 6 13:01:30 sshd[30]: Server listening on :: port 22.
Jan 6 13:01:30 sshd[30]: Server listening on 0.0.0.0 port 22.
2018/01/06 13:01:30 [ WARN] Custom config '/data/gogs/conf/app.ini' not found, ignore this if you're running first time
2018/01/06 13:01:30 [TRACE] Custom path: /data/gogs
2018/01/06 13:01:30 [TRACE] Log path: /app/gogs/log
2018/01/06 13:01:30 [TRACE] Build Time: 2017-11-22 08:19:49 UTC
2018/01/06 13:01:30 [TRACE] Build Git Hash:
2018/01/06 13:01:30 [TRACE] Log Mode: Console (Trace)
2018/01/06 13:01:30 [ INFO] Gogs 0.11.34.1122
2018/01/06 13:01:30 [ INFO] Cache Service Enabled
2018/01/06 13:01:30 [ INFO] Session Service Enabled
2018/01/06 13:01:30 [ INFO] SQLite3 Supported
2018/01/06 13:01:30 [ INFO] Run Mode: Development
2018/01/06 13:01:30 [ INFO] Listen: http://0.0.0.0:3000

打开页面继续配置Gogs
http://192.168.200.109:10080/install
配置数据库

应用基本配置

修正:应用URL填写http://192.168.200.109:10080
注册一个账号

账号:madongsheng
密码:123456
登录:http://192.168.200.109:10080

创建一个测试仓库

部署Drone server

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
# docker run -d \
-e DRONE_HOST=192.168.200.109 \
-e DRONE_GOGS=true \
-e DRONE_GOGS_URL=http://192.168.200.109:10080 \
-e DRONE_OPEN=true \
-e DRONE_SECRET=123456 \
-v /tmp/drone:/var/lib/drone \
-p 9090:8000 \
--restart=always \
--name=drone \
drone/drone
Unable to find image 'drone/drone:latest' locally
latest: Pulling from drone/drone
297640bfa2ef: Pull complete
c7bc7c1b3f31: Pull complete
Digest: sha256:ed0125a78737586cb59995fae65b1d89661676783f3141d9a7818fca3866769f
Status: Downloaded newer image for drone/drone:latest
0fc16e8e46f4d04196436c1946f25b611e15af5e61676a5d9b3e661ff39afa7f
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
drone/drone latest 45891aade610 2 weeks ago 30.4MB
gogs/gogs latest 6ab1773c26bc 6 weeks ago 139MB
mysql 5.7.19 3e3878acd190 2 months ago 412MB
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fc16e8e46f4 drone/drone "/bin/drone-server" 20 seconds ago Up 20 seconds 80/tcp, 443/tcp, 9000/tcp, 0.0.0.0:9090->8000/tcp drone

参数说明:
添加DRONE_DATABASE_DRIVER与DRONE_DATABASE_DATASOURCE参数说明使用MySQL存储相关数据,默认使用sqllite

登录drone控制面板
http://192.168.200.109:9090/login/form

使用Gogs账号密码登录 例如:账号madongsheng 密码123456

如何激活仓库

注释:登录后会列出所有有权限的repos信息,如果没有可以点击右上角菜单找到synchronize进行同步仓库信息。

部署Drone agent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# docker run -d \
-e DRONE_HOST=192.168.200.109 \
-e DRONE_SERVER=ws://192.168.200.109:9090/ws/broker \
-e DRONE_SECRET=123456 \
-e DRONE_DATABASE_DRIVER=mysql \
-e DRONE_DATABASE_DATASOURCE=root:[email protected]\(192.168.200.109:3306\)/drone?parseTime=true \
-e DRONE_GOGS=true \
-e DRONE_GOGS_URL=http://192.168.200.109:10080 \
-e DRONE_GOGS_GIT_USERNAME=madongsheng \
-e DRONE_GOGS_GIT_PASSWORD=123456 \
-v /var/run/docker.sock:/var/run/docker.sock \
--name=drone-agent \
--link gogs:gogs \
drone/drone agent
db57facd3db3554f70a6f5f1e95c437d7d32fa8718d45d657913a87727a4bf51
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
219448fc1c5c drone/drone "/bin/drone-server..." 4 seconds ago Up 3 seconds 80/tcp, 443/tcp, 8000/tcp, 9000/tcp drone-agent

应用案例

自动化构建案例

在库里添加构建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
# vim .drone.yml
pipeline:
build:
image: centos:6.9
commands:
- yum install -y gcc gcc-c++ automake autoconf
- echo "hello world!!!"
# git clone http://192.168.200.108:10080/madongsheng/test01.git
# cd test01/
# git add .
# git commit -m 'add .drone.yml'
# git push origin

提前将相关镜像pull到本地,加快测试速度

1
2
3
4
# docker pull golang
# docker pull node
# docker pull centos:6.9
# docker pull plugins/git

自动触发创建构建任务

配置Drone用户注册

开放注册

1
+     DRONE_OPEN= true

限制注册

1
2
+     DRONE_OPEN: true
+ DRONE_ORGS: dogpatch,dolores

关闭注册

1
2
+     - DRONE_OPEN=false
+ - DRONE_ADMIN=janedoe,johnsmith

显示状态图标

官网资料
显示效果,如下:

命令行管理Drone

管理主机安装命令行软件包

1
2
# curl -L https://github.com/drone/drone-cli/releases/download/v0.8.0/drone_linux_amd64.tar.gz | tar zx
# install -t /usr/local/bin/ drone

设置token

1
2
# export DRONE_SERVER=http://192.168.200.109:9090
# export DRONE_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXh0IjoibWFkb25nc2hlbmciLCJ0eXBlIjoidXNlciJ9.iGJkLKdYz9HbQIxWORSKe1gyyuKUU85c0MdyU498Ekg

测试

1
2
3
# drone info
User: madongsheng
Email: [email protected]

主要功能

  • 用户管理
  • 仓库管理
  • 构建管理
  • REGISTRY管理
  • 其他管理

API使用

  • 当前登录用户

    1
    GET /api/user
  • 当前登录用户的活动信息流

    1
    GET /api/user/feed
  • 当前登录用户的仓库列表

    1
    GET /api/user/repos

如何实现agent扩展

没有看到官网有类似说明文章,想到可以使用docker-compose进行快速伸缩

1
# docker-compose scale drone-agent=5

扩展前后可以明显看到并发任务量的增加

插件

官网插件地址
可以根据个人项目需求选择合适的插件进行配置。

附件

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
# cat docker-compose.yml
version: '2'
services:
mysql-gogs:
image: 'mysql:5.7.17'
restart: always
ports:
- '3306:3306'
volumes:
- /opt/docker/mysql/mysqlVolume:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123456

gogs:
image: 'gogs/gogs'
restart: always
ports:
- '10022:22'
- '10080:3000'
volumes:
- /opt/docker/gogs/data:/data
links:
- mysql-gogs:db

drone-server:
image: drone/drone:0.8

ports:
- '80:8000'
- '9000'
volumes:
- /tmp/drone:/var/lib/drone/
restart: always
environment:
- DRONE_HOST=drone-server
- DRONE_GOGS=true
- DRONE_GOGS_URL=http://192.168.200.108:10080
- DRONE_GOGS_GIT_USERNAME=madongsheng
- DRONE_GOGS_GIT_PASSWORD=123456
- DRONE_OPEN=true
- DRONE_SECRET=123456

drone-agent:
image: drone/agent:0.8

command: agent
restart: always
depends_on:
- drone-server
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DRONE_SERVER=drone-server:9000
- DRONE_SECRET=123456

待续

https://segmentfault.com/a/1190000012066735
https://segmentfault.com/a/1190000012459296

参考资料

Drone Github地址
体验基于gogs+Drone搭建的CI/CD平台

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/1a623683.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!