天天看点

利用SVN的post-commit钩子实现多项目自动同步

    svn的post-commit钩子脚本在每次提交(commit)之后运行,我们可以在这个脚本里实现一些比较实用的功能,比如发送邮件提醒、自动备份版本库,自动同步代码到web服务器等。

    这里用post-commit实现多项目自动同步,思路:在svn版本仓库根目录下划分多个项目文件夹,项目组成员在提交文件时,post-commit自动判断文件所属的项目,然后同步到相应的WEB服务器上。

测试环境配置:

SVN服务器:172.16.4.234

项目1WEB服务器:172.16.4.235

项目1代码库:svn://172.16.4.234/project1

项目2WEB服务器:172.16.4.236

项目2代码库:svn://172.16.4.234/project2

下面是操作步骤:

一、WEB服务器

    WEB服务器作为SVN客户端,从SVN服务器上checkout一份代码到本地。注意要先切换到www用户再checkout,因为www是apache或nginx的执行用户(你的环境可能不一样)。

    项目1WEB服务器,站点根目录为/data/apps/project1:

<code>shell</code><code># su www</code>

<code>shell$ </code><code>cd</code> <code>/data/apps/</code>

<code>shell$ svn co --username zb --password 123456 svn:</code><code>//172</code><code>.16.4.234</code><code>/project1</code>

    项目2WEB服务器,站点根目录为/data/apps/project2:

<code>shell$ svn co --username zb --password 123456 svn:</code><code>//172</code><code>.16.4.234</code><code>/project2</code>

二、配置ssh无密码访问

    SVN服务器需要无密码ssh访问WEB服务器,方便post-commit脚本ssh到WEB服务器上执行svn up。注意这里设置www用户无密码访问,因为WEB服务器上代码是www用户checkout出来的。

    在SVN服务器上执行ssh-keygen -t rsa,然后一直按回车键,生成公钥和私钥保存在/root/.ssh/。

    然后在WEB服务器上先切换到www用户,建立authorized_keys文件:

<code>[root@localhost ~]</code><code># su www</code>

<code>[www@localhost root]$ </code><code>cd</code>

<code>[www@localhost ~]$ </code><code>mkdir</code> <code>.</code><code>ssh</code>

<code>[www@localhost ~]$ </code><code>chmod</code> <code>755 .</code><code>ssh</code><code>/</code>

<code>[www@localhost ~]$ </code><code>vi</code> <code>.</code><code>ssh</code><code>/authorized_keys</code>

    把SVN服务器上/root/.ssh/id_rsa.pub公钥文件的内容拷贝到authorized_keys文件里。然后设置authorized_keys文件权限为600。

<code>[www@localhost ~]$ </code><code>chmod</code> <code>600 .</code><code>ssh</code><code>/authorized_keys</code>

    测试,SVN服务器登录项目1WEB服务器:

<code>[root@localhost ~]</code><code># ssh [email protected]</code>

<code>[www@localhost ~]$</code>

三、SVN服务器post-commit

    post-commit在SVN的hooks目录下。

    post-commit脚本内容:

<code>#!/bin/sh</code>

<code>REPOS=</code><code>"$1"</code>                  <code># 仓库的路径</code>

<code>REV=</code><code>"$2"</code>                    <code># 新提交的版本号</code>

<code>LOGFILE=</code><code>/var/log/svn</code><code>.log    </code><code># 钩子脚本的日志</code>

<code># 脚本的标准输出和标准错误输出都打印到日志文件里</code>

<code>exec</code> <code>1&gt;&gt;</code><code>"$LOGFILE"</code>

<code>exec</code> <code>2&gt;&amp;1</code>

<code>SVNLOOK=</code><code>/usr/bin/svnlook</code>

<code>TIME=$(</code><code>date</code> <code>"+%Y-%m-%d %H:%M:%S"</code><code>)</code>

<code>AUTHOR=$($SVNLOOK author -r $REV </code><code>"$REPOS"</code><code>)  </code><code>#提交作者</code>

<code>CHANGEDDIRS=$($SVNLOOK </code><code>dirs</code><code>-changed $REPOS) </code><code>#修改的目录集合</code>

