From 0a17b124e5baf3977829114c1127135d1ca86c0c Mon Sep 17 00:00:00 2001 From: Junyuan Feng Date: Sun, 26 Jun 2022 10:40:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE-Path=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 39 +++++++++++++++++++++++++++++++++---- init.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 58 +++++++++++++++++++++---------------------------------- 3 files changed, 104 insertions(+), 40 deletions(-) create mode 100644 init.go diff --git a/README.md b/README.md index 8bbca13..a7a984f 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,39 @@ git clone https://git.lolli.tech/lollipopkit/gogs-webhook go build ``` #### 1.2 开机自启 +##### 1.2.1 适用于root用户的操作 +
+点击查看 + +在`/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 +``` +
+ +##### 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", diff --git a/init.go b/init.go new file mode 100644 index 0000000..3fbfdc7 --- /dev/null +++ b/init.go @@ -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) + } +} diff --git a/main.go b/main.go index e79d74b..a706f57 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "os/exec" "strings" "sync" - "time" ) const ( @@ -23,43 +22,10 @@ const ( var ( configs = []Config{} configsLock = &sync.RWMutex{} + home = os.Getenv("HOME") + "/" 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() { addr := flag.String("a", ":3001", "Address to listen on") flag.Parse() @@ -116,7 +82,27 @@ func main() { 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 log.Printf("Running script: %s\n", scriptPath)