記錄一下在碼雲上佈置webhooks所遇到的坑

1.在服務器上搭建好項目,能git pull

2.在碼雲上創建webhooks;---要填能訪問的鏈接

3.文件代碼

<?php
//git webhook 自動部署腳本
//項目存放物理路徑,第一次clone時,必須保證該目錄爲空
$savePath = "/home/www/xxx";
$gitPath  = "https://用戶名:密碼@gitee.com/xxx/xxx.git";//代碼倉庫(用戶名和密碼不能有@)
//exec("cd {$savePath} &&su www && git pull origin master 2>&1",$return,$ss);
//var_dump($return);
//var_dump($ss);
//exit;
    $requestBody = file_get_contents("php://input");
    if (empty($requestBody)) {
        die('send fail');
    }
    $content = json_decode($requestBody, true);
    //若是主分支且提交數大於0
    if ($content['ref']=='refs/heads/master' && $content['total_commits_count']>0) {
        $res = PHP_EOL."pull start --------".PHP_EOL;
        $res .= shell_exec("cd {$savePath} && git pull origin master 2>&1");//拉去代碼
	$file=fopen("/logs/wxshangh-webhooks.log","w");
	fwrite($file,date("Y-m-d H:i:s")."\r\n");
	fwrite($file,$res."\r\n");
	fclose($file);
        $res_log = '-------------------------'.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 .= $res.PHP_EOL;
        $res_log .= "pull end --------".PHP_EOL;
        file_put_contents("git-webhook_log.txt", $res_log, FILE_APPEND);//寫入日誌到log文件中
    }

好了,現在來說遇到的坑

大坑:

git pull

解決問題的思路和思考:

PHP代碼執行不成功,手動通過xshell執行命令,看是否提示錯誤

如果沒有錯誤,查看是否用戶權限問題,PHP執行默認www用戶或別的,xshell是root用戶

寫日誌錯誤,文件夾沒有權限

shell_exec函數執行錯誤, 修改PHP配置文件,默認是禁用此類函數

坑1:git: could not read Username for 'https://github.com';

直接修改 .git/config 隱藏文件

(用戶名和密碼不能含有@)

坑2:insufficient permission for adding an object to repository database

是因爲項目的 .git 目錄有些文件夾的權限是www用戶,

解決辦法,切換到root超級管理員,修改文件所屬用戶爲當前電腦用戶

chown -R xxxx: .git

 坑3:打開FETCH_HEAD失敗

原因:這個文件權限不夠.需要權限

chmod -R 777 FETCH_HEAD

如果還有錯誤,那就將.git文件夾的權限全部轉化爲777

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章