gogs_runner/Dockhub.md
2022-07-12 14:43:30 +08:00

82 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Gogs Runner
通过Gogs内置的Webhook实现自动部署项目。最终目标是形成像gitlab-runner一样好用的CICD。
项目地址:[https://git.lacus.site/RainSun/gogs_runner](https://git.lacus.site/RainSun/gogs_runner)
## 🔨 配置项目
### 添加部署脚本
在你的项目中添加自动部署脚本(默认是`deploy.py`,当然你也可以在后续的配置文件中随便修改脚本名,[示例](https://git.lacus.site/RainSun/gogs_runner/src/master/example/deploy.py))
### 配置webhooks
屏幕右上方 -> 仓库设置 -> 管理Web钩子 -> 添加新的Web钩子 -> 选择Gogs
推送地址就是刚刚起的服务的地址数据格式默认json就好密钥文本留空剩下自己看着选
### 配置Runner
编写一个`config.json`
```json
[
{
// 名称git用户名/项目名(需要实现自动部署的项目),不可为空
"repo": "lollipopkit/example",
// 脚本文件名默认“deploy.py”
"script": "deploy.py",
// 信号未指定分支时commit messgae里包含信号则执行脚本默认“{D}”
"signal": "{D}",
// 分支指定分支只要push就部署最后一个提交无默认值可为空此配置优先于信号
"branch": "master"
}
]
```
后续将把此文件映射进容器
> 如果你所有项目都是用默认配置这个文件不写也可以的只要接收到了来自gogs的请求且commit包含`{D}`信号,就会自动执行`deploy.py`
## 🔌 把容器跑起来
这里有一份比较精简的`docker-compose.yml`
```yml
version: '3.3'
services:
gogs_runner:
image: zhaoyingbo/gogs_runner:1.0.0
container_name: gogs_runner
environment:
- TZ=Asia/Shanghai
ports:
- "3001:3001"
volumes:
- ~/.ssh:/root/.ssh # 必须
- ~/.gogs_runner/config.json:/gogs_runner/config.json
- /usr/bin/docker:/usr/bin/docker
- /var/run/docker.sock:/var/run/docker.sock
user: root
restart: always
```
### 文件系统映射
由于使用ssh克隆代码必须将.ssh映射进容器
根据实际情况修改config.json映射地址无需配置的话这行也可以删除
需要注意的是项目默认使用Python运行脚本所以是基于python:3.7构建的其余命令例如docker需要自己映射进容器
如果需要宿主机文件之类的也需要把相关文件夹映射进去
## 🎉 尝试自动部署
⚠️ **注意**,请仔细阅读下一节[要求](https://git.lolli.tech/lollipopkit/gogs-webhook#-%E8%A6%81%E6%B1%82),然后尝试自动部署。
在需要部署的项目内新建commitcommit message内需要包含刚在`config.json`内配置的`signal`(默认`{D}`然后push。 或者在配置文件指定的分支推送也可。
如果未能成功,请使用`docker logs`查看输出
## 🔖 要求
- 在仓库的webhook页面正确配置
- 部署过程需要有相应权限
- 未指定分支部署时需要在commit message中包含`{D}`字符(可以通过`config.json`参数自定义字符)
## 📝 License
`LGPL LollipopKit 2022`