SVN鉤子hooks使用

簡述

因爲安全緣由,Subversion版本庫在一個空環境中執行鉤子腳本-->就是沒有任何環境變量,甚至沒有$PATH或%PATH%。注意必須在你的鉤子中設置好環境變量或爲你的程序指定好絕對路徑。nginx

默認狀況下,SVN版本庫鉤子的目錄中包含各類版本庫鉤子模板web

[root@localhost hooks]# ll /svn/HLink/hooks/
總用量 36
-rw-r--r-- 1 root root 1977 12月 19 2016 post-commit.tmpl
-rw-r--r-- 1 root root 1638 12月 19 2016 post-lock.tmpl
-rw-r--r-- 1 root root 2289 12月 19 2016 post-revprop-change.tmpl
-rw-r--r-- 1 root root 1567 12月 19 2016 post-unlock.tmpl
-rw-r--r-- 1 root root 3426 12月 19 2016 pre-commit.tmpl
-rw-r--r-- 1 root root 2410 12月 19 2016 pre-lock.tmpl
-rw-r--r-- 1 root root 2786 12月 19 2016 pre-revprop-change.tmpl
-rw-r--r-- 1 root root 2100 12月 19 2016 pre-unlock.tmpl
-rw-r--r-- 1 root root 2780 12月 19 2016 start-commit.tmpl

 

start-commit  事務建立以前vim

傳給 hook 的 參數:安全

-         參 數 1 , 代碼庫路徑。bash

-         參 數 2 , 試圖提交的用戶名。服務器

hook 的返回值:非 0 則 終止。ssh

一 般用途:判斷用戶是否有權限進行提交 操做。svn

pre-commit  事務完成,但未提交post

-         參 數 1 , 代碼庫路徑。測試

-         參 數 2 , 事務名。

hook 的返回值:非 0 則 終止提交,操做回滾。

一 般用途:對提交內容進行檢查。如要求 提交必須填寫提交信息。

post-commit  事務提交完畢,新的修訂版被 建立

傳給 hook 的 參數:

-         參 數 1 , 代碼庫路徑。

-         參 數 2 , 剛建立的修訂版號。

hook 的返回值被忽 略。

一 般用途:發送郵件通知,或備份代碼 庫。

pre-revprop-change  修改修訂版屬性(如提交時提 供的信息 message )以前

因爲修訂版屬性一旦修改就會 永久的丟失,除非安裝這個事件的 hook ,subversion 的 客戶端不容許遠程修改修訂版屬性。

傳給 hook 的 參數:

-         參 數 1 , 代碼庫路徑。

-         參 數 2 , 要修改的修訂版號。

-         參 數 3 , 操做用戶名。

-         要 修改的屬性。

hook 的返回值:非 0 則 終止。

一 般用途:保存修訂版屬性的改變記錄。

post-revprop-change  修訂版屬性值被修改以後。

若是沒有安裝 pre-revprop-change 的 hook , 這個事件的 hook 不會被執行。

傳給 hook 的 參數:

-         參 數 1 , 代碼庫路徑。

-         參 數 2 , 要修改的修訂版號。

-         參 數 3 , 操做用戶名。

-         要 修改的屬性。

hook 的返回值被忽 略。

一 般用途:發送郵件

實踐

因svn倉庫在服務器2上, 服務器3是測試服務器, 鉤子要遠程執行更新腳本.因此先作ssh免密認證

服務器2上 生成祕鑰對   ssh-keygen   , ssh-copy-id 把公鑰發給服務器3 

非默認端口時:  ssh-copy-id    -i   /root/.ssh/id_rsa.pub  '-p 224 root@111.198.29.223'

[root@localhost ]# ssh-keygen  #回車
[root@localhost ]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.0.3

編輯鉤子腳本

[root@localhost hooks]# pwd
/svn/hcloud/hooks

[root@localhost hooks]# vim post-commit

#!/bin/bash
export LANG=en_US.UTF-8

REPOS="$1"
TXN="$2"

datelog=`date +%Y%m%d`
datetime=`date +%Y-%m-%d-%T-%A`
# Make sure that the log message contains some text.

/bin/echo $REPOS $TXN $datetime >>/tmp/svn-hcloud-$datelog.log || exit 1

#  ssh 192.168.0.3 "sh /srv/uphcloud.sh"
ssh 192.168.0.3 "nohup /srv/uphcloud.sh  >test.log 2>&1 &"

# Check that the author of this commit has the rights to perform # the commit on the files and directories being modified. # commit
-access-control.pl "$REPOS" "$TXN" commit-access-control.cfg || exit 1 # All checks passed, so allow the commit. exit 0

 

[root@local ~]# cat /srv/uphcloud.sh 
#!/bin/bash

type svn
if [ $? != 0 ];then
    echo "need install subversion first"
    exit
fi

cd /srv

[ ! -e /srv/hcloud ]&& mkdir -p /srv/hcloud;cd /srv/hcloud

echo `pwd` >>/srv/herror.log
echo `date` >>/srv/herror.log

# BScloud 

echo "hcloud" >>/srv/herror.log
#if [ ! -e /srv/hcloud ]; then
if [ ! -e /srv/hcloud/web ]; then
    /usr/bin/svn co --no-auth-cache svn://192.168.0.2/hcloud/web --username svn帳號 --password 密碼 2>&1 >>/srv/herror.log
else
    #cd /srv/hcloud/
    cd /srv/hcloud/web
    /usr/bin/svn update --no-auth-cache --username svn帳號 --password 密碼 2>&1 >>/srv/herror.log
    cd /srv
fi

cp -uraf /srv/hcloud/web/* /data/web/hcloud/web/
cd /data/web/
chown -R nginx:nginx hcloud/
chmod -R 777 /data/web/hcloud/web/web_code/smarty/templates_c
相關文章
相關標籤/搜索