目录

让 Gitea 支持 Drone 持续集成工具

简介

Drone是一个现代的持续集成工具

Drone by Harness™ is a modern Continuous Integration platform that empowers busy teams to automate their build, test and release workflows using a powerful, cloud native pipeline engine.

这几天心血来潮, 在自己的服务器上部署了一个Drone, 配合很久以前自己搭建的Gitea私有git仓库一起使用

让我们开始吧!

在gitea上创建 OAuth2 Application

https://static-1251996892.file.myqcloud.com/img/markdown/2021/gitea-create-oauth2-app.png

记下生成的client idclient key, 后面要用

部署Drone

我使用了drone推荐的容器部署方式, 官方直接用了docker run, 我改用docker-compose, 并且把dronedrone-runner-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
28
29
version: "3"
services:
  drone:
    container_name: drone
    image: drone/drone:2
    volumes:
       - ./drone:/data
    ports:
      - 8041:80
    environment:
      DRONE_GITEA_SERVER: https://git.yourdomain.com
      DRONE_GITEA_CLIENT_ID: 05136e57d80189bef462
      DRONE_GITEA_CLIENT_SECRET: 7c229228a77d2cbddaa61ddc78d45e
      DRONE_RPC_SECRET: super-duper-secret
      DRONE_SERVER_HOST: drone.yourdomain.com
      DRONE_SERVER_PROTO: https
    restart: always
  drone-runner-docker:
    container_name: drone-runner-docker
    image: drone/drone-runner-docker:1
    volumes:
       - /var/run/docker.sock:/var/run/docker.sock
    environment:
      DRONE_RPC_PROTO: https
      DRONE_RPC_HOST: drone.yourdomain.com
      DRONE_RPC_SECRET: super-duper-secret
      DRONE_RUNNER_CAPACITY: 2
      DRONE_RUNNER_NAME: drone-runner
    restart: always

上面的环境变量我就直接贴出官网的解释了

  • drone

DRONE_GITEA_CLIENT_ID
Required string value provides your Gitea oauth Client ID.
DRONE_GITEA_CLIENT_SECRET
Required string value provides your Gitea oauth Client Secret.
DRONE_GITEA_SERVER
Required string value provides your Gitea server address. For example https://gitea.company.com, note the http(s) otherwise you’ll see an error with “unsupported protocol scheme” from Gitea.
DRONE_GIT_ALWAYS_AUTH
Optional boolean value configures Drone to authenticate when cloning public repositories.
DRONE_RPC_SECRET
Required string value provides the shared secret generated in the previous step. This is used to authenticate the rpc connection between the server and runners. The server and runner must be provided the same secret value.
DRONE_SERVER_HOST
Required string value provides your external hostname or IP address. If using an IP address you may include the port. For example drone.company.com.
DRONE_SERVER_PROTO
Required string value provides your external protocol scheme. This value should be set to http or https. This field defaults to https if you configure ssl or acme.

  • drone-runner-docker

DRONE_RPC_HOST
provides the hostname (and optional port) of your Drone server. The runner connects to the server at the host address to receive pipelines for execution.
DRONE_RPC_PROTO
provides the protocol used to connect to your Drone server. The value must be either http or https.
DRONE_RPC_SECRET
provides the shared secret used to authenticate with your Drone server. This must match the secret defined in your Drone server configuration.

启用drone-runner-exec

drone-runner-exec用于在宿主机直接执行脚本, 需要独立安装

1
2
3
4
5
curl -L https://github.com/drone-runners/drone-runner-exec/releases/latest/download/drone_runner_exec_linux_amd64.tar.gz | tar zx
sudo install -t /usr/local/bin drone-runner-exec
sudo mkdir /var/log/drone-runner-exec
sudo mkdir /etc/drone-runner-exec
sudo vim /etc/drone-runner-exec/config

/etc/drone-runner-exec/config :

1
2
3
4
DRONE_RPC_PROTO=https
DRONE_RPC_HOST=drone.company.com
DRONE_RPC_SECRET=super-duper-secret
DRONE_LOG_FILE=/var/log/drone-runner-exec/log.txt
1
2
3
drone-runner-exec service install
drone-runner-exec service start
sudo systemctl enable drone-runner-exec

测试

创建一个新的golang项目test-drone

main.go:

1
2
3
4
5
6
7
package main

import "log"

func main() {
	log.Println("hello drone")
}

编写.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
# go 测试
---
kind: pipeline
type: docker
name: go test

steps:
- name: test
  image: golang:1.16
  pull: if-not-exists
  commands:
  - go test
  - go build

# push image
# 这里向本地私有仓库推送了一个镜像, 因此要使用 insecure: true
---
kind: pipeline
type: docker
name: push image
steps:
- name: publish
  image: plugins/docker
  pull: if-not-exists
  settings:
    registry: 192.168.7.20:5000
    insecure: true
    repo: 192.168.7.20:5000/lomot/test-drone
    tags: [ latest, 1.0, 1 ]

# exec cmd
---
kind: pipeline
type: exec
name: test exec
platform:
  os: linux
  arch: amd64
steps:
- name: test-exec
  commands:
  - echo test runner exec

结果

https://static-1251996892.file.myqcloud.com/img/markdown/2021/drone-test-build-img.png

https://static-1251996892.file.myqcloud.com/img/markdown/2021/drone-test-build-log.png

https://docs.drone.io/server/provider/gitea/

https://docs.drone.io/runner/exec/installation/linux/