这里写自定义目录标题
webhooks实现自动拉取文件
-
在自己的项目上创建一个可以访问的api(坑:不允许Get请求);
api代码// 部署根目录 $target = 'xxx'; // 部署密钥 $token = 'xxx'; //(此处新建webhooks的时候自定义的); // 部署分支 $branch = 'master';//(需要拉去的分支) //判断token是否正确 $XCodeupToken = request()->header('X-Codeup-Token'); $request_token = isset($XCodeupToken) ? $XCodeupToken : ''; if ($token !== $request_token) { info('token不正确');die; } // 分支判断 $arr = file_get_contents('php://input', 'r'); $arr = json_decode($arr, true); if (JSON_ERROR_NONE !== json_last_error()) { info('JSON_ERROR_NONE');die; } if(isset($arr['ref'])){ $ref = $arr['ref']; }else{ info('ref不存在'); die; } $ref = explode('/', $ref); $ref = array_pop($ref); if ($branch != $ref) { info('branch不等ref');die; } //此处2>&1 失败会返回错误信息 try{ $cmd = "cd $target && git pull 2>&1"; exec($cmd,$out,$status); info('cmd:'.json_encode($out)); info('cmd:'.$status); }catch (\Exception $e){ info('错误信息:'.$e->getMessage()); } $res_log = "---------------------------------------------------------------".PHP_EOL; $res_log .= PHP_EOL."pull start ---------------------------------------------".PHP_EOL; $res_log .= '------------------------------------------------------------'.PHP_EOL; $res_log .= '-----------当前时区:' . date_default_timezone_get(); $res_log .= $arr['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $arr['repository']['name'] . '项目的' . $arr['ref'] . '分支push了' . $arr['total_commits_count'] . '个commit:'; $res_log .= '------------------------------------------------------------'.PHP_EOL; $res_log .= "pull end -----------------------------------------------------".PHP_EOL; info($res_log);
2.在码云/git/阿里云效或者其他代码管理处新建webhooks,填写api,自定义token和需要触发的事件
3.保存完之后查看历史(可以看到头部信息以及等其他参数)
4.点击测试webhooks(阿里云效code 为例子 )
5. 2>&1(命令结尾一定要加上这个,要不然拉取失败你找不到原因)
6. exec( c m d , cmd, cmd,out, s t a t u s ) ; status); status);cmd:命令 $out:命令标准输出内容 $status:状态成功1:失败0
7. 招到报错信息就好办了
8. 如果拉取失败报错error: cannot open .git/FETCH_HEAD: Permission denied
9. 请进入根目录输入ll -lh .git/FETCH_HEAD 查看当前git的用户和用户组
10. .git/FETCH_HEAD的用户和用户组必须要和根目录的一致,否则没权限拉取
11. sudo chown -R xxx:xxx 根目录
12. sudo chown xxx:xxx .git (此时已解决权限问题)
13. 下一个错误是( fatal: could not read Username for ‘https://codeups.aliyuna.com’: No such device or address)
14. 进入项目目录输入:vi .git/config编辑
15:url格式一点要按照上图所述(https://用户名:密码@codeup.aliyun.com/xxxxxxxxxxx/mus.git)
16:此时再回去测试已经成功了