<code>MESSAGE=$($SVNLOOK log -r $REV </code><code>"$REPOS"</code><code>)    </code><code>#提交时的备注信息,不建议用中文</code>

<code># SVN客户端配置,需要自行修改**********************************</code>

<code>CLIENT1=172.16.4.235        </code><code>#project1的服务器</code>

<code>CLIENT2=172.16.4.236        </code><code>#project2的服务器</code>

<code>CLIENTSVNROOT=</code><code>/data/apps</code>    <code>#WEB服务器的代码根目录</code>

<code>SVNUSER=</code><code>"zb"</code>

<code>SVNPASSWD=</code><code>"123456"</code>

<code>#**************************************************************</code>

<code>function</code> <code>myecho() {</code>

<code>    </code><code>echo</code> <code>"$TIME"</code> <code>"$*"</code>

<code>}</code>

<code>myecho </code><code>"**************************************************************"</code>

<code>myecho </code><code>"提交版本:$REV 作者:$AUTHOR"</code>

<code>myecho </code><code>"提交备注:$MESSAGE"</code>

<code>myecho </code><code>"修改目录:$(echo $CHANGEDDIRS | tr '\n' ' ')"</code>

<code>MASTERDIR=$(</code><code>echo</code> <code>"$CHANGEDDIRS"</code> <code>| </code><code>head</code> <code>-1)  </code><code>#CHANGEDDIRS里的最上级目录</code>

<code># 遍历提交的代码目录,同步到WEB服务器上</code>

<code>while</code> <code>[ </code><code>"$CHANGEDDIRS"</code> <code>!= </code><code>""</code> <code>];</code><code>do</code>

<code>    </code><code>PROJECT=$(</code><code>echo</code> <code>$MASTERDIR | </code><code>awk</code> <code>-F / </code><code>'{print $1}'</code><code>)</code>

<code>    </code><code># 判断项目文件夹</code>

<code>    </code><code>if</code> <code>[ </code><code>"$PROJECT"</code> <code>== </code><code>"project1"</code> <code>];</code><code>then</code>

<code>        </code><code>myecho</code>

<code>        </code><code>myecho </code><code>"项目:$PROJECT 同步目录:$MASTERDIR"</code>

<code>        </code><code>myecho </code><code>"同步 $MASTERDIR 到 $CLIENT1:$CLIENTSVNROOT/$MASTERDIR"</code>

<code>        </code><code># 无密码ssh连接到客户端服务器,执行svn up</code>

<code>        </code><code>/usr/bin/ssh</code> <code>www@$CLIENT1 </code><code>"export LANG=en_US.UTF-8; svn up --non-interactive --username $SVNUSER --password $SVNPASSWD '$CLIENTSVNROOT/$MASTERDIR'"</code>

<code>    </code><code>elif</code> <code>[ </code><code>"$PROJECT"</code> <code>== </code><code>"project2"</code> <code>];</code><code>then</code>

<code>        </code><code>myecho </code><code>"同步 $MASTERDIR 到 $CLIENT2:$CLIENTSVNROOT/$MASTERDIR"</code>

<code>        </code><code>/usr/bin/ssh</code> <code>www@$CLIENT2 </code><code>"export LANG=en_US.UTF-8; svn up --non-interactive --username $SVNUSER --password $SVNPASSWD '$CLIENTSVNROOT/$MASTERDIR'"</code>

<code>    </code><code>else</code>

<code>        </code><code>:</code>

<code>    </code><code>fi</code>

<code>    </code><code># 在目录集合里删除子目录</code>

<code>    </code><code>CHANGEDDIRS=$(</code><code>echo</code> <code>"$CHANGEDDIRS"</code> <code>| </code><code>grep</code> <code>-</code><code>v</code> <code>"^$MASTERDIR"</code><code>)</code>

<code>    </code><code># 获取新的需要同步的最上级目录</code>

<code>    </code><code>MASTERDIR=$(</code><code>echo</code> <code>"$CHANGEDDIRS"</code> <code>| </code><code>head</code> <code>-1)</code>

<code>done</code>

    不要忘记给post-commit可执行权限。

四、测试

    日志显示文件已经同步到项目1WEB服务器上了。

本文转自 张斌_青岛 51CTO博客,原文链接:http://blog.51cto.com/qicheng0211/1563159