【項目管理】Jenkins+Maven+Git項目持續構建之搭建git服務器

gitlab和github兩者都是基於web的Git repositories(倉庫),擁有流水線型的web開發流程,它們爲開發團隊存儲、分享、發佈、測試和合作web開發項目提供了中心化的、雲存儲的場所。Git版本控制——對於多人共同開發一個project來說非常重要——它提供了分享開源項目的平臺。但github是一個開源代碼交互溝通平臺,是2008年由Ruby on Rails編寫而成。GitHub同時提供付費賬戶和免費賬戶。這兩種賬戶都可以創建公開的代碼倉庫,但是付費賬戶纔可以創建私有的代碼倉庫。所以在github上想要將代碼設置爲私有是需要收費的。而且將代碼保存在別人的服務器上畢竟具有一定的風險性,尤其對於保密級別較高的代碼。所以搭建公司私有的代碼服務器是十分必要的。不過幸好,github的缺陷gitlab解決了。
下面開始具體介紹一下搭建Git服務器,主要分爲以下幾個部分:
- 1、安裝bitnami
- 2、修改gitlab默認端口號
- 3、生成密鑰,將公鑰添加到gitlab中
- 4、配置gitlab:創建用戶
- 5、配置gitlab:創建項目
- 6、gitlab啓動


1、安裝bitnami

git服務器比較有名的是gitosis和gitolite,這兩個管理和使用起來稍微有些複雜,沒有web頁面,而gitlab則是類似於github的一個工具,並且gitlab解決了github無法免費建立私有倉庫的問題。gitlab有很多依賴,一一安裝這些依賴不僅對服務器現有的環境造成干擾,而且非常不方便,而bitnami製作了一鍵安裝的包,下載地址爲:https://bitnami.com/redirect/to/37478/bitnami-gitlab-7.0.0-0-linux-installer.run ,會下載當前最新版本的bitnami,當前最新版本應該爲10.0.3,下面介紹一下bitnami的具體安裝。
這裏以bitnami的7.8.1版本爲例
1) 將下載後的bitnami拷貝到適當的位置,切換到對應的目錄下
2) 賦權限:

chmod +x  bitnami-gitlab-7.8.1-0-linux-x64-installer.run

3) 安裝.run文件

./ bitnami-gitlab-7.8.1-0-linux-x64-installer.run

這裏寫圖片描述

以下爲bitnami的安裝細節,供參考

[root@localhost ~]# ls
anaconda-ks.cfg  gitlab  learngit.git
[root@localhost ~]# cd gitlab
[root@localhost gitlab]# chmod +x bitnami-gitlab-7.8.1-0-linux-x64-installer.run
[root@localhost gitlab]# ./bitnami-gitlab-7.8.1-0-linux-x64-installer.run
The installer detects that exists a 'git' user in the system. This installer will change the configuration for this user. Do you want to continue the installation? [y/N]: y

The installer detects that exists a 'gitlab_ci' user in the system. This installer will change the configuration for this user. Do you want to continue the installation? [y/N]: y

----------------------------------------------------------------------------
Welcome to the Bitnami Gitlab Stack Setup Wizard.

----------------------------------------------------------------------------
Select the components you want to install; clear the components you do not want 
to install. Click Next when you are ready to continue.

GitLab : Y (Cannot be edited)

GitLab CI [Y/n] :y

Is the selection above correct? [Y/n]: y

----------------------------------------------------------------------------
Installation folder

Please, choose a folder to install Bitnami Gitlab Stack

Select a folder [/opt/gitlab-7.8.1-0]: 

----------------------------------------------------------------------------
Create Admin account

Bitnami Gitlab Stack admin user creation

Email Address [user@example.com]: 輸入Email地址

Login [user]: 輸入用戶名

Password : 輸入密碼
Please confirm your password : 確認密碼
----------------------------------------------------------------------------
Hostname that will be used to create internal URLs. If this value is incorrect, 
you may be unable to access your Gitlab installation from other computers. It is 
advisable to use a Domain instead of an IP address for compatibility with 
different browsers.

Domain [127.0.0.1]: IP地址

Do you want to configure mail support? [y/N]: y

----------------------------------------------------------------------------
Configure SMTP Settings

