天天看點

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

版權: 淩雲物網智科實驗室< www.iot-yun.com >

聲明: 本文檔由淩雲物網智科實驗室郭工編著!

作者: 郭文學< QQ: 281143292 [email protected]>

版本: v1.0.0

一. gitblit介紹

作為一個合格的程式員,版本控制系統的重要性不言而喻。而作為程式員界的“扛把子”,Linus Torvalds 大神因忍受不了BitKeeper的諸多限制,為友善管理 Linux 核心源碼而設計了更為靈活的版本控制系統git。起初 Git 的開發也隻是為了輔助 Linux 核心開發,但很快越來越多的自由軟體項目和公司在軟體開發中都使用git了。本人從業十多年來,使用的版本控制系統也曆經了CVS->SVN->Git的變遷,最近幾年自己負責的所有項目源碼也都改成git托管了。

現在有許多免費的代碼托管網站,如國外的github、國内的gitee等。其中github因國内通路慢的“尿性”而廣受诟病,另外在這些網站上免費托管代碼必須開源,而私有git倉庫則需要收費。這樣,如果有一些商業項目代碼再在上面托管就不合時宜了,自己搭建一個git伺服器才是王道。

在選擇git伺服器時,備受推崇的莫過于著名的gitlab了,它是一個利用Ruby on Rails開發的開源應用程式,實作一個自托管的Git項目倉庫,可通過Web界面進行通路公開的或者私人項目。最開始也嘗試在自己的伺服器上使用gitlab搭建git伺服器,但Ruby畢竟是腳本語言,CPU和記憶體那是消耗得杠杠的,性能不行的伺服器實在經不起折騰!

後來曆經千山萬水,終于找到了今天的主角--gitblit,一個java實作的輕量級的git伺服器。gitblit的安裝和使用都非常簡單,當然最重要的一點是是它的速度要比gitlab快、快、快很多,作為一個小團隊的項目管理綽綽有餘。另外,我最喜歡他的另外一個特性--federation功能,該功能可以直接同步另外一台主機上的gitblit倉庫,這樣不同伺服器主機之間git倉庫可以鏡像備份了。

本文将詳細講解ubuntu Linux下gitblit的安裝和使用,測試環境為騰訊雲 Ubuntu-18.04 Linux伺服器。gitblit跨平台支援Windows和Linux,最新版本下載下傳請到官方站點:

http://gitblit.github.io/gitblit/

二. gitblit安裝

2.1 安裝jdk環境

因為gitblit是使用Java開發的,是以Java開發環境必須搭建好:

[email protected]:~$ sudo apt-get updateguowenxue@VM-0-12-ubuntu:~$ sudo apt-get install default-jre default-jdk
           

2.2 下載下傳安裝gitblit

從gitblit官網上下載下傳最新的Linux發行版本,目前最新的為 gitblit-1.9.1,解壓安裝到/opt路徑下:

[email protected]:~$ sudo tar -xzf gitblit-1.9.1.tar.gz -C /opt/[email protected]:~$ cd /opt/[email protected]:/opt$ sudo mv gitblit-1.9.1/ [email protected]:/opt$ cd gitblit/[email protected]:/opt/gitblit$ lsadd-indexed-branch.sh  docs         gitblit.sh                 install-service-fedora.sh  LICENSE             reindex-tickets.shauthority.sh           ext          gitblit-stop.sh            install-service-ubuntu.sh  migrate-tickets.sh  service-centos.shdata                   gitblit.jar  install-service-centos.sh  java-proxy-config.sh       NOTICE              service-ubuntu.sh
           

2.3 建立gitblit使用者并更改所有者

[email protected]:/opt/gitblit$ sudo useradd [email protected]:/opt/gitblit$ sudo chown -R gitblit:gitblit /opt/gitblit
           

2.4 修改配置服務端口

[email protected]:/opt/gitblit$ sudo vim data/defaults.propertiesgit.sshPort = 8882            修改SSH端口 8882server.httpPort = 8888        修改HTTP端口8888server.httpsPort = 0          禁用HTTPs端口git.packedGitLimit = 200m     單個檔案大小限制為200M
           

2.5 安裝并啟動服務

安裝相關服務:

guowenxu[email protected]:/opt/gitblit$ sudo ./install-service-ubuntu.sh
           

啟動gitblit服務:

guow[email protected]:/opt/gitblit$ sudo service gitblit restart
           

啟動需要一些時間,可以使用netstat指令檢視gitblit是否正常運作并監聽相應端口:

guowen[email protected]:/opt/gitblit$ sudo netstat -tlnp | grep java
           
centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

如果服務啟動異常,可以檢視gitblit服務的日志檔案:

[email protected]:/opt/gitblit$ sudo tail  /opt/gitblit/data/logs/gitblit.log
           

三. gitblit使用

3.1 web背景登入

Web浏覽器上輸入自己Ubuntu主機公網IP位址和相應端口即可通路gitblit服務背景, 如: http://122.51.234.174:8888/ 預設管理者使用者名: admin 密碼: admin

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

3.2 帳号管理

出于安全性考慮,使用管理者帳号登入後,我們應該到使用者中心建立新的管理者帳号,并删除預設的admin帳号。

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

在使用者管理頁面我們可以建立新的管理者帳号,管理者帳号也可以設定是否允許建立倉庫:

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

當然我們也可以在這裡給團隊内其他人建立普通帳号

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

出于安全考慮,删除預設帳号admin:

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

