This commit is contained in:
2021-11-14 14:32:08 +08:00
parent f75ad8bedd
commit b0f6120151
152 changed files with 22219 additions and 8 deletions

215
content/post/drone.md Normal file
View File

@@ -0,0 +1,215 @@
---
title: "Drone 笔记"
date: 2021-02-08T17:03:13+08:00
lastmod: 2021-02-08T21:08:00+08:00
keywords: []
tags: ["drone", "cicd"]
categories: ["dev/ops"]
---
# 环境
- 操作系统 Linux x86_64
- 这里的 drone 是基于 gitea 配置的
- 安装 gitea参考[官方文档](https://docs.gitea.io/zh-cn/install-from-binary/)
- 安装 docker-ce参考[我的 docker 笔记](https://www.colben.cn/post/docker/#%E5%AE%89%E8%A3%85)
# 创建 OAuth2 应用
- 登陆 gitea
- 点击 "个人头像" - "设置" - "应用"
- 在 "管理 OAuth2 应用程序" 中,输入
- 输入应用名称: "drone"
- 重定向URI: "http://<drone 服务器地址>:<drone 服务器端口>/login"
- 点击 "创建应用",在弹出的新页面中
- 记录 "客户端 ID"
- 记录 "客户端密钥"
- 点击 "保存"
# 安装 drone
- 下载 docker 镜像
```bash
docker pull drone/drone
```
- 启动容器
```bash
docker run -d \
--name drone \
-e DRONE_GITEA_SERVER=http://<gitea 服务器地址>:<gitea 服务器端口> \
-e DRONE_GITEA_CLIENT_ID=<客户端 ID> \
-e DRONE_GITEA_CLIENT_SECRET=<客户端密钥> \
-e DRONE_RPC_SECRET=1111aaaa2222bbbb3333cccc4444dddd \
-e DRONE_SERVER_HOST=<drone 服务器地址>:<drone 服务器端口> \
-e DRONE_SERVER_PROTO=http \
-e DRONE_GIT_ALWAYS_AUTH=true \
-p <drone 服务器端口>:80 \
-v <外挂的 drone 数据目录>:/data \
drone/drone
```
- 参考链接[https://docs.drone.io/server/provider/gitea/](https://docs.drone.io/server/provider/gitea/)
# 安装 drone runner
## 安装 docker runner
- 下载 drone-runner-docker 镜像
```bash
docker pull drone/drone-runner-docker
```
- 启动容器
```bash
docker run -d \
--name drone_runner_docker \
-e DRONE_RPC_PROTO=http \
-e DRONE_RPC_HOST=<drone 服务器地址>:<drone 服务器端口> \
-e DRONE_RPC_SECRET=1111aaaa2222bbbb3333cccc4444dddd \
-e DRONE_RUNNER_CAPACITY=10 \
-e DRONE_RUNNER_NAME=<该 runner 的名字> \
-e DRONE_RUNNER_LABELS=<key1>:<value1> \
-p <runner 端口>:3000 \
-v /var/run/docker.sock:/var/run/docker.sock \
drone/drone-runner-docker
```
- 参考链接[https://docs.drone.io/runner/docker/installation/linux/](https://docs.drone.io/runner/docker/installation/linux/)
## 安装 exec runner
- 下载
```bash
curl -L https://github.com/drone-runners/drone-runner-exec/releases/latest/download/drone_runner_exec_linux_amd64.tar.gz \
| tar zxf -C /usr/local/bin
chmod 0755 /usr/local/bin/drone-runner-exec
```
- 创建配置文件
```bash
cat > /etc/drone-runner-exec/config <<-EOF
DRONE_RPC_PROTO=http
DRONE_RPC_HOST=<drone 服务器地址>:<drone 服务器端口>
DRONE_RPC_SECRET=1111aaaa2222bbbb3333cccc4444dddd
DRONE_HTTP_BIND=:<runner 端口>
DRONE_LOG_FILE=/var/log/drone-runner-exec/log.txt
DRONE_RUNNER_LABELS=<key1>:<value1>
EOF
```
- 创建 systemd service 文件
```bash
cat > /etc/systemd/system/drone-runner-exec.service <<-EOF
[Unit]
Description=Drone Exec Runner
ConditionFileIsExecutable=/usr/local/bin/drone-runner-exec
[Service]
ExecStart=/usr/local/bin/drone-runner-exec "service" "run" "--config" "/etc/drone-runner-exec/config"
StartLimitInterval=5
StartLimitBurst=10
Restart=on-failure
RestartSec=120
[Install]
WantedBy=multi-user.target
EOF
```
- 参考链接[https://docs.drone.io/runner/exec/installation/linux/](https://docs.drone.io/runner/exec/installation/linux/)
# 使用 drone
- 登陆 drone: http://<drone 服务器地址>:<drone 服务器端口>,此时会跳转到 gitea 登陆界面
- 登陆成功后,浏览器返回 drone 首页,这里会显示我们创建/参与的 git 项目
- 选择一个项目,点击对应的 "ACTIVATE"drone 会打开该项目的 "SETTINGS" 页面,这里一般无需设置,默认即可
- 在该项目的 "ACTIVITY FEED" 页面会显示每次项目提交后触发的 CI/CD 流程
- 编辑该项目代码,在项目根目录下创建文件 .drone.yml内容如下
- docker pipeline 示例,详细参考[https://docs.drone.io/pipeline/docker/overview/](https://docs.drone.io/pipeline/docker/overview/)
```yaml
---
kind: pipeline
type: docker
name: default
steps:
- name: greeting
image: golang:1.12
commands:
- go build
- go test
node:
<key1>: <value1>
```
- exec pipeline 示例,详情参考[https://docs.drone.io/pipeline/exec/overview/](https://docs.drone.io/pipeline/exec/overview/)
```yaml
---
kind: pipeline
type: exec
name: default
platform:
os: linux
arch: amd64
steps:
- name: greeting
commands:
- echo hello world
node:
<key1>: <value1>
```
- 其他 pipeline 参考[https://docs.drone.io/pipeline/overview/](https://docs.drone.io/pipeline/overview/)
# 适配 sonarqube
- 下载镜像
```bash
docker pull sonarqube
```
- 启动 sonarqube 容器
```bash
docker run -d \
--name sonarqube \
-p <sonarqube 服务器端口>:9000 \
-v sonarqube_data:/opt/sonarqube/data \
-v sonarqube_extension:/opt/sonarqube/extensions \
-v sonarqube_log:/opt/sonarqube/logs \
sonarqube
```
- 登陆 sonarquebe: http://<sonarqube 服务器地址>:<sonarqube 服务器端口>,创建用户,获取 token
- 在 gitea 对应项目根目录下创建 .drone.yml内容如下
```yaml
kind: pipeline
type: docker
name: 代码分析
steps:
- name: 代码分析
image: aosapps/drone-sonar-plugin
settings:
sonar_host: http://<sonarqube 服务器地址>:<sonarqube 服务器端口>
sonar_token: <sonarqube 用户 token>
node:
role: sonarqube
```
- 在 gitea 对应项目根目录下创建 sonar-project.properties内容如下
```
sonar.projectKey={项目名称}
sonar.sources=.
```
- 提交代码,在 drone 中对应项目的 "ACTIVITY FEED" 页面下查看 pipeline 执行过程
- pipeline 执行完成后,浏览器打开 http://<sonarqube 服务器地址>:<sonarqube 服务器端口>,即可查看刚检测完成的项目
- sonarqube pipeline 参考[https://hub.docker.com/r/aosapps/drone-sonar-plugin](https://hub.docker.com/r/aosapps/drone-sonar-plugin)
- sonarqube 配置参考[https://docs.sonarqube.org/latest/analysis/analysis-parameters/](https://docs.sonarqube.org/latest/analysis/analysis-parameters/)