Drone+Gitea自动化部署

本使用Vagrant搭建虚拟环境

新建一个虚拟机ip为192.168.56.100

1.安装私有仓库harbor

下载地址:https://github.com/goharbor/harbor/releases

1
2
3
4
$ vagrant ssh
#密码vagrant
$ sudo su
$ tar xvf harbor-offline-installer-v1.10.1.tgz

编辑harbor.yml文件

1
2
$ cd ./harbor
$ vi harbor.yml

安装执行install.sh

1
$ ./install.sh

编辑 /etc/docker/daemon.json

1
2
3
4
{
"insecure-registries":["192.168.56.100"],
"registry-mirrors": ["https://c8x7u9xt.mirror.aliyuncs.com"]
}

重启docker:systemctl restart docker

重启harbor

登录

1
2
3
$ docker login 192.168.56.100
#用户名: admin
#密码: Harbor12345

浏览器访问 192.168.56.100

2.安装Gitea

docker-compose.yml

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
version: '3'

services:
mysql:
image: mysql:5.7
container_name: gitea_mysql
restart: always
ports:
- "13306:3306"
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_USER=gitea
- MYSQL_PASSWORD=gitea
- MYSQL_DATABASE=gitea
- MYSQL_DATABASE_CHARSET=utf8mb4
- MYSQL_DATABASE_COLLATION=utf8mb4_general_ci
volumes:
- /root/docker/data/gitea/mysql:/var/lib/mysql
gitea:
image: gitea/gitea:latest
container_name: gitea
restart: always
ports:
- "10022:22"
- "10080:3000"
environment:
- TZ=Asia/Shanghai
- DB_TYPE=mysql
- DB_HOST=gitea_mysql:3306
- DB_NAME=gitea
- DB_USER=gitea
- DB_PASSWD=gitea
volumes:
- /root/docker/data/gitea/gitea:/data
depends_on:
- mysql

安装

docker-compose up -d

打开浏览器访问 http://192.168.56.102:10080/install

配置数据库

基础配置

设置账号

3.安装Drone

生成secret用于Drone runners和server的通信:

1
2
openssl rand -hex 16
4af3a6ee506d2a06273f3583224983e0

记住客户端ID与客户端密钥,将用于docker-compse.yml中

docker-compose.yml

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
version: '3'

services:
mysql:
image: mysql:5.7
container_name: drone_mysql
restart: always
ports:
- "13307:3306"
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_USER=drone
- MYSQL_PASSWORD=drone
- MYSQL_DATABASE=drone
- MYSQL_DATABASE_CHARSET=utf8mb4
- MYSQL_DATABASE_COLLATION=utf8mb4_general_ci
volumes:
- /root/docker/data/drone/mysql:/var/lib/mysql
drone-server:
image: drone/drone:latest
container_name: drone
restart: always
ports:
- 10081:80
- 8443:443
environment:
- TZ=Asia/Shanghai
- DRONE_AGENTS_ENABLED=true
- DRONE_GITEA_SERVER=http://192.168.56.100:10080/
- DRONE_GITEA_CLIENT_ID=e2a50d23-2a78-4bcd-8fa2-b656bb944904
- DRONE_GITEA_CLIENT_SECRET=VGLOi5YC9XgauXcHbNTpAMpPcUnK6ALdR7hgatCV0ic=
- DRONE_RPC_SECRET=5d1789d5aa2ee55e6a5b956bec3c328f
- DRONE_SERVER_HOST=192.168.56.100:10081
- DRONE_SERVER_PROTO=http
# gitea的用户名,否则没有
- DRONE_USER_CREATE=username:gitea,admin:true
# - DRONE_LOGS_DEBUG=true
- DRONE_GIT_USERNAME=gitea
- DRONE_GIT_PASSWORD=gitea
- DRONE_GIT_ALWAYS_AUTH=false
- DRONE_DATABASE_DRIVER=mysql
- DRONE_DATABASE_DATASOURCE=drone:drone@tcp(drone_mysql:3306)/drone?parseTime=true
volumes:
- /root/docker/data/drone/drone:/data
depends_on:
- mysql
drone-agent:
image: drone/agent:latest
container_name: runner
restart: always
ports:
- 13000:3000
environment:
- TZ=Asia/Shanghai
- DRONE_RPC_PROTO=http
- DRONE_RPC_HOST=drone
- DRONE_RPC_SECRET=5d1789d5aa2ee55e6a5b956bec3c328f
- DRONE_RUNNER_CAPACITY=2
- DRONE_RUNNER_NAME=${HOSTNAME}
- DRONE_DEBUG=true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- drone-server

注意:DRONE_USER_CREATE=username:gitea,admin:true中username是gitea的账号

执行命令docker-compose up -d

访问192.168.56.100:10081,完成gitea的授权

