CICD篇:使用Drone CI来部署GitHub项目的丝滑体验
# 什么是Drone CI
Drone是一个半开源的ci工具,可以轻松进行自动化软件构建和测试,支持docker Pipelines、K8s Pipelines或Exec Pipelines等,涵盖广,插件丰富,国外使用率高。确定就是社区不大,文档一般,需要花时间了解。
相对与gitlab CI、或Jenkins,具有配置要求低,接入各种git仓库方便
# 安装
# 环境
docker
代理
nginx 或者 treafik
# docker-compose.yml
services:
drone:
restart: always
image: drone/drone:latest
container_name: drone
volumes:
- ./drone:/var/lib/drone
- ./data:/data
environment:
# 这里设置github用户名为admin,不然以管理员管理
- DRONE_USER_CREATE=username:xxx,admin:true
- DRONE_GITHUB_CLIENT_ID=xxx
- DRONE_GITHUB_CLIENT_SECRET=xxx
- DRONE_SERVER_HOST=xxx.xxx.cn
- DRONE_SERVER_PROTO=https
- DRONE_RPC_SECRET=xxx
- DRONE_USER_FILTER=xxx
- DRONE_JSONNET_ENABLED=true
networks:
- proxy
drone-runner:
restart: always
image: drone/drone-runner-docker:latest
container_name: drone-runner
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DRONE_RPC_PROTO=https
- DRONE_RPC_HOST=xxx.xxx.cn
- DRONE_RUNNER_CAPACITY=5
- DRONE_RPC_SECRET=xxx
networks:
- proxy
networks:
proxy:
external: true
# 启动
docker compose up -d
配置好代理后,打开配置的域名
# 使用
# 同步一个项目
选择一个项目开启ci,配置所需密钥和ci路径
# 在项目创建.drone.jsonnet ci文件
├── .drone.jsonnet
├── README.md
├── docker-compose.yml
├── source
└── static-web.yml
# .drone.jsonnet
local NAME="static-web";
local ROOT="/data/docker/awei/"+NAME+"/";
local SOURCE=ROOT+"source/";
local TRAEFIK_ROOT="/data/docker/traefik/data/configurations/";
[
{
"kind": "pipeline",
"type": "docker",
"name": "deploy",
"steps": [
{
"name": "copy",
"image": "node:14.17.1-alpine",
"volumes": [
{
"name": "traefik-conf",
"path": TRAEFIK_ROOT
},
{
"name": "root-conf",
"path": ROOT
}
],
"commands": [
"mkdir -p "+SOURCE, # 创建源码目录
"rm -rf "+SOURCE+"*", # 删除以前的源码,这会导致docker挂载的文件失效,所以后面直接docker up -d将不会重新挂载,需要docker stop && docker rm -f再重新up
"cp -rf source/* "+SOURCE,
"cp -f docker-compose.yml "+ROOT,
"cp -f static-web.yml "+TRAEFIK_ROOT+"static-web.yml",
]
},
{
"name": "up",
"image": "appleboy/drone-ssh",
"settings": {
"host": "bstu.cn",
"username": "root",
"key": {
"from_secret": "drone_id_rsa"
},
"port": 22,
"command_timeout": "10m",
"script_stop": false,
"script": [
"cd "+ROOT,
// "docker compose stop",
// // 由于上面统一删除了source,所以这里只能rm重新up
// "docker compose rm -f",
"docker compose up -d",
"docker compose restart"
]
}
},
{
"name": "notify",
"pull": "if-not-exists",
"image": "guoxudongdocker/drone-dingtalk:latest",
"settings": {
"token": {
"from_secret": "dingtalk_token"
},
"type": "markdown",
"message_color": true,
"message_pic": true,
"sha_link": true
},
"when": {
"status": [
"failure",
"success"
]
}
}
],
"volumes": [
{
"name": "traefik-conf",
"host": {
"path": TRAEFIK_ROOT
}
},
{
"name": "root-conf",
"host": {
"path": ROOT
}
}
]
}
]
# ci运行测试
push代码到github的main分支drone ci自动运行
^_^ Be the first to comment.
Comment
Nickname
Email (Reply notice)
Website
Content (can use Markdown syntax)