Lightweight CI/CD automatic construction platform Gitea+Drone construction

build environment

ip Role
10.0.7.20 Guide
10.0.7.40 drone server/runner

Build Gitea

This article uses the docker method to build

cat  docker-compose.yaml

version: "3"

networks:
  gitea:
    external: false

services:
  server:
    image: gitea/gitea:latest
    container_name: gitea
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - DB_TYPE=mysql
      - DB_HOST=db:3306
      - DB_NAME=gitea
      - DB_USER=gitea
      - DB_PASSWD=gitea
    restart: always
    networks:
      - gitea
    volumes:
      - ./gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3000:3000"
      - "222:22"
    depends_on:
      - db

  db:
    image: mysql:8
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=gitea
      - MYSQL_USER=gitea
      - MYSQL_PASSWORD=gitea
      - MYSQL_DATABASE=gitea
    networks:
      - gitea
    volumes:
      - mysql_db:/var/lib/mysql

volumes:
  mysql_db:

run Gitea

docker-compose up -d

View running results

[root@10 gitea]# docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS                                                                     NAMES
a3f073d7d034   gitea/gitea:latest   "/usr/bin/entrypoint…"   42 seconds ago   Up 11 seconds   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp, 0.0.0.0:222->22/tcp, :::222->22/tcp   gitea
8db614e594b5   mysql:8              "docker-entrypoint.s…"   42 seconds ago   Up 41 seconds   3306/tcp, 33060/tcp

Setting up and accessing web pages

http://10.0.7.20:3000

Fill in the server address
insert image description here

Administrator account settings
insert image description here

Administrator account login
insert image description here

insert image description here
Create a git repository
insert image description here

Configure Gitea

Create and manage OAuth2 applications


insert image description here

Configure webhooks

insert image description here

Drone server and runner construction

Configure server/runner

[root@10 drone]# cat docker-compose.yaml
version: "3"

networks:
  drone:


volumes:
  drone_data:

services:
  server:
    image: drone/drone
    container_name: drone
    restart: always
    environment:
      - DRONE_GITEA_SERVER=http://10.0.7.20:3000
      - DRONE_GITEA_CLIENT_ID=f5028918-5a2c-4db7-9f07-5f78c56bb11e  ###(客户端ID)
      - DRONE_GITEA_CLIENT_SECRET=YKXuTXorxZw3MVswjg1dUJyPLEcitLdZ3vC3jAuPVXah ####(客户端秘钥)
      - DRONE_RPC_SECRET=super-duper-secret 
      - DRONE_SERVER_HOST=10.0.7.40 ####(drone服务器地址)
      - DRONE_SERVER_PROTO=http ###(http协议)
      - DRONE_GIT_ALWAYS_AUTH=true
      - DRONE_GIT_USERNAME=(gitea-应用-tokens用户)
      - DRONE_GIT_PASSWORD=(gitea-应用-tokens用户令牌)
      - DRONE_USER_CREATE=username:root,admin:true,,token:55f24eb3d61ef6ac5e83d550178638dc (用于生成admin管理员,用于docker容器挂载本地文件使用,否则无法挂载提示untrusted repositories cannot mount host volumes)
      - TZ=Asia/Shanghai
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - drone_data:/data
    networks:
      - drone

  runner:
    image: drone/drone-runner-docker
    container_name: runner
    restart: always
    environment:
      - DRONE_RPC_PROTO=http
      - DRONE_RPC_HOST=10.0.7.40
      - DRONE_RPC_SECRET=super-duper-secret  ####(与server端一致即可)
      - DRONE_RUNNER_CAPACITY=4
      - DRONE_RUNNER_NAME=runner
      - TZ=Asia/Shanghai
      - DRONE_DEBUG=true
      - DRONE_TRACE=true
        #- DRONE_UI_USERNAME=root
        #- DRONE_UI_PASSWORD=
    ports:
      - "3000:3000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - drone
    depends_on:
      - server

  ssh:   ####(drone 用户支持ssh 管道服务)
    image: drone/drone-runner-ssh
    container_name: ssh
    restart: always
    environment:
      - DRONE_RPC_PROTO=http
      - DRONE_RPC_HOST=10.0.7.40
      - DRONE_RPC_SECRET=super-duper-secret
    ports:
      - "4000:3000"
    networks:
      - drone

