我们记录一下 github、gitlab、gitee、coding 平台的php版接收webhook脚本
github
<?php //网站目录 $www_file = '/www/wwwroot/laoji.org/api'; //日志文件路径 $log_file = '/www/wwwroot/www.test.com/auto_hook.log'; // GitHub项目 Settings/Webhooks 中的 Secret $client_secret = "laoji.org"; //接收数据 $vaild_data = json_decode(file_get_contents('php://input'), true); //传递的签名 $vaild_signature = $_SERVER['HTTP_X_HUB_SIGNATURE']; $hash = "sha1=".hash_hmac('sha1', $vaild_data, $client_secret ); if (strcmp($vaild_signature , $hash) == 0) { // sign sucess //执行shell命令并把返回信息写进日志 shell_exec("cd {$www_file} && git pull origin dev > {$log_file} 2>&1 "); }
gitlab
<?php //网站目录 $www_file = '/www/wwwroot/laoji.org/api'; //日志文件路径 $log_file = '/www/wwwroot/www.test.com/auto_hook.log'; //作为接口传输的时候认证的密钥 $client_token = 'laoji.org'; //传递的token $vaild_token = $_SERVER['HTTP_X_GITLAB_TOKEN']; //传递数据 $vaild_data = json_decode(file_get_contents("php://input"), true); /* test token */ if ($vaild_token !== $client_token ) { echo "error 403"; exit(); } //若是dev分支且提交数大于0 if ($vaild_data['ref']=='refs/heads/dev' && $vaild_data['total_commits_count']>0) { //执行shell命令并把返回信息写进日志 shell_exec("cd ".$www_file." && git pull origin dev > ".$log_file." 2>&1 "); }
gitee
<?php //网站目录 $www_file = '/www/wwwroot/laoji.org/api'; //日志文件路径 $log_file = '/www/wwwroot/www.test.com/auto_hook.log'; // 码云WebHooks中配置的密码 $client_password = "laoji.org"; $vaild_data = json_decode(file_get_contents('php://input'), true); // 判断密码 if ($vaild_data ['password'] === $client_password ) { //执行shell命令并把返回信息写进日志 shell_exec("cd ".$www_file." && git pull origin dev > ".$log_file." 2>&1 "); }
coding
<?php //网站目录 $www_file = '/www/wwwroot/laoji.org/api'; //日志文件路径 $log_file = '/www/wwwroot/www.test.com/auto_hook.log'; // 码云WebHooks中配置的密码 $client_token = "laoji.org"; // 接收Coding post传递的参数 $vaild_data = file_get_contents('php://input'); // 从请求头中获取签名 $vaild_signature = $_SERVER['HTTP_X_CODING_SIGNATURE']; // 进行签名解析 $sha1 = hash_hmac("sha1",$vaild_data,$client_token); $result_signature = 'sha1='. $sha1; // 进行身份验证 if ($result_signature === $vaild_signature ) { //执行shell命令并把返回信息写进日志 shell_exec("cd ".$www_file." && git pull origin dev > ".$log_file." 2>&1 "); }