Gitee码云通过WebHooks实现自动同步代码部署

码云(Gitee)的WebHooks功能,可以在我们每次提交代码后,向我们设定的地址post一个更新的json信息,这样我们就可以根据该信息,来自动拉去我们的代码,实现自动同步功能.

第一步 配置WebHooks

第二步 服务器软件配置

1、nginx用户设置成www

2、php用户设置成www

3、代码目录设置成www   chown -R www:www 代码目录

4、修改代码目录下.git文件夹下的config文件里的url地址为   url = https://gitee帐号:gitee密码@gitee.com/chishenme/gitee仓库名.git

第三步 服务器脚本配置,在代码目录里写入更新代码的脚本php

<?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文件中
}
原文地址:https://www.cnblogs.com/yuewangshanren/p/9681078.html