前景提要:最近正在學習如何在Linux系統搭建SVN,在網上也找到了很多相關教程,但是我發現很多教程都是隻講了一半的,什麼意思呢?我們一般搭建SVN是爲了做版本控制的,而網上的部分教程只講瞭如何搭建svn倉庫,卻沒有說如何自動部署代碼到項目。爲此我在這做個整合。
1.安裝svn
yum -y install subversion
1.1安裝成功:
1.2查看版本號,確認是否正確安裝
svnserve --version
2.搭建產品倉庫
2.1 建立svn根目錄(可自定義)
mkdir /svn
2.2建立產品倉庫
mkdir -p /svn/test
svnadmin create /svn/test
2.3修改配置文件
vim /svn/test/conf/svnserve.conf
主要修改的地方:
anon-access = none //默認是隻讀read
auth-access = write //認證後有寫入權限
password-db = passwd //帳號密碼配置文件
authz-db = authz //權限配置文件
realm = test //改成自己的版本庫 生效範圍
修改後文件內容如下:
### This file controls the configuration of the svnserve daemon, if you
### use it to allow access to this repository. (If you only allow
### access through http: and/or file: URLs, then this file is
### irrelevant.)
### Visit http://subversion.tigris.org/ for more information.
[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 directory containing
### this configuration file.
### If SASL is enabled (see below), this file will NOT be used.
### 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 the
### directory containing this file. 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 = test
[sasl]
### This option specifies whether you want to use the Cyrus SASL
### library for authentication. Default is false.
### This section will be ignored if svnserve is not built with Cyrus
### SASL support; to check, run 'svnserve --version' and look for a line
### reading 'Cyrus SASL authentication is available.'
# use-sasl = true
### These options specify the desired strength of the security layer
### that you want SASL to provide. 0 means no encryption, 1 means
### integrity-checking only, values larger than 1 are correlated
### to the effective key length for encryption (e.g. 128 means 128-bit
### encryption). The values below are the defaults.
# min-encryption = 0
# max-encryption = 256
2.4設置passwd用戶賬號信息
vim /svn/test/conf/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.
### 在下面添加用戶和密碼,每行一組username = password
[users]
# harry = harryssecret
# sally = sallyssecret
test = password #在此設置用戶名和密碼
只用在文檔後面加一句話即可,test代表你要設的用戶名,password代表你要設置的密碼,此處設置是爲了在本地連接之用
2.5設置authz,設置用戶訪問權限
vim /svn/test/conf/authz
在文件末尾添加以下代碼即可:
devteam = test //創建一個devteam的組,並制定一個用戶test
[test:/] //制定test目錄的權限
@devteam = rw //項目組的成員對test都有讀寫權限。
2.6啓動svn
svnserve -d -r /svn
默認的啓動端口號爲3690;
-d : 表示以daemon方式(後臺運行)運行;
-r /svn : 指定根目錄是/svn;
2.6.1檢查是否啓動
netstat -tunlp | grep svn
如果顯示以下信息說明啓動成功
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 10973/svnserve
最後就可以通過客戶端(小烏龜)進行訪問 svn://{your-server-ip}:/test/ 根據提示輸入賬號密碼即可
以上基本實現了對svn的搭建,接下來將講解如何自動部署代碼到項目
如果只是完成了上面的搭建,你可能會發現從本地上傳的代碼在服務器是無法找到的。這是因爲svn 存儲的是二進制的文件,所以想要實現版本控制還要多部署一步。
3.1配置post-commit文件
進入到hooks目錄下:
cd /svn/test/hooks
執行以下代碼:
cp post-commit.tmpl post-commit
修改 post-commit 文件
vim post-commit
將post-commit裏面的全部內容清除,替換成以下代碼:
#!/bin/sh
export LANG=zh_CN.UTF-8
/usr/bin/svn update --username test --password 123456 /var/www/test
其中第一行意思是:用sh來解析這個腳本,因爲各種shell的語法有細微的差別
第二行是編碼格式:這裏我使用的是UTF-8
最後一行/usr/bin/svn 是svn的路徑,不是項目路徑,應該都是一樣的,後半句分別是用戶名、密碼、和項目路徑。
test 代表用戶名,123456則表示密碼 ,也就是我們在前面配置的賬號和密碼。而後面的/var/www/test則是你自己項目的所在位置。本地svn上傳的文件將會上傳到此目錄。
修改post-commit的可執行權限
chmod 755 post-commit
最後進入到你的項目目錄,本例是/var/www/test
cd /var/www/test
接下來checkout整個項目
svn checkout svn://{您的服務器地址}:{端口號}/test . //注意目錄後面還有一個點
端口號一般爲:3690
至此,Linux系統搭建svn全部完成。
參考鏈接:
1.http://www.cnblogs.com/mitang/p/4309762.html
2.https://bbs.aliyun.com/read/255009.html?pos=18