天天看點

記錄一下在碼雲上布置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 隐藏檔案

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

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

坑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