首先你的有个 Coding 账号
然后在后台配置 webhook 的 https 接口即可,因为本人所在公司无权限更改 https 接口,只能提供一个接口地址给管理员配置完可以访问即可,
下面放代码,直接是接口的代码,我的代码是 thinkphp3.2.3, 回调接口是 /api/hook/autoEvent;
参考的是 github 的开源项目 githook,地址:https://github.com/andanyoung/githook
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | <?php /** * Created By M * 2022/07/21 15:06 */ namespace Api\Controller; use Think\Controller; use Think\Log; class HookController extends Controller { /** * "HTTP_AUTHORIZATION":"Basic aGFvZmFugerhhddzQ1Ng==", * "HTTP_X_CODING_SERVICE_HOOK_ACTION":"webhook_http_post", * "HTTP_X_CODING_SERVICE_HOOK_EVENT":"GIT_PUSHED", * "HTTP_X_CODING_SERVICE_HOOK_ID":"d334terwtertrewt2c-a1try29-69eyyrtycb75d4", * "HTTP_X_CODING_DELIVERY":"cb7ba79f-c592-4ba4-twerrwea1c78c0", */ public function autoEvent() { $time = date('Y-m-d H:i:s', time()); Log::write("================请求开始(".$time.")================="); if(IS_POST){ $HTTP_X_CODING_SERVICE_HOOK_EVENT = $_SERVER['HTTP_X_CODING_SERVICE_HOOK_EVENT']; $HTTP_X_CODING_SERVICE_HOOK_ACTION = $_SERVER['HTTP_X_CODING_SERVICE_HOOK_ACTION']; $HTTP_X_CODING_SERVICE_HOOK_ID = $_SERVER['HTTP_X_CODING_SERVICE_HOOK_ID']; $HTTP_X_CODING_DELIVERY = $_SERVER['HTTP_X_CODING_DELIVERY']; $HTTP_AUTHORIZATION = $_SERVER['HTTP_AUTHORIZATION']; $basic = "Basic ".base64_encode("xxxxxxx:xxxxxxxxx"); if($HTTP_AUTHORIZATION == $basic && $HTTP_X_CODING_SERVICE_HOOK_EVENT && $HTTP_X_CODING_SERVICE_HOOK_EVENT == "GIT_PUSHED" && $HTTP_X_CODING_SERVICE_HOOK_ACTION && $HTTP_X_CODING_SERVICE_HOOK_ACTION == "webhook_http_post"){ $info = file_get_contents('php://input', 'r'); if($info == ""){ Log::write("================解析异常data为空==================="); }else{ $data = json_decode($info, true); $commits = json_decode(json_encode($data['commits']),true); Log::write("===commit解析数据=>(".json_encode($commits[0]).")===="); $added = $commits[0]['added']; $removed = $commits[0]['removed']; $modified = $commits[0]['modified']; Log::write("====add=>(".json_encode($added).")========"); Log::write("====remove=>(".json_encode($removed).")======"); Log::write("====modified=>(".json_encode($modified).")======="); $added_str = $removed_str = $modified_str = ""; if(count($added) > 0){ foreach($added as $k => $v){ $str1 = explode('WWW/', $v); $added_str .= "/bin/cp -f /www/gitcode/xiangmu/".$v." /www/wwwroot/111.111.111.111/".$str1[1]."\r\n"; } } if(count($removed) > 0){ foreach($removed as $k => $v){ $removed_str .= "/bin/rm -rf /www/gitcode/xiangmu/".$v."\r\n"; } } if(count($modified) > 0){ foreach($modified as $k => $v){ $str3 = explode('WWW/', $v); $modified_str .= "/bin/cp -f /www/gitcode/xiangmu/".$v." /www/wwwroot/111.111.111.111/".$str3[1]."\r\n"; } } $final_str = "#!/bin/bash\r\n" . $added_str . $removed_str . $modified_str; $file_name = date('Y-m-d').'-'.$commits[0]['id'].'.sh'; Log::write("=====>(".$final_str.")=========="); $shell_dir = APP_ROOT . "Upload/shell/" . date('Y-m-d'); if(!is_dir($shell_dir)){ mkdir($shell_dir, 0777, true); } file_put_contents($shell_dir .'/'. $file_name, $final_str); } }else{ Log::write("===解析异常HTTP_X_CODING_SERVICE_HOOK_ID==>(".$HTTP_X_CODING_SERVICE_HOOK_ID.")==="); Log::write("===解析异常HTTP_X_CODING_DELIVERY==>(".$HTTP_X_CODING_DELIVERY.")==="); } Log::write("================请求结束(".$time.")================="); }else{ Log::write("================非法请求(".$time.")================="); Log::write("================请求结束(".$time.")================="); } } } |