增加触发相关脚本逻辑
This commit is contained in:
parent
4b517be8a8
commit
c51d6175d9
@ -15,6 +15,7 @@ import "git.zhangdeman.cn/zhangdeman/gopkg/cmd"
|
|||||||
//
|
//
|
||||||
// Date : 4:59 下午 2021/11/12
|
// Date : 4:59 下午 2021/11/12
|
||||||
type Task struct {
|
type Task struct {
|
||||||
|
Flag string `json:"flag"`
|
||||||
WorkDir string `json:"work_dir"`
|
WorkDir string `json:"work_dir"`
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Script string `json:"script"`
|
Script string `json:"script"`
|
||||||
|
46
git_hook.go
46
git_hook.go
@ -10,6 +10,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/gopkg/cmd"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/git-hook/define"
|
"git.zhangdeman.cn/zhangdeman/git-hook/define"
|
||||||
"git.zhangdeman.cn/zhangdeman/gopkg/git_hook"
|
"git.zhangdeman.cn/zhangdeman/gopkg/git_hook"
|
||||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||||||
@ -39,6 +41,17 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
// 注册回调路由
|
// 注册回调路由
|
||||||
_ = git_hook.RegisterGitHookRouter(router, func(ctx *gin.Context, hookData *git_hook.HookData) *git_hook.ResponseData {
|
_ = git_hook.RegisterGitHookRouter(router, func(ctx *gin.Context, hookData *git_hook.HookData) *git_hook.ResponseData {
|
||||||
|
// 检测仓库是否建立了相关任务
|
||||||
|
taskConfig, exist := getTaskConfig(hookData.Repository.Name)
|
||||||
|
if !exist {
|
||||||
|
// 没有配置相关任务
|
||||||
|
return &git_hook.ResponseData{
|
||||||
|
Code: 0,
|
||||||
|
Message: "成功接收到web hook通知, " + hookData.Repository.Name + " 没有配置任何任务, 不做处理",
|
||||||
|
Data: hookData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if hookData.Ref != "refs/heads/master" {
|
if hookData.Ref != "refs/heads/master" {
|
||||||
// 不是master分支, 不触发相应逻辑
|
// 不是master分支, 不触发相应逻辑
|
||||||
return &git_hook.ResponseData{
|
return &git_hook.ResponseData{
|
||||||
@ -47,10 +60,32 @@ func main() {
|
|||||||
Data: hookData,
|
Data: hookData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
taskRunResult := cmd.Execute(cmd.Config{
|
||||||
|
WorkDir: taskConfig.WorkDir,
|
||||||
|
Command: taskConfig.Command,
|
||||||
|
Script: taskConfig.Script,
|
||||||
|
ParameterList: taskConfig.Parameter,
|
||||||
|
})
|
||||||
|
|
||||||
|
if nil != taskRunResult.Err {
|
||||||
|
return &git_hook.ResponseData{
|
||||||
|
Code: -1,
|
||||||
|
Message: "task任务执行失败 : " + taskRunResult.Err.Error(),
|
||||||
|
Data: gin.H{
|
||||||
|
"command": taskRunResult.ExecuteCommand,
|
||||||
|
"output": string(taskRunResult.Output),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &git_hook.ResponseData{
|
return &git_hook.ResponseData{
|
||||||
Code: 0,
|
Code: 0,
|
||||||
Message: "成功接收到web hook通知",
|
Message: "成功接收到web hook通知",
|
||||||
Data: hookData,
|
Data: gin.H{
|
||||||
|
"command": taskRunResult.ExecuteCommand,
|
||||||
|
"output": string(taskRunResult.Output),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
_ = router.Run(":12590")
|
_ = router.Run(":12590")
|
||||||
@ -60,3 +95,12 @@ func parseCLIParam() {
|
|||||||
paramList := []string{"config_path"}
|
paramList := []string{"config_path"}
|
||||||
CLIParamConfig = util.ParseCLIParameter(paramList)
|
CLIParamConfig = util.ParseCLIParameter(paramList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTaskConfig(taskFlag string) (define.Task, bool) {
|
||||||
|
for _, item := range TaskList {
|
||||||
|
if item.Flag == taskFlag {
|
||||||
|
return item, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return define.Task{}, false
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user