merge: master

This commit is contained in:
zhaoyingbo 2022-07-09 18:43:52 +08:00
commit 353367c9b5
3 changed files with 104 additions and 6 deletions

View File

@ -12,8 +12,39 @@ 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`,内容如下:
```s
```shell
[Unit]
Description=Gogs Webhook Deploy Service
After=network.target
@ -41,10 +72,10 @@ sudo loginctl enable-linger {USER}
`{USER}`为你在系统中的用户名
### 2 配置
#### 2.1 Git
#### 2.1 Gogs Git 钩子
在Gogs中创建一个Git项目并为其设置Webhook。
进入你的项目`https://git.lolli.tech/USER/PROJECT/settings/hooks/gogs/new`,填写相关信息。
进入你的项目`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
@ -54,7 +85,7 @@ sudo loginctl enable-linger {USER}
{
// 名称git用户名/项目名(需要实现自动部署的项目),不可为空
"repo": "lollipopkit/example",
// 项目的父文件夹路径,默认“~”
// 项目的父文件夹路径,默认“~”,可以是相对路径
"path": "/home/lolli/pro",
// 脚本文件名默认“deploy.py”
"script": "deploy.py",

47
init.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"time"
)
func init() {
go setLogDestination()
go readConfigs()
}
func setLogDestination() {
for {
os.Mkdir(logDir, perm)
file := logDir + time.Now().Format("2006-01-02") + ".txt"
var err error
logFile, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, perm)
if err != nil {
panic(err)
}
log.SetOutput(logFile)
time.Sleep(time.Minute)
}
}
func readConfigs() {
for {
data, err := ioutil.ReadFile("config.json")
if err != nil {
return
}
configsLock.Lock()
if err := json.Unmarshal(data, &configs); err != nil {
log.Printf("Error unmarshalling configs: %v\n", err)
}
configsLock.Unlock()
time.Sleep(time.Second * 10)
}
}

24
main.go
View File

@ -24,8 +24,8 @@ const (
var (
configs = []Config{}
configsLock = &sync.RWMutex{}
home = os.Getenv("HOME") + "/"
logFile *os.File
home = os.Getenv("HOME")
)
func init() {
@ -221,7 +221,27 @@ func main() {
}
// 准备部署,生成项目地址和脚本地址
projectPath := fmt.Sprintf(deployConfig.Path+"/%s/", payload.Repository.Name)
projectPath := projectPath := func() string {
hasPrefix := strings.HasPrefix(c.Path, "/")
hasSuffix := strings.HasSuffix(c.Path, "/")
base := ""
// 如果路径以/开头,则认为是绝对路径
if hasPrefix {
base = c.Path
} else {
base = home + c.Path
}
repoFmt := ""
if hasSuffix {
repoFmt = "%s/"
} else {
repoFmt = "/%s/"
}
return fmt.Sprintf(base+repoFmt, payload.Repository.Name)
}()
scriptPath := projectPath + deployConfig.Script
// 处理项目clone并切换到指定的分支和commit