自建Git服务Gogs和CI/CD服务Drone

项目:https://gogs.io

Gogs运行

docker run -d --name=gogs -p 10022:22 -p 10088:3000 -v /storage/app/gogs:/data gogs/gogs

挂载在/storage/app/gogs

配置文件:

APP_NAME = 内部协作开发仓库
RUN_USER = git
RUN_MODE = prod

[database]
DB_TYPE  = mysql
HOST     = 192.:13306
NAME     = gogs
USER     = root
PASSWD   = 
SSL_MODE = disable
PATH     = data/gogs.db

[repository]
ROOT = /data/git/gogs-repositories
ENABLE_LOCAL_PATH_MIGRATION = true  
FORCE_PRIVATE = false 
DISABLE_HTTP_GIT = false

[server]
DOMAIN           = 192.
HTTP_PORT        = 3000
ROOT_URL         = http://192.:10088/
DISABLE_SSH      = false
SSH_PORT         = 10022
START_SSH_SERVER = false
OFFLINE_MODE     = false

[mailer]
ENABLED = true
HOST= smtp.exmail.qq.com:465
USER= 
PASSWD=
FROM=`xx <xx@xxx.com>`
SUBJECT_PREFIX=`[xxx提醒你]`

[service]
REGISTER_EMAIL_CONFIRM = true
ENABLE_NOTIFY_MAIL     = true
DISABLE_REGISTRATION   = false
ENABLE_CAPTCHA         = true
REQUIRE_SIGNIN_VIEW    = true

[picture]
DISABLE_GRAVATAR        = false
ENABLE_FEDERATED_AVATAR = true

[session]
PROVIDER = file

[log]
MODE      = file
LEVEL     = Info
ROOT_PATH = /data/logs

[security]
INSTALL_LOCK = true
SECRET_KEY   = Tgjni2mMDkKqB88

Gogs备份容灾

需要定时备份数据库:

# gogs_backup.sh

mysqldump -uroot -pxxx -h192. -P13306 gogs > /backup/sql/mygogs_`date +%Y%m%d_%H%M%S`.sql
  
#删除一周之前的备份文件
find /backup/sql/mygogs* -type f -mtime +7 -exec rm -f {} \;

定时器:

#每天凌晨5点备份
0 5 * * * /bin/bash -x /backup/gogs_backup.sh > /dev/null 2>&1

和同步/storage/app/gogs文件夹:

# gogs_backup2.sh

nohup rsync -e 'ssh -p 22' -avL --delete --exclude "logs" --exclude "ssh" --exclude "gogs/data/sessions" --exclude "gogs/git/.ssh" superpika@192.168.0.101:/storage/app/gogs /backup/data >> /backup/data/rsync.log 2>&1 &

定时器:

# 每30分一次同步
30 * * * * /bin/bash -x /backup/gogs_backup2.sh > /dev/null 2>&1

需要添加:

vim ~/.ssh/authorized_keys

搭建Drone

编辑以下 docker-compose.yaml, 只需更改http://192.:10088:

version: '2'
services:
  # 服务器主端
  drone-server:
    container_name: "mydrone-server"
    image: drone/drone:latest
    # 端口开放8080
    ports:
      - 8080:8000
      - 9000
    volumes:
      - /var/lib/drone:/var/lib/drone/
    restart: always
    environment:
      - DRONE_OPEN=true
      - DRONE_HOST=http://127.0.0.1
      # Gogs必备
      - DRONE_GOGS=true
      - DRONE_GOGS_URL=http://192.:10088
      - DRONE_SECRET=xxxigogo

  # 从端
  drone-agent:
    container_name: "mydrone-agent"
    image: drone/agent:latest

    command: agent
    restart: always
    depends_on:
      - drone-server
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_SERVER=drone-server:9000
      - DRONE_SECRET=xxxigogo

启动:

docker-compose up -d

我们开放了8080端口, 打开127.0.0.1:8080登录使用gogs帐号!

Drone实践

我们先登录Gogs:http://192.:10088, 建立一个仓库testmydrone.

先提交:

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin ssh://git@192.:10022/xxxx/testmydrone.git
git push -u origin master

然后登录http://127.0.0.1:8080勾选项目!

勾选完之后, 我们在我们的仓库新建一个文件.drone.yml

pipeline:
  backend:
    image: golang
    commands:
      - go build

还有一个源码main.go:

package main

func main(){
}

提交:

git add --all
git commit -m "push"
git push