SVN提交与自动部署


代码自动更新,解放你的双手

(一)SVN简介

Subversion(SVN) 是一个开源的版本控制系統。

(二)SVN安装

yum install subversion -y
mkdir /home/svn && svnadmin create /home/svn
authz 是权限控制文件
passwd 是帐号密码文件
svnserve.conf 是SVN服务配置文件
svnserve -d -r /home/svn

(三)SVN命令

add
blame (praise, annotate, ann)
cat
changelist (cl)
checkout (co)
cleanup
commit (ci)
copy (cp)
delete (del, remove, rm)
diff (di)
export
help (?, h)
import
info
list (ls)
lock
log
merge
mergeinfo
mkdir
move (mv, rename, ren)
patch
propdel (pdel, pd)
propedit (pedit, pe)
propget (pget, pg)
proplist (plist, pl)
propset (pset, ps)
relocate
resolve
resolved
revert
status (stat, st)
switch (sw)
unlock
update (up)
upgrade
svn co #等同于svn checkout
svn up #等同于svn update
svn commit -m 'xxxx'

(四)SVN钩子

钩子名称钩子作用作用
post-commit提交之后触发提交成功之后执行某操作,如自动更新
pre-commit提交之前触发禁止某些提交,如没有写备注

钩子位置:/home/svn/hooks/post-commit

授予执行权限:chmod +x /home/svn/hooks/post-commit

svnlook author $REPOS 最近提交的员工

svnlook log $REPOS 最近提交的备注

svnlook changed $REPOS 最近一次提交修改了哪些文件(U更新,A添加,D删除)

$ svnlook changed /home/svn
U   Java/think/app/index/controller/FootballController.php
A   Java/think/test11.txt
D   Java/think/test10.txt

(五)SVN自动部署

本地部署
#!/bin/sh
REPOS="$1"
REV="$2"
RECORD=`svnlook log $REPOS`
echo `date`,$REPOS,$REV,$RECORD >> /var/log/svnlog.txt
if [ "${RECORD}" == "deploy" ]; then
    /usr/bin/svn update /home/web --no-auth-cache >> /dev/null
    chown www.www -R /home/web
fi
异地部署
#!/bin/sh
REPOS="$1"
REV="$2"
RECORD=`svnlook log $REPOS`
echo `date`,$REPOS,$REV,$RECORD >> /var/log/svnlog.txt
if [ "${RECORD}" == "deploy" ]; then
    # ssh root@1.1.1.1 "bash xxx.sh"
    # 以其他账号登录
    su www -c "scp /www/wwwroot/home/* root@10.10.10.10:/www/wwwroot/home"
fi
echo `date`,`whoami`,$REPOS,$REV,$RECORD >> /var/log/svnlog-`date +%Y-%m`.txt
DIR=`svnlook dirs-changed $REPOS`
if [[ "${RECORD}" == "deploy" && "${DIR}" =~ "EASYDO3.0test" ]]; then
    /usr/bin/svn update /www/wwwroot/xxxx --username xxx --password xxx123 --no-auth-cache >> /dev/null
    chown www.www -R /www/wwwroot/xxxx
fi

参考文档

Linux下安装SVN服务端小白教程

svn钩子(post-commit)区分根目录上传的一级文件夹

编写Shell脚本的最佳实践