基本介绍:
使用webhook部署时git的一些设置
环境介绍:
阿里云上部署的Lnmp
git使用的是gitee
部署流程
服务器
git clone 好项目
创建gitautoload.php
123456789101112131415161718192021222324<?phpheader('Content-Type: text/html; charset=UTF-8');$token = 'token';$patharr=array('keyword' => "{ProjectPath}");$path = $patharr[$_GET['key']];$requestBody = file_get_contents("php://input");if (empty($requestBody)) {die('send fail');}$content = json_decode($requestBody, true);$res_log = '-------------------------'.PHP_EOL;if ($content['password'] != $token) {$res_log .= 'Waring:'. date('Y-m-d H:i:s'). ' URL:'.$_SERVER['REMOTE_ADDR'].":".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].":".$_SERVER["HTTP_REFERER"].' REQUEST:'.$requestBody.PHP_EOL;} else {if ($content['ref']=='refs/heads/master' && $content['total_commits_count']>0) {$res = shell_exec("cd {$path} && git pull 2>&1");//以www用户运行$res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:' . PHP_EOL;$res_log .= $res.PHP_EOL;}}$result = iconv("utf-8", "gb2312", $res_log);file_put_contents("git-webhook.txt", $result, FILE_APPEND);//追加写入进入项目设置git
12git remote set-url origin https://<username>:<password>@gitee.com/<username>/<repo_name>.gitgit branch --set-upstream-to=origin/<branch> master
gitee设置
- 访问git项目 https://gitee.com/{username}/{repo_name>}
- 进入 管理 -> WebHooks
Url填写gitautoload.php的访问路径,例如:
1http://yourdomain/gitautoload.php?key={keyword}填写token
- 选择push并提交
选择测试后可访问 http://yourdomain/git-webhook.txt 查看结果
错误处理
请保证git账户是否正确
git是否有权限(因为是通过shell_exec执行的,项目中git文件夹是否有相应的权限)