Automatic synchronization of multiple projects using the svn post-commit hook

Source: Internet
Author: User
Tags svn client

The SVN post-commit hook script runs after each transaction commit. We can implement some practical functions in this script, such as sending email reminders and automatic backup of version libraries, automatically synchronize code to the web server.

Here, we use post-commit to automatically synchronize multiple projects. The idea is to divide multiple project folders under the root directory of the svn repository. When project team members submit files, post-commit automatically identifies the project to which the file belongs, and then synchronizes it to the corresponding web server.

Server Configuration:

SVN server: 172.16.4.234

Project 1web server: 172.16.4.235

Project 1 code base: SVN: // 172.16.4.234/project1

Project 2web server: 172.16.4.235

Project 2 code base: SVN: // 172.16.4.234/project2

The procedure is as follows:

I. Web Servers

As the svn client, the web server checkout a copy of code from the svn server to the local machine.

Project 1 web server:

shell# su wwwshell$ cd /data/apps/shell$ svn co --username zb --password 123456 svn://172.16.4.234/project1

Project 2 Web Server:

shell# su wwwshell$ cd /data/apps/shell$ svn co --username zb --password 123456 svn://172.16.4.234/project2

2. Configure SSH password-less access

The SVN server requires SSH without a password to access the Web server, so that the post-commit script SSH can be used to execute SVN up on the web server. Here, the WWW user is used to access without a password, because the code is also sent out by the WWW user checkout.

Iii. SVN Server Post-commit

Post-commit is under the svn hooks directory.

Post-commit script content:

#!/bin/shREPOS="$1"                  # 仓库的路径REV="$2"                    # 新提交的版本号LOGFILE=/var/log/svn.log    # 钩子脚本的日志# 脚本的标准输出和标准错误输出都打印到日志文件里exec 1>>"$LOGFILE"exec 2>&1SVNLOOK=/usr/bin/svnlookTIME=$(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客户端配置,需要自行修改**********************************CLIENT1=172.16.4.235        #project1的服务器CLIENT2=172.16.4.236        #project2的服务器CLIENTSVNROOT=/data/apps    #WEB服务器的代码根目录SVNUSER="zb"SVNPASSWD="123456"#**************************************************************function myecho() {    echo "$TIME" "$*"}myecho "**************************************************************"myecho "提交版本:$REV $AUTHOR"myecho "提交备注:$MESSAGE"myecho "修改目录:$(echo $CHANGEDDIRS | tr ‘\n‘ ‘ ‘)"MASTERDIR=$(echo "$CHANGEDDIRS" | head -1)  #CHANGEDDIRS里的主目录# 遍历提交的代码目录,同步到WEB服务器上while [ "$CHANGEDDIRS" != "" ];do    PROJECT=$(echo $MASTERDIR | awk -F / ‘{print $1}‘)    # 判断项目文件夹    if [ "$PROJECT" == "project1" ];then        myecho        myecho "项目:$PROJECT 同步目录:$MASTERDIR"        myecho "同步 $MASTERDIR 到 $CLIENT1:$CLIENTSVNROOT/$MASTERDIR"        # 无密码ssh连接到客户端服务器,执行svn up        /usr/bin/ssh [email protected]$CLIENT1 "export LANG=en_US.UTF-8; svn up --non-interactive --username $SVNUSER --password $SVNPASSWD ‘$CLIENTSVNROOT/$MASTERDIR‘"    elif [ "$PROJECT" == "project2" ];then        myecho        myecho "项目:$PROJECT 同步目录:$MASTERDIR"        myecho "同步 $MASTERDIR 到 $CLIENT2:$CLIENTSVNROOT/$MASTERDIR"        /usr/bin/ssh [email protected]$CLIENT2 "export LANG=en_US.UTF-8; svn up --non-interactive --username $SVNUSER --password $SVNPASSWD ‘$CLIENTSVNROOT/$MASTERDIR‘"    else        :    fi    # 在目录集合里删除已经同步好的目录    CHANGEDDIRS=$(echo "$CHANGEDDIRS" | grep -v "^$MASTERDIR")    # 获取新的需要同步的主目录    MASTERDIR=$(echo "$CHANGEDDIRS" | head -1)done

Do not forget to grant the post-commit executable permission.

Iv. Test

Upload a "new deployment documentation .txt" under the "project1/client" and "project1/Server" directories of Project 1 to view the log file/var/log/SVN. log: 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/50/wKiom1Q7UJTw7rTBAANxx5IVbGM785.jpg "Title =" qq20151113120234_ .png "alt =" wkiom1q7ujtw7rtbaanxx5ivbgm785.jpg "/>

The log shows that the file has been synchronized to the project 1web server.

This article is from the "departure-Linux technology blog" blog, please be sure to keep this source http://qicheng0211.blog.51cto.com/3958621/1563159

Automatic synchronization of multiple projects using the svn post-commit hook

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.