svn hooks位置一定要确定,即你的 版本库位置,配置hooks更新到对应的网站目录。
一 安装与配置SVN
1.安装subversion
centos: yum install subversion
ubuntu: apt-get install subversion
(关闭svn killall svnserve 创建 svnadmin create )
2.建立版本库,本例中我们的版本库建立在/var/svn,假设该版本库目录为:foo
cd /var
mkdir svn
svnadmin create foo
3.配置版本库
刚才建立的版本库,需要配置认证方式,用户名和密码,权限配置这三个文件,分别对应/var/svn/foo/conf下的
svnserve.conf,passwd和authz这三个文件。
svnserve.conf(去掉注释)
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz.conf
realm = 填写登录时提示的认证域名称
passwd文件添加相应用户名和密码:
user1 = password1
user2 = password2
如果要使用通用的用户名密码和权限认证,指定password-db和authz-db路径到相应passwd和authz.conf文件位置即可
authz文件添加相应用户名到组并分配库权限:
[groups]
foo_user = user1,user2 (多个用户以逗号分隔)
[/]
@foo_user = rw
* =
其中*=意味着匿名用户没有权限,引用组名时要加上@符号
4.启动和使用
配置成功以后,启动svn版本库服务
svnserve -d -r /var/svn
获取版本库:
svn co svn://ip地址/foo
服务器上可以直接使用 svn co file:///var/svn/foo
(win上直接checkout,然后填写svn://ip地址/foo即可,输入密码)
二、同步到WEB目录
1.创建web目录(已有目录则略过)
cd /var
mkdir www
chmod 777 www
2.检出副本到/var/www
svn co file:///var/svn/foo
3.增加hooks(钩子)文件
在/var/svn/foo/hooks/目录中
# start-commit 提交前触发事务
# pre-commit 提交完成前触发事务
# post-commit 提交完成时触发事务
# pre-revprop-change 版本属性修改前触发事务
# post-revprop-change 版本属性修改后触发事务
我们这里只需要修改post-commit文件:
cp post-commit.tmpl post-commit
编辑该文件
#!/bin/sh
# 配置库地址
REPOS="$1"
REV="$2"
# WEB目录
BASEPATH=/var/www
# web文件夹名与svn库名相同
NAME=$(basename $REPOS)
WEBPATH="$BASEPATH/$NAME"
# 避免中文信息乱码
export LANG="zh_CN.UTF-8"
# 可以配合svnlook命令更新指定目录
svn update $WEBPATH --username=user1 --password=password1 --no-auth-cache
保存后需要修改post-commit文件权限为755,否则SVN无法调用执行
chmod 755 post-commit
我的hooks/post-commit文件
[root@localhost hooks]# cat post-
post-commit post-lock.tmpl post-unlock.tmpl
post-commit.tmpl post-revprop-change.tmpl
[root@localhost hooks]# cat post-commit
#!/bin/sh
# POST-COMMIT HOOK
#
# The post-commit hook is invoked after a commit. Subversion runs
# this hook by invoking a program (script, executable, binary, etc.)
# named 'post-commit' (for which this file is a template) with the
# following ordered arguments:
#
# [1] REPOS-PATH (the path to this repository)
# [2] REV (the number of the revision just committed)
#
# The default working directory for the invocation is undefined, so
# the program should set one explicitly if it cares.
#
# Because the commit has already completed and cannot be undone,
# the exit code of the hook program is ignored. The hook program
# can use the 'svnlook' utility to help it examine the
# newly-committed tree.
#
# On a Unix system, the normal procedure is to have 'post-commit'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# Note that 'post-commit' must be executable by the user(s) who will
# invoke it (typically the user httpd runs as), and that user must
# have filesystem-level permission to access the repository.
#
# On a Windows system, you should name the hook program
# 'post-commit.bat' or 'post-commit.exe',
# but the basic idea is the same.
#
# The hook program typically does not inherit the environment of
# its parent process. For example, a common problem is for the
# PATH environment variable to not be set to its usual value, so
# that subprograms fail to launch unless invoked via absolute path.
# If you're having unexpected problems with a hook program, the
# culprit may be unusual (or missing) environment variables.
#
# Here is an example hook script, for a Unix /bin/sh interpreter.
# For more examples and pre-written hooks, see those in
# the Subversion repository at
# http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and
# http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/
REPOS="$1"
REV="$2"
#WEB 目录
#BASEPATH=/www/web/OP/mocules/oss/launcher
#NAME=$(basename $REPOS)
#WEBPATH="/www/web/OP/modules/oss/launcher"
# 避免中文信息乱码
#export LANG="zh_CN.UTF-8"
# 可以配合svnlook命令更新指定目录
#svn update $WEBPATH --username=lm --password=123456 --no-auth-cache
svn update /www/web/OP/modules/oss/launcher --username=lm --password=123456 --no-auth-cache
#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf