配置-Path支持相对路径
This commit is contained in:
parent
c5a5e45c97
commit
0a17b124e5
39
README.md
39
README.md
@ -12,8 +12,39 @@ git clone https://git.lolli.tech/lollipopkit/gogs-webhook
|
|||||||
go build
|
go build
|
||||||
```
|
```
|
||||||
#### 1.2 开机自启
|
#### 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`,内容如下:
|
在`~/.config/systemd/user/`目录下创建一个文件,命名为`gogs-webhook-deploy.service`,内容如下:
|
||||||
```s
|
```shell
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Gogs Webhook Deploy Service
|
Description=Gogs Webhook Deploy Service
|
||||||
After=network.target
|
After=network.target
|
||||||
@ -41,10 +72,10 @@ sudo loginctl enable-linger {USER}
|
|||||||
`{USER}`为你在系统中的用户名
|
`{USER}`为你在系统中的用户名
|
||||||
|
|
||||||
### 2 配置
|
### 2 配置
|
||||||
#### 2.1 Git
|
#### 2.1 Gogs Git 钩子
|
||||||
在Gogs中创建一个Git项目,并为其设置Webhook。
|
在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 项目
|
#### 2.2 项目
|
||||||
在需要自动部署的项目里添加自动部署脚本(默认是`deploy.py`,[示例](https://git.lolli.tech/lollipopkit/gogs-webhook/src/master/example/deploy.py))。
|
在需要自动部署的项目里添加自动部署脚本(默认是`deploy.py`,[示例](https://git.lolli.tech/lollipopkit/gogs-webhook/src/master/example/deploy.py))。
|
||||||
#### 2.3 Gogs-Webhook-Deploy
|
#### 2.3 Gogs-Webhook-Deploy
|
||||||
@ -54,7 +85,7 @@ sudo loginctl enable-linger {USER}
|
|||||||
{
|
{
|
||||||
// 名称:git用户名/项目名(需要实现自动部署的项目),不可为空
|
// 名称:git用户名/项目名(需要实现自动部署的项目),不可为空
|
||||||
"repo": "lollipopkit/example",
|
"repo": "lollipopkit/example",
|
||||||
// 项目的父文件夹路径,默认“~”
|
// 项目的父文件夹路径,默认“~”,可以是相对路径
|
||||||
"path": "/home/lolli/pro",
|
"path": "/home/lolli/pro",
|
||||||
// 脚本文件名,默认“deploy.py”
|
// 脚本文件名,默认“deploy.py”
|
||||||
"script": "deploy.py",
|
"script": "deploy.py",
|
||||||
|
47
init.go
Normal file
47
init.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
58
main.go
58
main.go
@ -11,7 +11,6 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -23,43 +22,10 @@ const (
|
|||||||
var (
|
var (
|
||||||
configs = []Config{}
|
configs = []Config{}
|
||||||
configsLock = &sync.RWMutex{}
|
configsLock = &sync.RWMutex{}
|
||||||
|
home = os.Getenv("HOME") + "/"
|
||||||
logFile *os.File
|
logFile *os.File
|
||||||
home = os.Getenv("HOME")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
os.Mkdir(logDir, perm)
|
|
||||||
file := ".log/" + 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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addr := flag.String("a", ":3001", "Address to listen on")
|
addr := flag.String("a", ":3001", "Address to listen on")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@ -116,7 +82,27 @@ func main() {
|
|||||||
log.Println("Using default config")
|
log.Println("Using default config")
|
||||||
}
|
}
|
||||||
|
|
||||||
projectPath := fmt.Sprintf(c.Path+"/%s/", payload.Repository.Name)
|
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 + c.Script
|
scriptPath := projectPath + c.Script
|
||||||
log.Printf("Running script: %s\n", scriptPath)
|
log.Printf("Running script: %s\n", scriptPath)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user