1、概要流程
2、准備工作
gogs服務器
linux網站服務器(寶塔)
本地客戶端
3、編寫鈎子訪問的接口
在public下新建githook.php文件,代碼如下:
<?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、測試接口
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改回去
5、配置到gogs鈎子去
點擊項目git主頁--》倉庫設置,--》在左側找到管理Web鈎子,--》添加鈎子--》輸入githook.php地址,其他默認即可,點擊添加,有多個服務器就配置多個鈎子地址,客戶端一旦推送,會同時觸發