配置項目提交到git的時候自動同步服務器代碼
一、在服務器項目跟目錄新建文件hook.php 代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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解決衝突或錯誤111