且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux下搭建SVN服务器及自动更新项目文件到web发布目录(www)

更新时间:2022-10-08 09:51:19

一、linux服务器端配置

1
2
3
4
5
6
7
8
9
[root@server ~]# rpm -qa | grep sub
subversion-libs-1.7.14-10.el7.x86_64
subversion-1.7.14-10.el7.x86_64
[root@server ~]# ps -ef | grep svn
root      21019  20613  0 21:57 pts/0    00:00:00 grep --color=auto svn
[root@server ~]# mkdir -p /opt/svn/repo
[root@server ~]# svnserve --version
svnserve, version 1.7.14 (r1542130)
   compiled Nov 20 2015, 19:25:09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Copyright (C) 2013 The Apache Software Foundation.
This software consists of contributions made by many people; see the NOTICE
file for more information.
Subversion is open source software, see http://subversion.apache.org/
The following repository back-end (FS) modules are available:
* fs_base : Module for working with a Berkeley DB repository.
* fs_fs : Module for working with a plain file (FSFS) repository.
Cyrus SASL authentication is available.
[root@server ~]# svnadmin create /opt/svn/repo/
[root@server ~]# cd /opt/svn/repo/
[root@server repo]# pwd
/opt/svn/repo
[root@server repo]# ls
conf  db  format  hooks  locks  README.txt
[root@server repo]# cd conf/
[root@server conf]# pwd
/opt/svn/repo/conf
[root@server conf]# ls
authz  passwd  svnserve.conf
[root@server conf]# vim authz 
注:authz最后加上以下两行(这两行解决了 SVN客户端解决authorization failed问题)
[/]
* = rw
[root@server conf]# vim passwd 
注:passwd修改为:
[users]  
admin = 123456    //这里的username和password自己设置  
[root@server conf]# vim svnserve.conf 
注:配置如下:
anon-access = none            #匿名访问的权限,可以是read,write,none,默认为read
auth-access = write           #使授权用户有写权限
password-db = passwd          #密码数据库的路径 
authz-db = authz              #访问控制文件 
realm = /opt/svn/repo         #认证命名空间,subversion会在认证提示里显示,并且作为凭证缓存                               #的关键字
[root@server conf]# cd
[root@server ~]# svnserve -d -r /opt/svn/
-d 表示后台运行 
-r 指定根目录是 /opt/svn/
[root@server ~]# ps -ef | grep svn
root      21122      1  0 22:21 ?        00:00:00 svnserve -d -r /opt/svn/
root      21124  20613  0 22:21 pts/0    00:00:00 grep --color=auto svn
[root@server ~]# netstat -antlp | grep svn
tcp        0      0 0.0.0.0:3690            0.0.0.0:*               LISTEN      21122/svnserve



二、linux客户端使用介绍

1、将文件checkout到本地目录 

1
2
3
4
5
6
7
[root@server home]# cd
[root@server ~]# cd /home/
[root@server home]# ls
[root@server home]# svn checkout svn://127.0.0.1/repo                #简写:svn co 
Checked out revision 0.
[root@server home]# ls
repo

2、往版本库中添加新的文件 

1
2
3
4
5
6
7
[root@server home]# cd repo/
[root@server repo]# ls
[root@server repo]# touch test.txt
[root@server repo]# ls
test.txt
[root@server repo]# svn add test.txt     
A         test.txt

3、将改动的文件提交到版本库 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
svn commit -m “LogMessage“ [-N] [--no-unlock] PATH(如果选择了保持锁,就使用–no-unlock开关) 
[root@server repo]# svn commit -m "add testing" test.txt               #简写:svn ci 
Adding         test.txt
Transmitting file data .
Committed revision 1.
[root@server repo]# ls
test.txt
[root@server repo]# vim test.txt 
[root@server repo]# cat test.txt 
testing
testing
testing
testing
testing
testing
testing
testing
[root@server repo]# svn commit -m "add testing something" test.txt 
Sending        test.txt
Transmitting file data .
Committed revision 2.

4、删除文件 

svn delete path -m “delete test fle“ 

例如:第一步:svn delete svn://192.168.10.151/pro/domain/test.php -m “delete test file”

第二步:svn commit

第三步:svn update   

或者直接svn delete test.php 然后再svn ci -m ‘delete test file‘,推荐使用这种 

简写:svn (del, remove, rm) 

5、更新到某个版本 

svn update -r m path 

例如: 

svn update如果后面没有目录,默认将当前目录以及子目录下的所有文件都更新到最新版本。 

svn update -r 200 test.php(将版本库中的文件test.php还原到版本200) 

svn update test.php(更新,于版本库同步。如果在提交的时候提示过期的话,是因为冲突,需要先update,修改文件,然后清除svn resolved,最后再提交commit) 

简写:svn up 

6、恢复本地修改 

svn revert: 恢复原始未改变的工作副本文件 (恢复大部份的本地修改)。revert: 

用法: revert PATH… 

