支持配置

This commit is contained in:
LollipopKit 2022-06-01 16:43:13 +08:00
parent a78cd5bc3a
commit 0e30fe4fa1
2 changed files with 29 additions and 2 deletions

20
README.md Normal file
View File

@ -0,0 +1,20 @@
## Gogs Webhook Deploy
通过Gogs内置的Webhook实现自动部署项目。
## 要求
- 在仓库的webhook页面正确配置
- 部署机器上项目文件夹名与Git项目名一致
- 部署过程需要有相应权限不可sudo
## 运行
```sh
Usage of gogs-webhook:
-a string
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/")
```

11
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
@ -15,6 +16,12 @@ const (
)
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")
flag.Parse()
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
@ -38,7 +45,7 @@ func main() {
log.Printf("Commit: [SHA] %s [Message] %s\n", commit.ID, commit.Message)
log.Println("Ready to deploy")
cmd := exec.Command("python3", fmt.Sprintf("~/pro/%s/deploy.py", payload.Repository.Name))
cmd := exec.Command(*deployInterpreter, fmt.Sprintf(*projectsPath + "%s/" + *deployFileName, payload.Repository.Name))
err = cmd.Run()
if err != nil {
log.Fatalf("Error running deploy script: %v\n", err)
@ -55,5 +62,5 @@ func main() {
log.Println("Webhook service started.")
http.ListenAndServe(":3001", nil)
http.ListenAndServe(*addr, nil)
}