天天看點

SVN hook實作SVN與WEB測試伺服器同步

多人開始使用subversion之後,就想着,要建立一個測試用的伺服器,不需要把檔案update到本地再進行測試。

原理:基于subversion的鈎子,即hook(在每個版本庫下有hooks檔案夾,裡面有很多鈎子程式)。在subversion執行一個操作時, 那會相應的首先去調用相關的鈎子程式(如果存在的話)。那麼實作一個同步的測試伺服器,我們隻需要在一個使用者執行完畢一個commit操作之後,讓鈎子程式去自動更新測試伺服器的檔案即可。通過這個思路,我們需要作的就是建立一個post-commit的鈎子。

SVN hook實作SVN與WEB測試伺服器同步

1.頁面同步按鈕觸發

update_develop_svn.sh

#!/bin/sh
SVN=/usr/bin/svn
WEB=$1
WWWROOT=/data/www/wwwroot/test.com/
SVNTMP=/data/www/svntmp/test.com/

if [ ! -d "${WWWROOT}${WEB}" ]; then
      echo "arg error"
      exit
fi

#export LANG =en_US.UTF-8
$SVN update $SVNTMP$WEB  --username *** --password **
/cp -rv $SVNTMP$WEB $WWWROOT #svn更新檔案複制到項目中
find $WWWROOT$WEB -type d -name ".svn" | xargs rm -rf #先(遞歸)找到目前路徑下含有 .svn的檔案目錄
chown www.www $WWWROOT$WEB -R
           

php執行shell,網頁可以通路

<?php
if (isset($_POST['Submit3']) &&$_POST['Submit3']){
    shell_exec("./update_develop_svn.sh");// exec("./update_develop_svn.sh",$output);
}
?>
<form name="form3" method="post" action="">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <label>
        <input name="Submit3" type="submit" id="Submit3" value="同步" οnclick="return confirm('确認需要同步?')">
    </label>
</form>
           

2.windows自動更新

On a Windows system, you should name the hook program 'post-commit.bat' or 'post-commit.exe',

在svn項目demo\hooks下面,建立post-commit.bat檔案

@echo off
"C:\svn\bin\svn.exe"  cleanup "c:\htdocs\demo"
"C:\svn\bin\svn.exe"  update "c:\htdocs\demo" --quiet  --username XXX --password XXXX
           

3. linux自動更新

 項目庫的 hooks/ 目錄下建立 post-commit 檔案

#!/bin/sh 
SVN=/usr/bin/svn           #這裡配置的是svn安裝bin目錄下的svn檔案 
WEB=/var/www/html/test     #要更新的目錄 
$SVN update $WEB --username xxx --password xxx 
           

讓post-commit有執行的權限,并将checkout的項目相關權限和宿主改一下

chmod 777 post-commit
chown -R www:www ./.svn
chown -R www:www ./.svn/*
chown -R www:www ./.svn/
chown -R www:www ./.svn
chown -R www:www ./
           

 以上的實作原理參考hooks/post-commit.tmpl檔案