代碼管理平臺SVN部署實戰

一、SVN 介紹

Subversion(SVN) 是一個開源的版本控制系統, 管理着隨時間改變的數據。 這些數據放置在一箇中央資料檔案庫(repository) 中。 這個檔案庫很像一個普通的文件服務器, 不過它會記住每一次文件的變動。 這樣你就可以把檔案恢復到舊的版本, 或是瀏覽文件的變動歷史。

二、SVN關鍵概念

repository(源代碼庫):源代碼統一存放的地方

Checkout(提取):當你手上沒有源代碼的時候,你需要從repository checkout一份

Commit(提交):當你已經修改了代碼,你就需要Commit到repository

Update (更新):當你已經Checkout了一份源代碼, Update一下你就可以和Repository上的源代碼同步,你手上的代碼就會有最新的變更

實際工作開發場景:

日常開發過程其實就是這樣的(假設你已經Checkout並且已經工作了幾天):Update(獲得最新的代碼) -->作出自己的修改並調試成功 --> Commit(大家就可以看到你的修改了) 。

如果兩個程序員同時修改了同一個文件呢, SVN可以合併這兩個程序員的改動,實際上SVN管理源代碼是以行爲單位的,就是說兩個程序員只要不是修改了同一行程序,SVN都會自動合併兩種修改。如果是同一行,SVN會提示文件Confict, 衝突,需要手動確認。

三、SVN 的主要功能

  1. 目錄版本控制
    CVS 只能跟蹤單個文件的歷史, 不過 Subversion 實作了一個 "虛擬" 的版本控管文件系統, 能夠依時間跟蹤整個目錄的變動。 目錄和文件都能進行版本控制。

  2. 真實的版本歷史
    自從CVS限制了文件的版本記錄,CVS並不支持那些可能發生在文件上,但會影響所在目錄內容的操作,如同複製和重命名。除此之外,在CVS裏你不能用擁有同樣名字但是沒有繼承老版本歷史或者根本沒有關係的文件替換一個已經納入系統的文件。在Subversion中,你可以增加(add)、刪除(delete)、複製(copy)和重命名(rename),無論是文件還是目錄。所有的新加的文件都從一個新的、乾淨的版本開始。

  3. 自動提交
    一個提交動作,不是全部更新到了檔案庫中,就是完全不更新。這允許開發人員以邏輯區間建立並提交變動,以防止當部分提交成功時出現的問題。

  4. 納入版本控管的元數據
    每一個文件與目錄都附有一組屬性關鍵字並和屬性值相關聯。你可以創建, 並儲存任何你想要的Key/Value對。 屬性是隨着時間來作版本控管的,就像文件內容一樣。

  5. 選擇不同的網絡層
    Subversion 有抽象的檔案庫存取概念, 可以讓人很容易地實作新的網絡機制。 Subversion 可以作爲一個擴展模塊嵌入到Apache HTTP 服務器中。這個爲Subversion提供了非常先進的穩定性和協同工作能力,除此之外還提供了許多重要功能: 舉例來說, 有身份認證, 授權, 在線壓縮, 以及文件庫瀏覽等等。還有一個輕量級的獨立Subversion服務器, 使用的是自定義的通信協議, 可以很容易地通過 ssh 以 tunnel 方式使用。

  6. 一致的數據處理方式
    Subversion 使用二進制差異算法來異表示文件的差異, 它對文字(人類可理解的)與二進制文件(人類無法理解的) 兩類的文件都一視同仁。 這兩類的文件都同樣地以壓縮形式儲存在檔案庫中, 而且文件差異是以兩個方向在網絡上傳輸的。

  7. 有效的分支(branch)與標籤(tag)
    在分支與標籤上的消耗並不必一定要與項目大小成正比。 Subversion 建立分支與標籤的方法, 就只是複製該項目, 使用的方法就類似於硬連接(hard-link)。 所以這些操作只會花費很小, 而且是固定的時間。

  8. Hackability
    Subversion沒有任何的歷史包袱; 它主要是一羣共用的 C 程序庫, 具有定義完善的API。這使得 Subversion 便於維護, 並且可被其它應用程序與程序語言使用。

四、SVN安裝部署

1.安裝svn

yum install -y subversion

2.創建svn版本庫目錄,注意創建的這個目錄是個空目錄

[root@localhost ~]# mkdir -p /data/svnroot/myproject
[root@localhost ~]# ls  /data/svnroot/myproject/
[root@localhost ~]# 

