120 lines
3.6 KiB
Markdown
120 lines
3.6 KiB
Markdown
## 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),然后尝试自动部署。
|
||
在需要部署的项目内,新建commit,commit 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` |