配置项目提交到git的时候自动同步服务器代码php
1、在服务器项目跟目录新建文件hook.php 代码以下:git
1web 2shell 3json 4服务器 5spa 63d 7日志 8code 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:passwod@gitee.com/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文件中 } ?> |
2、配置码云webhook:

3、确认服务器安装了git,在web目录下clone 项目:git clone https://gitee.com/xxxx/xxxx.git;
4、把git的目录权限改成web用户权限: chown -r www:www
5、手动更新代码:cd /home/www/honli && git pull;
6、git pull 冲突或错误时,用命令git stash && git pull解决冲突或错误