From c51d6175d94c27d10f44b9c601e6ad55243a4f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 12 Nov 2021 17:35:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=A6=E5=8F=91=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E8=84=9A=E6=9C=AC=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/task.go | 1 + git_hook.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/define/task.go b/define/task.go index 69305f5..b705779 100644 --- a/define/task.go +++ b/define/task.go @@ -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"` diff --git a/git_hook.go b/git_hook.go index ebfbdd3..b3127af 100644 --- a/git_hook.go +++ b/git_hook.go @@ -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 +}