博客列表 >gogs 实现webhook钩子(php接口形式)

gogs 实现webhook钩子(php接口形式)

弘德誉曦的博客
弘德誉曦的博客原创
2021年08月06日 15:14:04864浏览

编写钩子访问的接口


在public下新建githook.php文件,代码如下:

1
2
3
4
5
<?php
$cmd = "cd  /www/wwwroot/testfc/pro/  && git reset --hard origin/master && sudo git pull origin master 2>&1";
$res = array();
exec($cmd,$res);
var_dump($res);

有多台服务器就每个服务器的网站public下都加这个,然后配置到gogs服务器去

测试接口


4.1 先把cmd命令换成 $cmd = "cd /www/wwwroot/testfc/pro/ sudo git pull origin master 2>&1"; 测试下
  var_dump($res); 打印出错误

可能会出现以下错误:

错误1:Warning: scandir() has been disabled for security reasons in…
原因:LNMP 0.9禁用了部分存在危险的PHP函数,当前是exec无法执行
找到php版本,修改配置信息,搜索 disable_functions 把它后面的 exec去掉,保存,重启php

错误2:sudo: no tty present and no askpass program specified

           或者这个错误 We trust you have received the usual lecture from the local System
在宝塔上找到etc/sudoers 编辑 ,加入 %www ALL = NOPASSWD: ALL

错误3:fatal: could not read Username for
找到项目 .git/config文件夹 /www/wwwroot/testfc/pro/.git/config
在ip前面加上用户名与密码格式如: url = http://username:psd@xxx.xxx.xxx.xxx:3000/aaa/pro.git

错误4:执行完接口每次都会出现一堆已更新的东西

打开终端,cd 到项目 根目录,执行 git pull --all    然后再执行 你脚本内的那个  git  pull origin  xxx(你要固定拉取的分支名)即可,然后再执行就会出现

 

************有看到 Already up-to-date 字眼表示测试成功,把cmd改回去

配置到gogs钩子去


点击项目git主页--》仓库设置,--》在左侧找到管理Web钩子,--》添加钩子--》输入githook.php地址,其他默认即可,点击添加,有多个服务器就配置多个钩子地址,客户端一旦推送,会同时触发

 


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议