更新示例{D}

This commit is contained in:
LollipopKit 2022-06-02 13:04:48 +08:00
parent 4f92b82b14
commit c7cb567978
7 changed files with 47 additions and 29 deletions

6
.gitignore vendored
View File

@ -1,4 +1,4 @@
config.json /config.json
gogs-webhook /gogs-webhook
deploy.py /deploy.py
.log .log

View File

@ -5,7 +5,7 @@
- 在仓库的webhook页面正确配置 - 在仓库的webhook页面正确配置
- 部署机器上项目文件夹名与Git项目名一致 - 部署机器上项目文件夹名与Git项目名一致
- 部署过程需要有相应权限不可sudo - 部署过程需要有相应权限不可sudo
- 部署时需要在commit message中包含`[DEPLOY]`字符标记(标记可以通过`-s`参数自定义 - 部署时需要在commit message中包含`{D}`字符(可以通过`config.json`参数自定义字符
- 可能需要手动切换到对应的git分支 - 可能需要手动切换到对应的git分支
## 配置 ## 配置
@ -13,10 +13,14 @@
```json ```json
[ [
{ {
// repo名称git用户名/项目名,不可为空
"repo": "lollipopkit/example", "repo": "lollipopkit/example",
// 项目的父文件夹路径,默认“~/pro”
"path": "/home/lolli/pro", "path": "/home/lolli/pro",
// 脚本文件名默认“deploy.py”
"script": "deploy.py", "script": "deploy.py",
"signal": "[DEPLOY]" // 信号commit messgae里包含信号则执行脚本默认“{D}”
"signal": "{D}"
} }
] ]
``` ```
@ -26,12 +30,4 @@
Usage of gogs-webhook: Usage of gogs-webhook:
-a string -a string
Address to listen on (default ":3001") Address to listen on (default ":3001")
-d string
Name of deploy script (default "deploy.py")
-i string
Interpreter to use for deploy script (default "python3")
-p string
Path to projects (default "~/pro/")
-s string
Signal for deploy (default "[DEPLOY]")
``` ```

6
example/config.json Normal file
View File

@ -0,0 +1,6 @@
[
{
"repo": "lollipopkit/gogs-webhook",
"signal": "[D]"
}
]

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python3
import os import os
APP_NAME = 'free_novel_web_go' APP_NAME = 'gogs-webhook'
SYSTEMD_NAME = 'freenovel.service' SYSTEMD_NAME = 'gogs-wh-deploy'
def pull(): def pull():
@ -9,7 +10,7 @@ def pull():
code = os.system('git fetch --all') code = os.system('git fetch --all')
if code != 0: if code != 0:
return False return False
code = os.system('git reset --hard origin/api') code = os.system('git reset --hard')
if code != 0: if code != 0:
return False return False
code = os.system('git pull') code = os.system('git pull')
@ -30,6 +31,7 @@ def kill():
pid = splited[0] pid = splited[0]
if not pid: if not pid:
pid = splited[1] pid = splited[1]
print(f'killing pid {pid} ...') print(f'killing pid {pid} ...')
exitcode = os.system('kill ' + pid) exitcode = os.system('kill ' + pid)
return exitcode == 0 return exitcode == 0
@ -37,7 +39,7 @@ def kill():
def build(): def build():
print('[building...]') print('[building...]')
code = os.system('go build') code = os.system('/usr/local/go/bin/go build')
return code == 0 return code == 0

View File

@ -4,7 +4,6 @@ After=network.target
[Service] [Service]
Type=simple Type=simple
User=lolli
Restart=on-failure Restart=on-failure
RestartSec=5s RestartSec=5s
ExecStart=/home/lolli/pro/xxx/xxx ExecStart=/home/lolli/pro/xxx/xxx

29
main.go
View File

@ -23,7 +23,7 @@ const (
var ( var (
configs = []Config{} configs = []Config{}
configsLock = &sync.RWMutex{} configsLock = &sync.RWMutex{}
logFile *os.File logFile *os.File
) )
func init() { func init() {
@ -82,15 +82,26 @@ func main() {
log.Printf("\nReceived: [Name] %s [Sender] %s", payload.Repository.FullName, payload.Sender.FullName) log.Printf("\nReceived: [Name] %s [Sender] %s", payload.Repository.FullName, payload.Sender.FullName)
// 寻找配置 // 寻找包含Signal的commit
for _, commit := range payload.Commits { for _, commit := range payload.Commits {
c := Config{} // 寻找配置
c := Config{
Path: "~/pro",
Script: "deploy.py",
Signal: "{D}",
}
for _, config := range configs { for _, config := range configs {
if config.Repo == payload.Repository.FullName { if config.Repo == payload.Repository.FullName {
c.Path = config.Path c.Repo = config.Repo
c.Script = config.Script if config.Path != "" {
c.Signal = config.Signal c.Path = config.Path
}
if config.Script != "" {
c.Script = config.Script
}
if config.Signal != "" {
c.Signal = config.Signal
}
break break
} }
} }
@ -100,6 +111,10 @@ func main() {
log.Printf("Commit: [SHA] %s [Message] %s", commit.ID, commit.Message) log.Printf("Commit: [SHA] %s [Message] %s", commit.ID, commit.Message)
log.Println("Ready to deploy") log.Println("Ready to deploy")
if c.Repo == "" {
log.Println("Using default config")
}
projectPath := fmt.Sprintf(c.Path+"/%s/", payload.Repository.Name) projectPath := fmt.Sprintf(c.Path+"/%s/", 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)

View File

@ -2,13 +2,13 @@ package main
type Config struct { type Config struct {
// repo名称git用户名/项目名 // repo名称git用户名/项目名
Repo string `json:"repo" default:""` Repo string `json:"repo"`
// 项目的父文件夹路径 // 项目的父文件夹路径
Path string `json:"path" default:"~/pro"` Path string `json:"path"`
// 脚本文件名 // 脚本文件名
Script string `json:"script" default:"deploy.py"` Script string `json:"script"`
// 信号commit messgae里包含信号则执行脚本 // 信号commit messgae里包含信号则执行脚本
Signal string `json:"signal" default:"[DEPLOY]"` Signal string `json:"signal"`
} }
type GogsPayload struct { type GogsPayload struct {