基本步骤:
1、安装必需的subversion
2、创建版本库
3、配置用户和权限
4、钩子和svn常用命令说明
一、安装subversion
在这里我们使用yum来安装subversion,使用以下命令即可完成。
[root@localhost ~]# yum -y install subversion
二、创建版本库
[root@localhost ~]# svnadmin create /var/www/svnroot
如果需要创建子版本库也可以使用 svnadmin create /var/www/svnroot/子版本库名称
的方式进行创建。但是要注意上级版本库中是否有文件夹叫这个名字,不然不重名冲突
三、版本库管理配置
进行刚才创建的版本库目录下的conf目录,可以看到有三个文件。
[root@localhost ~]# cd /var/www/svnroot/conf [root@localhost conf]# ls authz passwd svnserve.conf
svnserve.conf 这个是版本库的配置文件
passwd 这个是记录用户帐号密码的文件
authz 这个则是记录组、权限和身份验证的文件
1、配置svnserve.conf文件
这里需要设置以下几处
anon-access = none 指定匿名权限,默认为可读,现设置匿名无权限
auth-access = write 用户有写权限
password-db = passwd psswd文件地址 password-db 设置成 ../../passwd 这样可以方便统一管理
authz-db = authz 同上
注意去掉#注释以后配置一定要顶格写,下同。
[root@localhost conf]# vim svnserve.conf [general] ### These options control access to the repository for unauthenticated ### and authenticated users. Valid values are "write", "read", ### and "none". The sample settings below are the defaults. anon-access = none auth-access = write ### The password-db option controls the location of the password ### database file. Unless you specify a path starting with a /, ### the file's location is relative to the conf directory. ### Uncomment the line below to use the default password file. password-db = passwd ### The authz-db option controls the location of the authorization ### rules for path-based access control. Unless you specify a path ### starting with a /, the file's location is relative to the conf ### directory. If you don't specify an authz-db, no path-based access ### control is done. ### Uncomment the line below to use the default authorization file. authz-db = authz ### This option specifies the authentication realm of the repository. ### If two repositories have the same authentication realm, they should ### have the same password database, and vice versa. The default realm ### is repository's uuid. # realm = My First Repository
2、配置passwd
该文件中记录svn用户名密码,以 (帐号 = 密码)或 (帐号 : 密码)的形式进行储存。
多用户之前用换行区分。这里配置了一个用户名为sgf密码为123456的svn 账户。
[root@localhost conf]# vim passwd ### This file is an example password file for svnserve. ### Its format is similar to that of svnserve.conf. As shown in the ### example below it contains one section labelled [users]. ### The name and password for each user follow, one account per line. [users] # harry = harryssecret # sally = sallyssecret sgf = 123456
3、authz文件的配置
[groups]表用于用户组的配置例如
“group1 = sgf,sgf2”这样就将这2个用户方在了group1 组织之中。
建立组是为了方便给一组相同权限的用户分配权限。
[/] 指定是svn的根版本库
版本库目录格式:
[<版本库>:/项目/目录]
@<用户组名> = <权限>
<用户名> = <权限>
[root@localhost conf]# vim authz ### This file is an example authorization file for svnserve. ### Its format is identical to that of mod_authz_svn authorization ### files. ### As shown below each section defines authorizations for the path and ### (optional) repository specified by the section name. ### The authorizations follow. An authorization line can refer to a ### single user, to a group of users defined in a special [groups] ### section, or to anyone using the '*' wildcard. Each definition can ### grant read ('r') access, read-write ('rw') access, or no access ### (''). [groups] # harry_and_sally = harry,sally # [/foo/bar] # harry = rw # * = # [repository:/baz/fuz] # @harry_and_sally = rw # * = r [/] sgf = rw * =
这样svn版本库就算配置完成了。
运行 svn
[root@localhost ~]# svnserve -d -r /var/www/svnroot
我们继续说下其他常用的一些配置
1、开机自动运行
在/etc/rc.d/rc.local 添加 “/usr/bin/svnserve -d -r svn版本库目录” 即可
[root@localhost ~]# vim /etc/rc.d/rc.local # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /usr/bin/svnserve -d -r /var/www/svnroot
2、钩子自动更新
说明,在开发时经常需要提交代码,如果使用钩子自动更新的话,只要有人提交后就能自动更新到服务器上,非常方便哦。
首先必须对代码目录checkout
[root@localhost ~]# cd /var/www [root@localhost www]# svn checkout svn://localhost html Authentication realm: <svn://localhost:3690> 21d46c22-96a8-465b-9d0b-58a1e04abdfd Password for 'root': Authentication realm: <svn://localhost:3690> 21d46c22-96a8-465b-9d0b-58a1e04abdfd Username: sgf Password for 'sgf': Checked out revision 0.
如果需要先提交服务器上的代码 可以使用
svn add 文件或目录
svn ci -m 注释
进行提交
svn up 可以将最新版本更新出来。
进入版本库hooks目录
将post-commit.tmpl 模板复制一份 取名post-commit
在末尾加上
svn up /var/www/html/ –username sgf –password 123456
并赋予执行权限。
[root@localhost ~]# cd /var/www/svnroot/hooks/
[root@localhost hooks]# ls
post-commit.tmpl post-revprop-change.tmpl pre-commit.tmpl pre-revprop-change.tmpl start-commit.tmpl
post-lock.tmpl post-unlock.tmpl pre-lock.tmpl pre-unlock.tmpl
[root@localhost hooks]# cp post-commit.tmpl post-commit
[root@localhost hooks]# vim post-commit
# 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.collab.net/repos/svn/trunk/tools/hook-scripts/ and
# http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/
REPOS="1"
REV="2"
svn up /var/www/html/ --username sgf --password 123456
[root@localhost hooks]# chmod +x post-commit
这样当用户提交文件的第时候,svn将会自动执行更新,使代码同步。