start up

insert image description here

visit drone

http://10.0.7.40

insert image description here
insert image description here
insert image description here

Test Gitea-webhook

test gieta we-hook
insert image description here

Solution to webhook failure problem (supplementary explanation)

You need to configure the gitea/conf/app.ini that allows the webhook ip
to modify the mapping in gitea, and add the following content

[webhook]
ALLOWED_HOST_LIST = 10.0.7.0/16

Drone uses docker pipeline instance

1. View gitea and drone

insert image description here

2. Create and submit .drone.yaml

[root@10 project]# ls -al
总用量 8
drwxr-xr-x  3 root root  53 825 16:57 .
drwxr-xr-x. 5 root root  54 825 15:15 ..
-rw-r--r--  1 root root 125 815 16:22 .drone.yml
drwxr-xr-x  8 root root 166 825 16:57 .git
-rw-r--r--  1 root root  12 825 15:49 README.md

cat .drone.yml

kind: pipeline
type: docker
name: default

steps:
- name: greeting
  image: alpine
  commands:
  - echo hello
  - echo world

3. View the running results

After git executes fiirt commit, drone executes the specified command successfully
insert image description here
insert image description here

4. User Interface Display


If there is no option as shown above, it means that the admin user is not used. Please refer to the above drone's compose file to reset.
insert image description here

Drone uses the command line pipeline instance

1. Install drone-runner-exec

Need to install drone-runner-exec on the server where the runner is located (10.0.7.40)

方式一
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

方式二
浏览器地址栏输入https://github.com/drone-runners/drone-runner-exec/releases/latest/download/drone_runner_exec_linux_amd64.tar.gz 
下载成功后,将tar包上传到drone-runner所在服务器
执行:
tar -zxvf drone_runner_exec_linux_amd64.tar.gz -C /usr/sbin

[root@10 ~]# ls /usr/sbin/drone*
/usr/sbin/drone-runner-exec

配置drone-runner-exec 环境变量
mkdir /etc/drone-runner-exec
touch /etc/drone-runner-exec/config
vim /etc/drone-runner-exec/config (以下3个参数对照上述docker-compose文件填写)
DRONE_RPC_PROTO=http
DRONE_RPC_HOST=10.0.7.40
DRONE_RPC_SECRET=super-duper-secret

启动drone-runner-exec
drone-runner-exec service install
drone-runner-exec service start (启动命令)
drone-runner-exec service stop (停止命令)

注: exec问题谢谢网友分享,参考链接
https://blog.csdn.net/weixin_36766709/article/details/116795778

2. Change and commit .drone.yaml

[root@10 project]# cat .drone.yml
kind: pipeline
type: exec
name: default

platform:
  os: linux
  arch: amd64

steps:
- name: greeting
  commands:
  - echo Linux yyds 

3. View the running results

insert image description here

insert image description here

Drone uses ssh pipeline instance

The drone/drone-runner-ssh container has been deployed in the drone server/runner dockr-compose file before

1. Install and use drone/drone-runner-ssh

insert image description here

2. Change and commit .drone.yaml

1. Remote server address 10.0.7.0

在10.0.7.0 中创建 /tmp/drone_test.log
echo "third commit" >> /tmp/drone_test.log

2. Modify .drone.yaml

kind: pipeline
type: ssh
name: default


server:
  host: 10.0.7.0
  user: root
  password: 1@3$qWeR

steps:
- name: greeting
  commands:
  - cat /tmp/drone_test.log

3. Check the running results

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44157851/article/details/126350649