码云(Gitee)的WebHooks功能,能够在咱们每次提交代码后,向咱们设定的地址post一个更新的json信息,这样咱们就能够根据该信息,来自动拉去咱们的代码,实现自动同步功能.php
一、nginx用户设置成wwwnginx
二、php用户设置成wwwgit
三、代码目录设置成www chown -R www:www 代码目录web
四、修改代码目录下.git文件夹下的config文件里的url地址为 url = https://gitee账号:gitee密码@gitee.com/chishenme/gitee仓库名.gitjson
<?php
$requestBody = file_get_contents("php://input");
if (empty($requestBody)) {
die('send fail');
}
$content = json_decode($requestBody, true);
if ($content['ref'] == 'refs/heads/master' && $content['total_commits_count'] > 0 && $content['password'] == 'gitee上设置的webhook密码') {
exec("cd 代码目录 && git pull origin master 2<&1", $output, $return);
$res_log = PHP_EOL . '----------------------------------------------------------------------------------------------------' . PHP_EOL;
$res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:';
$res_log .= PHP_EOL . "pull start --------" . PHP_EOL;
$res_log .= '$output:' . var_export($output, true) . PHP_EOL . '$return:' . var_export($return, true) . PHP_EOL;
$res_log .= PHP_EOL . "pull end --------" . PHP_EOL;
//$res_log .= var_export($requestBody, true);
file_put_contents(git_webhook.log日志地址, $res_log, FILE_APPEND);//写入日志到log文件中
}