SVN 以及自动部署配置 原

2018-08-15 10:01:42 浏览数 (1)

1.配置权限

代码语言:javascript复制
[root@localhost conf]# cat authz 

[groups]
g_manager = user1,user2
g_developer = user2

[repo:/]
@g_manager = rw
* =

[repo:/inspur]
@g_manager = rw
@g_developer = rw
* =

[root@localhost conf]# cat passwd 
[users]
user1=123456
user2=123456
[root@localhost conf]# 

[root@localhost conf]# cat svnserve.conf 
[general]
#匿名访问的权限,可以是read,write,none,默认为read
anon-access = none
#认证用户的权限,可以是read,write,none,默认为write
auth-access = write
#密码数据库的路径,去掉前面的#
password-db = passwd
authz-db = authz

2.启动命令

代码语言:javascript复制
[root@localhost conf]# /usr/bin/svnserve -d -r /home/logonuser/svn --listen-port 7001 &

3.ANT自动部署

代码语言:javascript复制
[root@localhost hooks]# cat build.xml
<?xml version="1.0"?>
<!-- 从svn更新源代码,并重新编译、打包、部署 -->
<project name="auto-deploy" basedir="/home/logonuser/www/guangjie" default="redeploy">

        <!-- 定义存放检出代码的目录 -->
        <property name="project_dir" value="/home/logonuser/www/guangjie"/>
        <!-- 指明jetty的目录 -->
        <property name="jetty_home" value="/home/logonuser/jetty/bin"/>
        <!--在ant中添加property,使用eclipse jdt compiler编译-->
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

        <!-- 当前时间及格式 -->
        <tstamp>
                <format property="current_time" pattern="yyyyMMddhhmm" locale="zh"/>
        </tstamp>

        <!-- 项目的classpath路径 -->
        <path id="project_classpath">
                <fileset dir="${project_dir}/WebRoot/WEB-INF/lib/"> 
                        <include name="*.jar"/>
                </fileset>
        </path> 

                <!-- 清除旧的编译文件 -->
        <target name="clean-class">
        <echo message="开始清空classes文件夹内之前编译生成的旧的类文件,请稍后..." />
                <delete dir="${project_dir}/WebRoot/WEB-INF/classes"/>
                <mkdir dir="${project_dir}/WebRoot/WEB-INF/classes"/>
                <echo message="classes文件清空完毕。" />

        </target>

        <!-- 编译源代码 -->
        <target name="compile-ext" depends="clean-class">
                <echo message="开始编译源文件,请稍后..." /> 
                <javac nowarn="true" destdir="${project_dir}/WebRoot/WEB-INF/classes" source="1.6" target="1.6" 
                                debug="true" debuglevel="lines, vars, source">  
                        <src path="${project_dir}/jfinal.ext"/>
                        <classpath refid="project_classpath"/>
                </javac>  
        </target> 
        <target name="compile" depends="compile-ext">
                <echo message="开始编译源文件,请稍后..." /> 
                <javac nowarn="true" destdir="${project_dir}/WebRoot/WEB-INF/classes" source="1.6" target="1.6" 
                                debug="true" debuglevel="lines, vars, source"> 
                        <src path="${project_dir}/src"/>
                        <classpath refid="project_classpath"/>
                </javac>  
        </target> 

        <!-- 复制配置文件 -->
        <target name="copy-config-files" depends="compile">
                <copy todir="${project_dir}/WebRoot/WEB-INF/classes/">  
                        <fileset dir="${project_dir}/cfg/"/>
                </copy>
        </target> 

        <!-- 部署,主要是执行依赖任务 -->  
  <target name="redeploy" depends="copy-config-files">  
      <echo>部署完毕,请测试是否部署成功。</echo>  
  </target>

</project>

[root@localhost hooks]# cat post-commit
#!/bin/sh   
export LANG=zh_CN.UTF-8 

JAVA_HOME=/usr/java/jdk1.6.0_32
JAVA_BIN=$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
CLASSPATH=./home/logonuser/svn/repo/hooks/jar/1.jar:/home/logonuser/svn/repo/hooks/jar/2.jar:/home/logonuser/svn/repo/hooks/jar/3.jar:/home/logonuser/svn/repo/hooks/jar/4.jar:/home/logonuser/svn/repo/hooks/jar/5.jar:/home/logonuser/svn/repo/hooks/jar/6.jar:$CLASSPATH
#1-6.jar下载链接: https://pan.baidu.com/s/1o8MuzLS 密码: xf6t
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME JAVA_BIN PATH CLASSPATH
    
REPOS="$1"                                       # 仓库的路径   
REV="$2"                                         # 新提交的版本号   
LOGFILE=/home/logonuser/autosvn.log              # 钩子脚本的日志   
# 脚本的标准输出和标准错误输出都打印到日志文件里   
exec 1>>"$LOGFILE" 
exec 2>&1   

SVN=/usr/bin/svn  
SVNLOOK=/usr/bin/svnlook  
TIME=$(date " %Y-%m-%d %H:%M:%S")   
AUTHOR=$($SVNLOOK author -r $REV "$REPOS")      #提交作者   
CHANGEDDIRS=$($SVNLOOK dirs-changed $REPOS)     #修改的目录集合   
MESSAGE=$($SVNLOOK log -r $REV "$REPOS")        #提交时的备注信息,不建议用中文   
    
# SVN客户端配置 ***********************************************       
CLIENTSVNROOT=/home/logonuser/www               #WEB服务器的代码根目录   
SVNUSER="xxx" 
SVNPASSWD="xxx" 
#**************************************************************   
    
function myecho() {   
    echo "$TIME" "$*" 
}   
    
myecho "**************************************************************" 
myecho "提交版本:$REV 作者:$AUTHOR" 
myecho "提交备注:$MESSAGE" 
myecho "修改目录:$(echo $CHANGEDDIRS | tr 'n' ' ')" 
    
Content=$(echo $CHANGEDDIRS | tr 'n' ' ')
MASTERDIR=$(echo "$CHANGEDDIRS" | head -1)  #CHANGEDDIRS里的主目录   
# 遍历提交的代码目录,同步到WEB服务器上   
while [ "$CHANGEDDIRS" != "" ];do
                myecho   
                myecho "同步目录:$MASTERDIR" 
                myecho "同步 $MASTERDIR 到 $CLIENTSVNROOT/$MASTERDIR"
                ${SVN} update --no-auth-cache --username $SVNUSER --password $SVNPASSWD $CLIENTSVNROOT/$MASTERDIR
    # 在目录集合里删除已经同步好的目录   
    CHANGEDDIRS=$(echo "$CHANGEDDIRS" | grep -v "^$MASTERDIR")   
    # 获取新的需要同步的主目录   
    MASTERDIR=$(echo "$CHANGEDDIRS" | head -1)   
done
if [[ $Content =~ "src" ]]
then
        /home/logonuser/jetty.test/bin/jetty.sh stop
        ant -f /home/logonuser/svn/repo/hooks/build.xml
        chown -R logonuser:logonuser /home/logonuser/www/guangjie/WebRoot/WEB-INF/classes
        /home/logonuser/jetty.test/bin/jetty.sh restart
else
        echo "不包含src文件,不需要重启服务器"
fi

if [[ $MESSAGE =~ "none" ]]
then
        echo "包含none,不需要发送通知"
else
        /usr/bin/mutt -s "作者 $AUTHOR 部署了新代码,提交版本 $REV,提交备注 $MESSAGE" xxx@139.com
fi

0 人点赞