3.初始化創建的目錄

[root@localhost ~]# svnadmin create /data/svnroot/myproject/
[root@localhost ~]# ls !$
ls /data/svnroot/myproject/
conf  db  format  hooks  locks  README.txt
[root@localhost ~]# cd !$/conf
cd /data/svnroot/myproject//conf
[root@localhost conf]# ls
authz  passwd  svnserve.conf

說明:

authz:是權限配置文件
passwd:用戶密碼文件
svnserve.conf:svn代碼管理主配置文件

編輯authz配置文件

##
### A match can be inverted by prefixing the rule with '~'. Rules can
### grant read ('r') access, read-write ('rw') access, or no access
### ('').

[aliases]
# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average

[groups]    ##這個模塊是定義對代碼倉庫操作權限的用戶組,不同的用戶具有不同的操作權限
# harry_and_sally = harry,sally
# harry_sally_and_joe = harry,sally,&joe

# [/foo/bar]
# harry = rw
# &joe = r
# * =
admins = liu,user1   ##定義admins這個用戶組,組內有兩個用戶分爲別liu,user1
[/]    ##中括號裏面“/”表示 /data/svnroot/myproject/目錄
@admins =rw  ##admins這個用戶組對代碼項目目錄具有讀寫權限
* = r     ##其他用戶組只具備讀權限

[myproject:/]   ##這裏用來定義/data/svnroot/目錄下有多個項目代碼的時候
user1 = rw

編輯 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
liu = liu_!(!@#
user1 = user1!@#
user2 = user2!@#

編輯svnserve.conf ,在[general]增加如下配置


[general]
anon-access = none    ##匿名用戶沒有任何權限
auth-access = write  ##被授權用戶具有寫權限
password-db = passwd  ##授權用戶的密碼存放位置
authz-db = authz  ##指定權限控制文件
realm = /data/svnroot/myproject  ##定義對哪個項目目錄生效

4.啓動svn服務

[root@server-1 conf]# svnserve -d -r /data/svnroot/
[root@server-1 conf]# ps aux |grep svnserver
root     18670  0.0  0.0 112660   976 pts/0    S+   14:54   0:00 grep --color=auto svnserver
[root@server-1 conf]# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3690            0.0.0.0:*               LISTEN      18592/svnserve      

五、如何配置SVN客戶端(Linux)

1.安裝SVN客戶端軟件

[root@client ~]# yum install -y subversion

2.SVN客戶端檢出操作和提交操作

[root@client ~]# mkdir svntest
[root@client ~]# cd svntest/
[root@client svntest]#  svn checkout svn://10.71.11.1/myproject --username=user2   ##注意,這條命令實現從svn服務端10.71.11.1上檢出項目程序代碼,使用user2這個用戶和密碼
Authentication realm: <svn://10.71.11.1:3690> /data/svnroot/myproject
Password for 'user2': 

-----------------------------------------------------------------------
ATTENTION!  Your password for authentication realm:

   <svn://10.71.11.1:3690> /data/svnroot/myproject

can only be stored to disk unencrypted!  You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible.  See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? yes
Checked out revision 0.
[root@client svntest]# ls   ##成功將遠端服務上的項目程序代碼拉到本地,就可以修改代碼
myproject

那麼如何把本地修改的代碼上牀到SVN服務端呢?

常見錯誤01:

[root@client ~]# cd svntest/
[root@client svntest]# ls
myproject
[root@client svntest]# touch svntest01
[root@client svntest]# ls
myproject  svntest01
[root@client svntest]# svn add svntest01 
svn: E155007: '/root/svntest' is not a working copy

原因分析:因爲從SVN服務端檢出的目錄是/myprojec,因此必須進入到這個目錄下向SVN服務端提交本地修改的代碼

常見錯誤02

[root@client svntest]# cd myproject/
[root@client myproject]# ls
[root@client myproject]# touch test01
[root@client myproject]# ls
test01
[root@client myproject]# svn add test01 
A         test01
[root@client myproject]# svn commit -m "add test01"
svn: E170001: Commit failed (details follow):
svn: E170001: Authorization failed

查看工作副本中的狀態

[root@client myproject]# svn status
A       fstab
A       test01

此時fstab的狀態爲A,它意味着這個文件已經被成功地添加到了版本控制中。

爲了把 fstab存儲到版本庫中,使用 commit -m 加上註釋信息來提交。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章