【Webhook】各类网站的自动化部署校验脚本-PHP版本

PHP,代码片段 2019-11-23

目录只有这几个平台:github、gitlab、gitee、coding

1 github

 
//网站目录
$www_file  = '/www/wwwroot/laofantest/api';
//日志文件路径
$log_file = '/www/wwwroot/www.test.com/auto_hook.log';
// GitHub项目 Settings/Webhooks 中的 Secret
$client_secret = "laofan";

//接收数据
$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 ");  
}

2 gitlab

    
//网站目录
$www_file  = '/www/wwwroot/laofantest/api';
//日志文件路径
$log_file = '/www/wwwroot/www.test.com/auto_hook.log';
    
//作为接口传输的时候认证的密钥  
$client_token = 'laofan';  


//传递的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 ");  
}    
    

3 gitee


//网站目录
$www_file  = '/www/wwwroot/laofantest/api';
//日志文件路径
$log_file = '/www/wwwroot/www.test.com/auto_hook.log';
// 码云WebHooks中配置的密码
$client_password = "laofan";

$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 ");
}
 

4 coding



//网站目录
$www_file  = '/www/wwwroot/laofantest/api';
//日志文件路径
$log_file = '/www/wwwroot/www.test.com/auto_hook.log';
// 码云WebHooks中配置的密码
$client_token = "laofan";

// 接收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 ");
}

帅气的楼主关闭了评论