├── README.md ├── post-commit ├── push_online.sh ├── svn.php ├── update.php └── updateMoreSecurity.php /README.md: -------------------------------------------------------------------------------- 1 | svn-hook-to-remote-server 2 | ========================= 3 | ##你是否也有这样的场景 4 | A服务器是Web测试服务器 5 | 6 | B服务器为SVN服务器 7 | 8 | 我们需要频繁地将代码提交到B服务器上,然后在A服务器上从SVN里检出最新的代码,再做测试。(*有时候不是所有人都有A服务器的登录权限的,又或者登录A服务器非常麻烦,比如要先连VPN,然后SSH连接。那样自动触发更新就非常有必要了。*) 9 | 10 | **如果你开发的时候有这个使用情景,那么这几段简短的代码对你非常有用啦!它会在我们往SVN服务器提交代码的时候,自动更新到A服务器上,不管你是在内网还是外网。** 11 | 12 | ##使用说明 13 | >1. `post-commit` 存放在B服务器(SVN服务器)是SVN的钩子文件,存放在 `svn项目路径/hooks/post-commit` 14 | 15 | >2. `svn.php` 存放在B服务器(SVN服务器)上,供 `post-commit` 来执行,通过 `curl` 来触发模拟http协议访问A服务器上的 `update.php` 16 | 17 | >3. `update.php` 存放在A服务器(Web测试服务器)上,保证该脚本可以通过Web方式访问 18 | 19 | >4. `updateMoreSecurity.php` 存放在C服务器(Web线上服务器)上,手动更新脚本,保证该脚本可以通过Web方式访问 20 | 21 | ##注意事项 22 | 使用过程可以会有很多的脚本执行权限问题,请往nginx和apache的属主和属组上修改 23 | ```shell 24 | chown -R www:www xxx 25 | ``` 26 | 还有需要注意的就是svn1.6版本之后在远程执行 `svn up` 的时候会提示时候保存密码,需要对远程服务器上的svn配置做下修改,可以参考我之前的帖子: http://mengkang.net/67.html 27 | -------------------------------------------------------------------------------- /post-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export LANG="zh_CN.UTF-8" 3 | #更新到远程的服务器 4 | #/home/wwwroot/xxx/svn.php 为svn.php在svn服务器上的存放地址 5 | /usr/local/php/bin/php /home/wwwroot/xxx/svn.php -------------------------------------------------------------------------------- /push_online.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ######################## config ######################## 4 | 5 | # 升级日志 6 | publish_log_file="/update/log/path/publish.log" 7 | 8 | # 需要邮件通知的人 9 | master=( 10 | i@zhoumengkang.com 11 | mengkang@topit.me 12 | zhoumengkang@php.net 13 | ) 14 | 15 | # 更新服务器列表 16 | remote_hosts=( 17 | 192.168.1.14 18 | 192.168.1.10 19 | 192.168.1.17 20 | 192.168.1.13 21 | 192.168.1.14 22 | 192.168.1.13 23 | 192.168.1.26 # 图片上传服务器 24 | 192.168.1.17 # 用于 static.mengkang.net 图片访问 25 | ) 26 | ######################## config ######################## 27 | 28 | function_svn_diff(){ 29 | svn_diff_size=`sudo svn diff|wc -c`; 30 | 31 | if((svn_diff_size>0));then 32 | echo -e "\e[10;31m以下文件修改了,但还未提交\e[0m" 33 | echo -e "\e[10;31m============================================================\e[0m" 34 | sudo svn diff|grep "Index:"|awk '{print $2}' 35 | echo -e "\e[10;31m============================================================\e[0m" 36 | echo -e "\e[10;31m请先进行 svn 代码提交再执行上线操作\e[0m" 37 | exit; 38 | fi 39 | } 40 | 41 | function_svn_need_add(){ 42 | 43 | echo -e "\e[10;31m以下文件还未加入版本库\e[0m" 44 | echo -e "\e[10;31m============================================================\e[0m" 45 | sudo svn st|grep "?"|grep "\.php"|grep -v "site2.0/back"|awk '{print $2}' 46 | echo -e "\e[10;31m============================================================\e[0m" 47 | echo -e "\e[10;31m忽略这些文件直接上线? \e[0m\e[10;32m yes \e[0m / \e[10;31m no \e[0m" 48 | 49 | read comfirm 50 | 51 | if [ "$comfirm" != "yes" ]; then 52 | echo -e "\e[0m\e[10;32m上线取消\e[0m" 53 | exit 54 | fi 55 | } 56 | 57 | 58 | function_puslish_init(){ 59 | for i in "${!remote_hosts[@]}";do 60 | if(($i == 0)); then 61 | function_rsync_online ${remote_hosts[$i]} | tee $publish_log_file; 62 | 63 | # 先看有没有更新文件,如果没有更新则退出,没有更新则只有下面四行,如果有更新则会有更新的文件列表 64 | # sending incremental file list 65 | 66 | # sent 147892 bytes received 639 bytes 99020.67 bytes/sec 67 | # total size is 707645226 speedup is 4764.29 68 | num=`cat $publish_log_file|wc -l` 69 | if(($num<5)); then 70 | echo -e "\e[0m\e[10;32m暂无更新\e[0m"; 71 | exit; 72 | fi 73 | else 74 | function_rsync_online ${remote_hosts[$i]} 75 | fi 76 | done 77 | } 78 | 79 | # 分发 80 | function_rsync_online(){ 81 | if [[ $1 ]]; then 82 | export RSYNC_PASSWORD=rsync 83 | /usr/bin/rsync -avz --delete --exclude=".*" /path/of/code/ rsync@$1::topit_online/ 84 | fi 85 | } 86 | 87 | function_publish_quit(){ 88 | # online 目录版本 89 | head_log=`sudo svn log -r head|grep -v "\-\-"|grep -v ^$` 90 | publish_log=`cat $publish_log_file` 91 | # 类似于 r175 | xiaofei | 2015-07-08 15:40:24 +0800 (Wed, 08 Jul 2015) | 1 line 活动规则显示 bug 修复 92 | echo $head_log; 93 | 94 | for x in ${master[@]};do 95 | echo -e "$head_log\n---------------------------------------\n$publish_log\n---------------------------------------\n上线人: $USER"|mail -s "topit上线通知" $x 96 | echo -e "已邮件通知\e[0m\e[10;32m $x\e[0m"; 97 | done 98 | } 99 | 100 | cd /path/of/code/; 101 | 102 | function_svn_diff; 103 | function_svn_need_add; 104 | function_puslish_init; 105 | function_publish_quit; 106 | -------------------------------------------------------------------------------- /svn.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | error_reporting(E_ALL); 8 | //设置下字符集,有不认识的字符,也会导致不可更新 9 | putenv("LC_CTYPE=zh_CN.UTF-8"); 10 | //"/home/wwwroot/test/" 为代码更新到的指定目录路径 11 | $handle = popen('svn up --username zmk --password 123456 /home/wwwroot/test/ 2>&1','r'); 12 | //echo "'$handle'; " . gettype($handle) . "\n"; 13 | $read = stream_get_contents($handle); 14 | //TODO 如果在$read中可以匹配到“error/conflict”,就应该发送邮件到管理员的邮箱了! 15 | echo $read; 16 | pclose($handle); 17 | -------------------------------------------------------------------------------- /updateMoreSecurity.php: -------------------------------------------------------------------------------- 1 | 9 |
10 | 11 | 12 | 48 |
49 | &1', 'r'); 55 | $read = stream_get_contents($handle); 56 | echo "
";
57 | printf($read);
58 | echo "
"; 59 | pclose($handle); 60 | --------------------------------------------------------------------------------