3.3 倉庫管理

gitblit上建立倉庫也很簡單,在“版本庫”菜單下點選“建立版本庫”就開始建立項目倉庫:

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

如下圖所示,我們建立了一個IGKv1的項目,這是淩雲實驗室打算出的一款ARM Linux物聯網網關開發闆,我們設定所有人都可以通路、克隆該倉庫源碼,但隻有授權的使用者才能送出代碼。

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

倉庫建立好後,點選進入該項目我們如下圖可以擷取倉庫的克隆下載下傳位址:

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

gitblit上的相關倉庫檔案都将存放到 gitblit 安裝路徑的 data/git下,一個倉庫對應一個檔案夾,如下圖所示的 IGKv1.git。

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

建立好項目後,我們可以點選進入到該項目下,點選右側的“編輯”按鈕來管理該項目權限:

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

在“permisssions”菜單中,我們可以将負責該項目開發的帳号添加進去,這樣這些賬戶就能夠送出代碼了。

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

如果不想要這個倉庫,我們也可以在該頁面中删除:

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

四. gitblit federation

gitblit對我來說還有一個比較實用的功能就是federation,該機制可以在不同的伺服器之間同步備份,這點對于源碼安全來說太重要了!譬如我的項目源碼會在 阿裡雲新加坡伺服器或騰訊雲伺服器 與 家裡的Linux伺服器之間同步,這樣一個伺服器奔潰也不會導緻資料丢失。

關于federation功能的說明可以參考官方 說明( http://gitblit.com/federation.html),下面是功能的描述和使用說明,就不翻譯了。

A Gitblit federation is a mechanism to clone repositories and keep them in sync from one Gitblit instance to another. Federation can be used to maintain a mirror of your Gitblit instance, to aggregate repositories from developer workstations, or to initially clone groups of repositories to developer workstations. If you are/were a Subversion user you might think of this as svn-sync, but better.

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

Origin Gitblit Instance Requirements:

git.enableGitServlet must be true, all Git clone and pull requests are handled through Gitblit's JGit servlet

federation.passphrase must be non-empty

The Gitblit origin instance must be http/https accessible by the pulling Gitblit instance.

That may require configuring port-forwarding on your router and/or opening ports on your firewall.

下面是在我家裡的Linux伺服器上定時自動同步騰訊雲伺服器上倉庫的詳細過程:

騰訊雲Ubuntu伺服器上gitblit federation操作:

首先在騰訊雲伺服器上配置gitblit使能 federation:

[email protected]:~$ cd /opt/gitblit/[email protected]:/opt/gitblit$ sudo vim data/defaults.propertiesgit.enableGitServlet = truefederation.passphrase = mypassword     設定同步的密碼
           

接下來關閉并重新開機gitblit伺服器,我們從伺服器的啟動日志中可以找到相應的Federation Token,這個Token一會同步時會用到:

[email protected]:/opt/gitblit$ sudo service gitblit [email protected]:/opt/gitblit$ sudo rm -f data/logs/[email protected]:/opt/gitblit$ sudo service gitblit startguowen[email protected]:/opt/gitblit$ sudo vim data/logs/gitblit.log
           
centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

家裡Ubuntu伺服器上gitblit同步操作:

[email protected]:~$ cd /opt/gitblitguowenxu[email protected]:/opt/gitblit$ sudo vim data/defaults.properties
           

... ...

federation.allowProposals = true 首先使能federation

... ...

#federation.example1.url = https://go.gitblit.com

#federation.example1.token = 6f3b8a24bf970f17289b234284c94f43eb42f0e4

#federation.example1.frequency = 120 mins

#federation.example1.folder =

#federation.example1.bare = true

#federation.example1.mirror = true

#federation.example1.mergeAccounts = true

# 找到上面這些行,在下面添加騰訊雲伺服器上的gitblit同步

# 下面這個配置将5分鐘同步一次騰訊雲伺服器 gitblit 下的所有倉庫

#federation.tencent.url = http://122.51.234.174:8888/

#federation.tencent.token = 472c159489febfb4fa15dcaadbeb5da15151496e

#federation.tencent.frequency = 5 mins

#federation.tencent.folder =

#federation.tencent.bare = true

#federation.tencent.mirror = true

#federation.tencent.mergeAccounts = true

# 下面這個配置将5分鐘同步一次騰訊雲伺服器 gitblit 下的IGKv1項目倉庫

federation.IGKv1.url = http://122.51.234.174:8888/

federation.IGKv1.token = 472c159489febfb4fa15dcaadbeb5da15151496e

federation.IGKv1.frequency = 5 mins

federation.IGKv1.folder =

federation.IGKv1.bare = true

federation.IGKv1.mirror = true

federation.IGKv1.exclude = *

federation.IGKv1.include = IGKv1.git

配置好後重新開機gitblit服務:

guo[email protected]:/opt/gitblit$ sudo service gitblit restart
           

5分鐘之後,我們可以在gitblit路徑下看到騰訊雲伺服器上的倉庫已經同步過來!

[email protected]:/opt/gitblit$ ls data/git/IGKv1.git/
           
centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

在浏覽器背景上通路家裡的gitblit伺服器,也可以看到相應的項目檔案了:

centos 6.8安裝git_ubuntu下輕量級跨平台git伺服器gitblit安裝和使用詳細過程一. gitblit介紹二. gitblit安裝三. gitblit使用四. gitblit federation

繼續閱讀