gitee webhook自动部署php脚本

  • Post author:
  • Post category:php



<?php /** * 使用gitee webhook 自动部署网站 */ //WebHook密钥信息 $webHookSecret = ‘123456’; //以流的方式读取传输过来的json $body = file_get_contents(“php://input”); if (empty($body)) { die(‘无输入’); } //json转换为array $data = json_decode($body, true); //签名校验 (文档详见: https://gitee.com/help/articles/4290) $secretJoin = $data[‘timestamp’] . “\n” . $webHookSecret; $mySign = base64_encode(hash_hmac(‘sha256’, $secretJoin, $webHookSecret, true)); if ($mySign !== $data[‘sign’]) { die(‘签名错误’); } //获取推送分支 //若是主分支且提交数大于0 if (isset($data[‘ref’]) && $data[‘total_commits_count’] > 0){ switch ($data[‘ref’]) { case ‘refs/heads/master’: //网站根目录绝对路径(以/结尾) $dir = ‘/www/wwwroot/damei0000/’; $branch = ‘master’; break; case ‘refs/heads/dev’: $dir = ‘/www/wwwroot/damei0001’; $branch = ‘dev’; break; default: die(‘无需更新’); } //执行更新 $output = shell_exec(‘cd ‘ . $dir . ‘; git fetch –all && git reset –hard origin/’.$branch.’ && git config pull.rebase false && git pull 2>&1;’); }else{ $output = ‘无需更新’; } //输出执行结果 die($branch.’分支执行结果:’ . $output);



版权声明:本文为qq_32421489原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。