注意: 本子命令不会存取网络,并且会解除冲突的状况。但是它不会恢复 

被删除的目录 

7、解决冲突 

svn resolved: 移除工作副本的目录或文件的“冲突”状态。 

用法: resolved PATH… 

注意: 本子命令不会依语法来解决冲突或是移除冲突标记;它只是移除冲突的 

相关文件,然后让 PATH 可以再次提交。 


8、加锁/解锁 

svn lock -m “LockMessage“ [--force] PATH 

例如:svn lock -m “lock test file“ test.php 

svn unlock PATH 

9、查看文件或者目录状态 

1)svn status path(目录下的文件和子目录的状态,正常状态不显示) 

【?:不在svn的控制中;M:内容被修改;C:发生冲突;A:预定加入到版本库;K:被锁定】 

2)svn status -v path(显示文件和子目录状态) 

第一列保持相同,第二列显示工作版本号,第三和第四列显示最后一次修改的版本号和修改人。 

注:svn status、svn diff和 svn revert这三条命令在没有网络的情况下也可以执行的,原因是svn在本地的.svn中保留了本地版本的原始拷贝。 

简写:svn st 

10、查看日志 

svn log path 

例如:svn log test.php 显示这个文件的所有修改记录,及其版本号的变化 

11、查看文件详细信息 

svn info path 

例如:svn info test.php 

12、比较差异 

svn diff path(将修改的文件与基础版本比较) 

例如:svn diff test.php 

svn diff -r m:n path(对版本m和版本n比较差异) 

例如:svn diff -r 200:201 test.php 

简写:svn di 

13、将两个版本之间的差异合并到当前文件 

svn merge -r m:n path 

例如:svn merge -r 200:205 test.php(将版本200与205之间的差异合并到当前文件,但是一般都会产生冲突,需要处理一下) 

14、SVN 帮助 

svn help 

svn help ci 


三、自动更新项目文件到web发布目录(www)

1、检出(checkout)到本地目录是/home/repo

1
2
3
4
5
6
7
8
9
10
11
[root@server repo]# ls
index.php  test.txt
[root@server repo]# cd ..
[root@server home]# ls
repo
[root@server home]# mv repo/ www/     #将检出到本地的版本目录更名为web发布目录
[root@server home]# ls
www
[root@server home]# cd www/
[root@server www]# ls
index.php  test.txt

2、通过脚本文件实现自动更新

使用SVN中post-commit实现自动实时从svn中检出文件并同步到Web站点根目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@server ~]# cd /opt/svn/repo/
[root@server repo]# ls
conf  db  format  hooks  locks  README.txt
[root@server repo]# cd hooks/
[root@server hooks]# pwd
/opt/svn/repo/hooks
[root@server hooks]# ls
post-commit.tmpl          post-unlock.tmpl  pre-revprop-change.tmpl
post-lock.tmpl            pre-commit.tmpl   pre-unlock.tmpl
post-revprop-change.tmpl  pre-lock.tmpl     start-commit.tmpl
[root@server hooks]# cp post-commit.tmpl post-commit
[root@server hooks]# ls
post-commit       post-revprop-change.tmpl  pre-lock.tmpl            start-commit.tmpl
post-commit.tmpl  post-unlock.tmpl          pre-revprop-change.tmpl
post-lock.tmpl    pre-commit.tmpl           pre-unlock.tmpl
[root@server hooks]# cp post-commit.tmpl post-commit
[root@server hooks]# vim post-commit
[root@server hooks]# > post-commit
[root@server hooks]# vim post-commit

******************************************************************************************

1
2
3
4
5
6
7
8
输入:
#!/bin/sh
export LC_CTYPE="zh_CN.UTF-8"
SVN=/usr/bin/svn
WEB_PATH=/home/www #要强制更新的目录
SVN_USER=admin
SVN_PASS=123456
$SVN update $WEB_PATH --username $SVN_USER --password $SVN_PASS            #执行更新

******************************************************************************************

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@server hooks]# ll post-commit
-rw-r--r--. 1 root root 220 Apr 13 00:21 post-commit
[root@server hooks]# chmod a+x post-commit  #给予执行权限
[root@server hooks]# ll post-commit
-rwxr-xr-x. 1 root root 220 Apr 13 00:21 post-commit
[root@server hooks]# cat post-commit
#!/bin/sh
export LC_CTYPE="zh_CN.UTF-8"
SVN=/usr/bin/svn
WEB_PATH=/home/www #要强制更新的目录
SVN_USER=admin
SVN_PASS=123456
$SVN update $WEB_PATH --username $SVN_USER --password $SVN_PASS            #执行更新
[root@server hooks]# pwd
/opt/svn/repo/hooks

注:文件/opt/svn/repo/hooks(俗称钩子)/post-commit属于自动执行。





      本文转自MQ_douer 51CTO博客,原文链接:http://blog.51cto.com/douer/1915293,如需转载请自行联系原作者