支持不同配置
This commit is contained in:
parent
d47e03e6e5
commit
33e0d3c280
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
config.json
|
||||
gogs-webhook
|
||||
deploy.py
|
13
README.md
13
README.md
@ -8,6 +8,19 @@
|
||||
- 部署时,需要在commit message中包含`[DEPLOY]`字符标记(标记可以通过`-s`参数自定义)
|
||||
- 可能需要手动切换到对应的git分支
|
||||
|
||||
## 配置
|
||||
`config.json`
|
||||
```json
|
||||
[
|
||||
{
|
||||
"repo": "lollipopkit/example",
|
||||
"path": "/home/lolli/pro",
|
||||
"script": "deploy.py",
|
||||
"signal": "[DEPLOY]"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
## 运行
|
||||
```sh
|
||||
Usage of gogs-webhook:
|
||||
|
51
main.go
51
main.go
@ -9,18 +9,40 @@ import (
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
path = "/"
|
||||
)
|
||||
|
||||
var (
|
||||
configs = []Config{}
|
||||
configsLock = &sync.RWMutex{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
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() {
|
||||
projectsPath := flag.String("p", "~/pro/", "Path to projects")
|
||||
addr := flag.String("a", ":3001", "Address to listen on")
|
||||
deployFileName := flag.String("d", "deploy.py", "Name of deploy script")
|
||||
deployInterpreter := flag.String("i", "python3", "Interpreter to use for deploy script")
|
||||
deploySignal := flag.String("s", "[DEPLOY]", "Signal for deploy")
|
||||
flag.Parse()
|
||||
|
||||
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
@ -42,11 +64,26 @@ func main() {
|
||||
log.Printf("Received: [Name] %s [Sender] %s", payload.Repository.FullName, payload.Sender.FullName)
|
||||
|
||||
for _, commit := range payload.Commits {
|
||||
if strings.Contains(commit.Message, *deploySignal) {
|
||||
var path string
|
||||
var script string
|
||||
var signal string
|
||||
|
||||
for _, config := range configs {
|
||||
if config.Repo == payload.Repository.FullName {
|
||||
path = config.Path
|
||||
script = config.Script
|
||||
signal = config.Signal
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(commit.Message, signal) {
|
||||
log.Printf("Commit: [SHA] %s [Message] %s\n", commit.ID, commit.Message)
|
||||
log.Println("Ready to deploy")
|
||||
|
||||
cmd := exec.Command(*deployInterpreter, fmt.Sprintf(*projectsPath + "%s/" + *deployFileName, payload.Repository.Name))
|
||||
|
||||
scriptPath := fmt.Sprintf(path + "/%s/" + script, payload.Repository.Name)
|
||||
log.Printf("Running script: %s\n", scriptPath)
|
||||
cmd := exec.Command(scriptPath)
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Printf("Error running deploy script: %v\n", err)
|
||||
|
11
model.go
11
model.go
@ -1,5 +1,16 @@
|
||||
package main
|
||||
|
||||
type Config struct {
|
||||
// repo名称:git用户名/项目名
|
||||
Repo string `json:"repo" default:""`
|
||||
// 项目的父文件夹路径
|
||||
Path string `json:"path" default:"~/pro"`
|
||||
// 脚本文件名
|
||||
Script string `json:"script" default:"deploy.py"`
|
||||
// 信号:commit messgae里包含信号,则执行脚本
|
||||
Signal string `json:"signal" default:"[DEPLOY]"`
|
||||
}
|
||||
|
||||
type GogsPayload struct {
|
||||
Ref string `json:"ref"`
|
||||
Before string `json:"before"`
|
||||
|
Loading…
x
Reference in New Issue
Block a user