gogs_runner/README.md
2022-06-26 10:40:11 +08:00

120 lines
3.6 KiB
Markdown
Raw Permalink 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 Webhook Deploy
通过Gogs内置的Webhook实现自动部署项目。
## 🔨 步骤
### 1 安装
在需要实现自动部署的机器上安装本项目。
#### 1.1 构建
```shell
# clone至本地
git clone https://git.lolli.tech/lollipopkit/gogs-webhook
# 构建
go build
```
#### 1.2 开机自启
##### 1.2.1 适用于root用户的操作
<details>
<summary>点击查看</summary>
`/etc/systemd/system`目录下创建一个文件,命名为`gogs-webhook-deploy.service`,内容如下:
```shell
[Unit]
Description=Gogs Webhook Deploy Service
After=network.target
[Service]
Type=simple
User=root
Restart=on-failure
RestartSec=5s
ExecStart={ABSOLUTE_PATH_TO_GOGS_WEBHOOK_DIR}/gogs-webhook -a ":3001"
WorkingDirectory={ABSOLUTE_PATH_TO_GOGS_WEBHOOK_DIR}
[Install]
WantedBy=multi-user.target
```
`{ABSOLUTE_PATH_TO_GOGS_WEBHOOK_DIR}`为Clone的目录绝对路径
然后执行:
```shell
systemctl enable --now gogs-webhook-deploy.service
```
</details>
##### 1.2.2 适用于非root用户
`~/.config/systemd/user/`目录下创建一个文件,命名为`gogs-webhook-deploy.service`,内容如下:
```shell
[Unit]
Description=Gogs Webhook Deploy Service
After=network.target
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart={ABSOLUTE_PATH_TO_GOGS_WEBHOOK_DIR}/gogs-webhook -a ":3001"
WorkingDirectory={ABSOLUTE_PATH_TO_GOGS_WEBHOOK_DIR}
[Install]
WantedBy=multi-user.target
```
`{ABSOLUTE_PATH_TO_GOGS_WEBHOOK_DIR}`为Clone的目录绝对路径
然后执行:
```shell
systemctl --user enable --now gogs-webhook-deploy.service
```
如果需要在系统启动但用户未登录时就自动启动,请执行:
```shell
sudo loginctl enable-linger {USER}
```
`{USER}`为你在系统中的用户名
### 2 配置
#### 2.1 Gogs Git 钩子
在Gogs中创建一个Git项目并为其设置Webhook。
进入你的项目`https://git.lolli.tech/{USER}/{PROJECT}/settings/hooks/gogs/new`,填写相关信息。
#### 2.2 项目
在需要自动部署的项目里添加自动部署脚本(默认是`deploy.py`[示例](https://git.lolli.tech/lollipopkit/gogs-webhook/src/master/example/deploy.py))。
#### 2.3 Gogs-Webhook-Deploy
在clone本项目的目录内添加`config.json`
```json
[
{
// 名称git用户名/项目名(需要实现自动部署的项目),不可为空
"repo": "lollipopkit/example",
// 项目的父文件夹路径,默认“~”,可以是相对路径
"path": "/home/lolli/pro",
// 脚本文件名默认“deploy.py”
"script": "deploy.py",
// 信号commit messgae里包含信号则执行脚本默认“{D}”
"signal": "{D}"
}
]
```
更改本配置后,不需要重启服务,会自动加载最新配置。
### 3 尝试自动部署
⚠️ **注意**,请仔细阅读下一节[要求](https://git.lolli.tech/lollipopkit/gogs-webhook#-%E8%A6%81%E6%B1%82),然后尝试自动部署。
在需要部署的项目内新建commitcommit message内需要包含刚在`config.json`内配置的`signal`(默认`{D}`然后push。
如果未能成功请查看clone的本项目内的`.log`文件夹内的日志。
## 🔖 要求
- 在仓库的webhook页面正确配置
- 部署机器上项目文件夹名与Git项目名一致
- 部署过程需要有相应权限不可sudo除非以root权限运行本项目
- 部署时需要在commit message中包含`{D}`字符(可以通过`config.json`参数自定义字符)
- 如果要部署非默认分支需要手动切换到对应的git分支
## 🔍 CLI
```shell
Usage of gogs-webhook:
-a string
Address to listen on (default ":3001")
```
## 📝 License
`LGPL LollipopKit 2022`