配置碼雲git自動更新的webhook

  • 2019 年 10 月 26 日
  • 筆記

配置項目提交到git的時候自動同步伺服器程式碼

一、在伺服器項目跟目錄新建文件hook.php 程式碼如下:

<?php    $json = file_get_contents("php://input");    $data = json_decode($json,true);    if (isset($data['ref']) && $data['total_commits_count']>0) {        $res = PHP_EOL."pull start ---------------------------------------------".PHP_EOL;        $res .= shell_exec("cd /home/www/honli && git pull https://654589023%40qq.com:[email protected]/xxxx/xxxx.git 2<&1 ");        $res_log = '------------------------------------------------------------'.PHP_EOL;        $res_log .= $data['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $data['repository']['name'] . '項目的' . $data['ref'] . '分支push了' . $data['total_commits_count'] . '個commit:'.$data['commits']['message'];        $res_log .= $res.PHP_EOL;        $res_log .= "pull end -----------------------------------------------------".PHP_EOL;        file_put_contents("/home/wwwlogs/webhook/".date('Y-m-d',time()).".txt", $res_log, FILE_APPEND);//寫入日誌到log文件中    }    ?>

二、配置碼雲webhook:

三、確認伺服器安裝了git,在web目錄下clone 項目:git clone https://gitee.com/xxxx/xxxx.git;

四、把git的目錄許可權改為web用戶許可權: chown -r www:www

五、手動更新程式碼:cd /home/www/honli && git pull;

六、git pull 衝突或錯誤時,用命令git stash && git pull解決衝突或錯誤

本文轉載自:https://www.cnblogs.com/mayi168/p/9070505.html