增加触发相关脚本逻辑

This commit is contained in:
白茶清欢 2021-11-12 17:35:03 +08:00
parent 4b517be8a8
commit c51d6175d9
2 changed files with 46 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import "git.zhangdeman.cn/zhangdeman/gopkg/cmd"
//
// Date : 4:59 下午 2021/11/12
type Task struct {
Flag string `json:"flag"`
WorkDir string `json:"work_dir"`
Command string `json:"command"`
Script string `json:"script"`

View File

@ -10,6 +10,8 @@ package main
import (
"encoding/json"
"git.zhangdeman.cn/zhangdeman/gopkg/cmd"
"git.zhangdeman.cn/zhangdeman/git-hook/define"
"git.zhangdeman.cn/zhangdeman/gopkg/git_hook"
"git.zhangdeman.cn/zhangdeman/gopkg/util"
@ -39,6 +41,17 @@ func main() {
router := gin.Default()
// 注册回调路由
_ = 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" {
// 不是master分支, 不触发相应逻辑
return &git_hook.ResponseData{
@ -47,10 +60,32 @@ func main() {
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{
Code: 0,
Message: "成功接收到web hook通知",
Data: hookData,
Data: gin.H{
"command": taskRunResult.ExecuteCommand,
"output": string(taskRunResult.Output),
},
}
})
_ = router.Run(":12590")
@ -60,3 +95,12 @@ func parseCLIParam() {
paramList := []string{"config_path"}
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
}