保存log,设定工作目录
This commit is contained in:
parent
33e0d3c280
commit
b955ce7f20
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
config.json
|
||||
gogs-webhook
|
||||
deploy.py
|
||||
deploy.py
|
||||
.log
|
BIN
gogs-webhook
BIN
gogs-webhook
Binary file not shown.
39
main.go
39
main.go
@ -7,6 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -14,15 +15,29 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
path = "/"
|
||||
path = "/"
|
||||
logDir = ".log/"
|
||||
perm = 0770
|
||||
)
|
||||
|
||||
var (
|
||||
configs = []Config{}
|
||||
configs = []Config{}
|
||||
configsLock = &sync.RWMutex{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
go func() {
|
||||
for {
|
||||
os.Mkdir(logDir, perm)
|
||||
file := ".log/" + time.Now().Format("2006-01-02") + ".txt"
|
||||
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 {
|
||||
@ -46,6 +61,7 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
// 解析Payload
|
||||
reqBody, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error reading request body: %v\n", err)
|
||||
@ -63,6 +79,7 @@ func main() {
|
||||
|
||||
log.Printf("Received: [Name] %s [Sender] %s", payload.Repository.FullName, payload.Sender.FullName)
|
||||
|
||||
// 寻找配置
|
||||
for _, commit := range payload.Commits {
|
||||
var path string
|
||||
var script string
|
||||
@ -77,13 +94,20 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// 包含部署信号
|
||||
if strings.Contains(commit.Message, signal) {
|
||||
log.Printf("Commit: [SHA] %s [Message] %s\n", commit.ID, commit.Message)
|
||||
log.Println("Ready to deploy")
|
||||
|
||||
scriptPath := fmt.Sprintf(path + "/%s/" + script, payload.Repository.Name)
|
||||
projectPath := fmt.Sprintf(path+"/%s/", payload.Repository.Name)
|
||||
scriptPath := projectPath + script
|
||||
log.Printf("Running script: %s\n", scriptPath)
|
||||
|
||||
// 开始执行脚本
|
||||
cmd := exec.Command(scriptPath)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = projectPath
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Printf("Error running deploy script: %v\n", err)
|
||||
@ -99,6 +123,9 @@ func main() {
|
||||
})
|
||||
|
||||
log.Println("Webhook service started at " + *addr)
|
||||
|
||||
http.ListenAndServe(*addr, nil)
|
||||
}
|
||||
|
||||
err := http.ListenAndServe(*addr, nil)
|
||||
if err != nil {
|
||||
log.Println("Error starting webhook service: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user