在harbor新建一个项目wmicro

在项目根目录中编写 .drone.yml

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
kind: pipeline
name: wmicro

steps:
- name: build micro
image: golang:1.13.6-alpine3.11
volumes:
- name: cache
path: /go
- name: gocache
path: /root/.cache/go-build
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOPROXY: https://mirrors.aliyun.com/goproxy,direct
commands:
- go version
- go env
- cd gate/micro
- go build -o micro
- name: build dd
image: golang:1.13.6-alpine3.11
volumes:
- name: cache
path: /go
- name: gocache
path: /root/.cache/go-build
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOPROXY: https://mirrors.aliyun.com/goproxy,direct
commands:
- go version
- go env
- cd web/dd
- go build -o dd
- name: build gid
image: golang:1.13.6-alpine3.11
volumes:
- name: cache
path: /go
- name: gocache
path: /root/.cache/go-build
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOPROXY: https://mirrors.aliyun.com/goproxy,direct
commands:
- go version
- go env
- cd srv/gid
- go build -o gid
- name: build passport
image: golang:1.13.6-alpine3.11
volumes:
- name: cache
path: /go
- name: gocache
path: /root/.cache/go-build
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOPROXY: https://mirrors.aliyun.com/goproxy,direct
commands:
- go version
- go env
- cd srv/passport
- go build -o passport
- name: build user
image: golang:1.13.6-alpine3.11
volumes:
- name: cache
path: /go
- name: gocache
path: /root/.cache/go-build
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOPROXY: https://mirrors.aliyun.com/goproxy,direct
commands:
- go version
- go env
- cd srv/user
- go build -o user
- name: publish micro
image: plugins/docker:18.09
settings:
username: admin
password: Harbor12345
context: gate/micro
dockerfile: gate/micro/Dockerfile
repo: 192.168.56.100/wmicro/micro
registry: 192.168.56.100
tags: latest
insecure: true
- name: publish dd
image: plugins/docker:18.09
settings:
username: admin
password: Harbor12345
context: web/dd
dockerfile: web/dd/Dockerfile
repo: 192.168.56.100/wmicro/dd
registry: 192.168.56.100
tags: latest
insecure: true
- name: publish gid
image: plugins/docker:18.09
settings:
username: admin
password: Harbor12345
context: srv/gid
dockerfile: srv/gid/Dockerfile
repo: 192.168.56.100/wmicro/gid
registry: 192.168.56.100
tags: latest
insecure: true
- name: publish passport
image: plugins/docker:18.09
settings:
username: admin
password: Harbor12345
context: srv/passport
dockerfile: srv/passport/Dockerfile
repo: 192.168.56.100/wmicro/passport
registry: 192.168.56.100
tags: latest
insecure: true
- name: publish user
image: plugins/docker:18.09
settings:
username: admin
password: Harbor12345
context: srv/user
dockerfile: srv/user/Dockerfile
repo: 192.168.56.100/wmicro/user
registry: 192.168.56.100
tags: latest
insecure: true
- name: deploy micro
image: appleboy/drone-ssh:1.5.5
settings:
host: 192.168.56.100
username: root
password: vagrant
port: 22
script:
- docker pull 192.168.56.100/wmicro/micro
- docker rm -f micro || true
- docker image prune -f
- docker run -d --name micro -p 8082:8082 192.168.56.100/wmicro/micro web
- name: deploy dd
image: appleboy/drone-ssh:1.5.5
settings:
host: 192.168.56.100
username: root
password: vagrant
port: 22
script:
- docker pull 192.168.56.100/wmicro/dd
- docker rm -f dd || true
- docker image prune -f
- docker run -d --name dd 192.168.56.100/wmicro/dd
- name: deploy gid
image: appleboy/drone-ssh:1.5.5
settings:
host: 192.168.56.100
username: root
password: vagrant
port: 22
script:
- docker pull 192.168.56.100/wmicro/gid
- docker rm -f gid || true
- docker image prune -f
- docker run -d --name gid 192.168.56.100/wmicro/gid
- name: deploy passport
image: appleboy/drone-ssh:1.5.5
settings:
host: 192.168.56.100
username: root
password: vagrant
port: 22
script:
- docker pull 192.168.56.100/wmicro/passport
- docker rm -f passport || true
- docker image prune -f
- docker run -d --name passport 192.168.56.100/wmicro/passport
- name: deploy user
image: appleboy/drone-ssh:1.5.5
settings:
host: 192.168.56.100
username: root
password: vagrant
port: 22
script:
- docker pull 192.168.56.100/wmicro/user
- docker rm -f user || true
- docker image prune -f
- docker run -d --name user 192.168.56.100/wmicro/user
volumes:
- name: cache
host:
path: /tmp/drone-cache/go
- name: gocache
host:
path: /tmp/drone-cache/go_build