kubernetes-handbook/practice/drone-ci-cd.md

92 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2022-01-18 10:29:07 +08:00
# 使用 Drone 进行持续构建与发布
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
[Drone](https://drone.io) 是一个用 Go 语言开发的基于容器运行的持续集成软件。
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
## 配置 GitHub
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
使用 Drone 对 GitHub 上的代码进行持续构建与发布,需要首先在 GitHub 上设置一个 OAuth如下
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
**1. 在 Github 上创建一个新的 OAtuh 应用 **
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
访问 [此页面](https://github.com/settings/applications/new),创建新的 OAuth 应用。
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
![OAuth 注册](../images/github-oauth-register.jpg)
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
填写应用程序的地址,因为是在本地与行,所以我们都填 `http://localhost`
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
**2. 获取 OAtuh Client ID 和 Client Secret**
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
在注册完成后就可以获得如下图所示的 OAuth Client ID 和 Client Secret保存下来我们后面要用到。
2017-10-27 11:57:44 +08:00
![OAuth key](../images/github-oauth-drone-key.jpg)
2022-01-18 10:29:07 +08:00
## 使用 docker-compose 单机运行
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
我们在本地环境,使用 docker-compose按照 [Drone 官方安装文档](http://docs.drone.io/installation/) 安装配置 Drone。
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
我们将代码托管在 GitHub 上,需要 Drone 可以持续集成和发布 GitHub 的代码,因此需要修改 `docker-compose.yaml` 文件中的 GitHub 配置。
2017-10-27 11:57:44 +08:00
```yaml
version: '2'
services:
drone-server:
2018-01-14 14:36:00 +08:00
image: drone/drone:0.8
2017-10-27 11:57:44 +08:00
ports:
- 80:8000
2018-01-14 14:36:00 +08:00
- 9000
2017-10-27 11:57:44 +08:00
volumes:
- /var/lib/drone:/var/lib/drone/
restart: always
environment:
- DRONE_OPEN=true
2018-01-14 14:36:00 +08:00
- DRONE_HOST=${DRONE_HOST}
2017-10-27 11:57:44 +08:00
- DRONE_GITHUB=true
- DRONE_GITHUB_CLIENT=${DRONE_GITHUB_CLIENT}
- DRONE_GITHUB_SECRET=${DRONE_GITHUB_SECRET}
2018-01-14 14:36:00 +08:00
- DRONE_SECRET=${DRONE_SECRET}
2017-10-27 11:57:44 +08:00
drone-agent:
2018-01-14 14:36:00 +08:00
image: drone/agent:0.8
2017-10-27 11:57:44 +08:00
command: agent
restart: always
depends_on:
- drone-server
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
2018-01-14 14:36:00 +08:00
- DRONE_SERVER=drone-server:9000
- DRONE_SECRET=${DRONE_SECRET}
2017-10-27 11:57:44 +08:00
```
2022-01-18 10:29:07 +08:00
- `/var/lib/drone` 是在本地挂载的目录,请确保该目录已存在,且可以被 docker 访问到Mac 下可以在 docker 的共享目录中配置。
- `DRONE_SECRET` 可以是一个随机的字符串,要确保 `drone-server``drone-client``DRONE_SECRET` 相同。
- `DRONE_GITHUB_CLIENT``DRONE_GITHUB_SECRET` 即在前面申请的 OAuth 的 Client ID 和 Client Secret。
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
### 启动 Drone
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
使用下面的命令在本地启动 drone
2017-10-27 11:57:44 +08:00
```bash
docker-compose up
```
2022-01-18 10:29:07 +08:00
这样是在前台启动,加上`-d` 参数就可以在后台启动。
2017-10-27 11:57:44 +08:00
2018-01-14 14:36:00 +08:00
访问 `http://localhost` 可以看到登陆画面。
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
![Drone 登陆界面](../images/drone-login-github.jpg)
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
授权后可以看到 GitHub repo 设置。
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
![Github 启用 repo 设置](../images/drone-github-active.jpg)
2017-10-27 11:57:44 +08:00
2022-01-18 10:29:07 +08:00
![Github 单个 repo 设置](../images/drone-github-repo-setting.jpg)
2017-10-27 11:57:44 +08:00
## 参考
2022-01-18 10:29:07 +08:00
- [harness/drone - github.com](https://github.com/harness/drone)
2018-01-14 14:36:00 +08:00
- [Drone 搭配 Kubernetes 升級應用程式版本 - blog.wu-boy.com](https://blog.wu-boy.com/2017/10/upgrade-kubernetes-container-using-drone/)