This is required so your application can send notifications via email.

Default email provider:

[1] GMail
[2] Custom
Please choose an option [1] : 2

----------------------------------------------------------------------------
Configure SMTP Settings

This data is stored in the application configuration files and may be visible to 
others. For this reason, it is recommended that you do not use your personal 
account credentials.

Username []: Email地址

Password :
Re-enter :
SMTP Host []: smtp.qq.com

SMTP Port []: 25

Secure connection

[1] None
[2] SSL
[3] TLS
Please choose an option [3] : 3

----------------------------------------------------------------------------
Setup is now ready to begin installing Bitnami Gitlab Stack on your computer.

Do you want to continue? [Y/n]: y

----------------------------------------------------------------------------
Please wait while Setup installs Bitnami Gitlab Stack on your computer.

 Installing
 0% ______________ 50% ______________ 100%
 ########################################
----------------------------------------------------------------------------
Setup has finished installing Bitnami Gitlab Stack on your computer.

Info: To access the Bitnami Gitlab Stack, go to
http://IP地址:80 from your browser.
Press [Enter] to continue:

2、修改gitlab默認端口號

gitlab默認的端口號爲80,如果該端口被佔用,可以修改端口號,具體操作如下:
1) 找到gitlab的安裝目錄,一般在opt文件下,我的在../**/gitlab/gitlab-7.8.1-0
2) 修改../**/gitlab/gitlab-7.8.1-0/apache2/conf/httpd.conf,修改Listen 80爲新的端口號,例如Listen 9001
3) 修改../**/gitlab/gitlab-7.8.1-0/apache2/conf/bitnami/bitnami.conf,修改端口號爲新端口號
4) 修改../**/gitlab/gitlab-7.8.1-0/apps/gitlab/gitlab-shell/config.yml下的端口號
5) 修改../**/gitlab/gitlab-7.8.1-0/apps/gitlab/htdocs/config/gitlab.yml下的端口號

3、生成密鑰,將公鑰添加到gitlab中

在終端輸入ssh-keygen -t rsa -C “Email地址”,生成的密鑰保存在/root/.ssh下,將密鑰添加到gitlab的ssh key中。
1) 進入到ssh文件夾中:cd /root/.ssh
2) 將公鑰保存在gitlab中,在gitlab面板中依次點擊ProfileSSH KeysAdd SSH Keys,然後把.ssh文件下的id_rsa.pub公鑰的內容粘貼到輸入框中保存起來。

注:git倉庫之間的代碼傳輸協議主要使用ssh協議,而一般搭建gitlab時使用的git用戶是沒有密碼的,因此直接ssh是不能登錄的,就需要使用ssh-keygen上傳公鑰,使用非對稱加密傳輸。

4、配置gitlab:創建用戶

1)點擊Admin area,選擇Users
這裏寫圖片描述

2)輸入相應的用戶名、密碼、郵箱等信息
這裏寫圖片描述

3)創建用戶成功後,添加git公鑰到Gitlab,點擊Profile settings,選擇SSH Keys
這裏寫圖片描述

4)在Key欄中輸入生成的公鑰,以獲得用戶的使用權限
這裏寫圖片描述

5、配置gitlab:創建項目

1)創建項目,點擊右上側的New project,輸入項目的相關信息
這裏寫圖片描述

2)輸入相應的項目名稱及權限
這裏寫圖片描述
項目Namespance及權限的簡單介紹:
【1】Namespace:這個選擇是用來決定這個工程所屬的,可以選User爲你自己。或者選擇組,這個會影響到後面工程的url。
【2】Visibility Level:權限等級分三種
 Private:私有的,只有你自己或者組內的成員能訪問
 Internal:所有登錄的用戶
 Public:公開的,所有人都可以訪問
項目創建成功後,會生成對應的url,根據url就可以進行代碼的上傳和下載了。

3)創建完成後,根據頁面提示,可上傳項目代碼
這裏寫圖片描述

6、gitlab啓動

gitlab安裝目錄下,有一個ctlscript.sh文件,啓動該文件即可完成gitlab的啓動,具體操作爲:
切換到root用戶下,輸入命令gitlab-7.8.1-0/ctlscript.sh start命令,則gitlab啓動
這裏寫圖片描述

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