1. 程式人生 > >git與github——7,GitLab的安裝及使用教程

git與github——7,GitLab的安裝及使用教程

在生產環境的部署,yum源的配置要相對麻煩一些,可以採用npm 來安裝

一、安裝gitlab

1、配置yum源

vim /etc/yum.repos.d/gitlab-ce.repo

複製以下內容:

 

[gitlab-ce]

name=Gitlab CE Repository

baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/

gpgcheck=0

enabled=1

 

2、更新本地yum快取

sudo yum makecache

GitLab的安裝及使用教程

3、安裝GitLab社群版

sudo yum install gitlab-ce        #自動安裝最新版
sudo yum install gitlab-ce-x.x.x    #安裝指定版本

GitLab的安裝及使用教程

GitLab常用命令

注意,gitlab-ctl start 不是啟動gitlab服務,只是啟動所有的元件

gitlab-ctl reconfigure 的時間有點久,要等一下,等到reconfigure完之後就可以開啟gitlab

sudo gitlab-ctl start    # 啟動所有 gitlab 元件;
sudo gitlab-ctl stop        # 停止所有 gitlab 元件;
sudo gitlab-ctl restart        # 重啟所有 gitlab 元件;
sudo gitlab-ctl status        # 檢視服務狀態;
sudo gitlab-ctl reconfigure        # 啟動服務;
sudo vim /etc/gitlab/gitlab.rb        # 修改預設的配置檔案;
gitlab-rake gitlab:check SANITIZE=true --trace    # 檢查gitlab;
sudo gitlab-ctl tail        # 檢視日誌;

GitLab使用

二、登入GitLab、修改root密碼

1、在瀏覽器的位址列中輸入ECS伺服器的公網IP即可登入GitLab的介面,第一次登入使用的使用者名稱和密碼為 root 和 5iveL!fe。

我這裡遇到一個問題,他報了502錯誤。可能是虛擬機器資源不夠,另外訪問不用加埠,也就是80埠就可以訪問。應該是可以設定埠。

在生產環境中發現,密碼不對!果然有些教程還是有水分的,然後用了一套修改密碼的方法:

獲得使用者資料,修改使用者密碼
[[email protected] bin]# gitlab-rails console production
Loading production environment (Rails 4.2.5.2)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id: 1, email: "

[email protected]", ...
irb(main):002:0> user.password=12345678
=> 12345678
irb(main):003:0> user.password_confirmation=12345678
=> 12345678
irb(main):004:0> user.save!
=> true
irb(main):005:0> quit
 

這樣就可以將root密碼改為12345678

GitLab的安裝及使用教程

2、首次登入會強制使用者修改密碼。密碼修改成功後,輸入新密碼進行登入。

 

三、設定gitlab的ip及埠

建立了一個test倉庫後,看看連結:

並不是本機ip,這裡需要修改配置檔案:

vim /etc/gitlab/gitlab.rb

將這一段的ip改成伺服器的就行了,192.168.0.1:8888

另外,企業的80埠一般是比較重要的,不會專門給到gitlab 80埠,因此我們還要修改gitlab的埠設定:

同樣這個檔案,找到linsten_port這一行:

修改埠:

之後重新reconfigure,這裡要注意一點,就是gitlab reconfigure後需要等一段時間再訪問,一分鐘大概就可以。

針對防火牆(iptables),這麼設定就可以了:

        vim /etc/sysconfig/iptables
        -A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT
        service iptables restart

 

四、漢化

Gitlab預設語言是英文,對於想加強英文的同學,建議繼續使用英文,但要求使用中文,這裡需要下載一個漢化包
下載最新的漢化包:

[[email protected] ~]# git clone https://gitlab.com/xhang/gitlab.git

如果要下載指定版本的漢化包,需要加上版本號。
例:下載10.5.1,命令如下:

[[email protected] ~]# git clone https://gitlab.com/xhang/gitlab.git  -b  v10.5.1-zh

下載完成後,將下載的資料夾內容複製到gitlab目錄下
複製前先停止Gitlab

[[email protected] ~]# gitlab-ctl stop
ok: down: gitaly: 0s, normally up
ok: down: gitlab-monitor: 0s, normally up
ok: down: gitlab-workhorse: 1s, normally up
ok: down: logrotate: 0s, normally up
ok: down: nginx: 1s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 0s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 1s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 1s, normally up

[[email protected] ~]# cp -r -f  ./gitlab/*  /opt/gitlab/embedded/service/gitlab-rails/ 

複製時可能不斷提示是否要覆蓋,這時可能是系統每次執行cp命令時,其實是執行了cp -i命令的別名。出現這種情況可以修改~/.bashrc,在“alias cp=’cp -i’”前加#註釋,再重新整理檔案 source ~/.bashrc

在複製過程中提示,以下資訊是正常的
Gitlab 10.5版本的部署使用及漢化

複製完成後,需要重新載入配置,並啟動Gitlab

[[email protected] ~]# gitlab-ctl reconfigure      
...
... ...
... ... ...
... ... ... ...
...
Running handlers:
Running handlers complete
Chef Client finished, 2/516 resources updated in 09 seconds
gitlab Reconfigured!

[[email protected] ~]# gitlab-ctl restart
ok: run: gitaly: (pid 42828) 0s
ok: run: gitlab-monitor: (pid 42852) 0s
ok: run: gitlab-workhorse: (pid 42858) 0s
ok: run: logrotate: (pid 42867) 0s
ok: run: nginx: (pid 42874) 0s
ok: run: node-exporter: (pid 42879) 0s
ok: run: postgres-exporter: (pid 42884) 1s
ok: run: postgresql: (pid 42894) 0s
ok: run: prometheus: (pid 42897) 0s
ok: run: redis: (pid 42907) 0s
ok: run: redis-exporter: (pid 42912) 1s
ok: run: sidekiq: (pid 42997) 0s
ok: run: unicorn: (pid 43007) 0s

[[email protected] ~]# lsof -i:80
COMMAND   PID       USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   42874       root    7u  IPv4 125185      0t0  TCP *:http (LISTEN)
nginx   42875 gitlab-www    7u  IPv4 125185      0t0  TCP *:http (LISTEN)

[[email protected] web-demo]# lsof -i:8080
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bundle  40358  git   18u  IPv4 119105      0t0  TCP localhost:webcache (LISTEN)
bundle  40650  git   18u  IPv4 119105      0t0  TCP localhost:webcache (LISTEN)
bundle  40652  git   18u  IPv4 119105      0t0  TCP localhost:webcache (LISTEN)

再次訪問web介面,顯示的已經為漢化熟悉的中文

 

 

五、建立Project

1、安裝Git工具linux:安裝Git,使用自帶的源安裝。

yum install git

GitLab的安裝及使用教程

2、生成金鑰檔案:使用ssh-keygen生成金鑰檔案.ssh/id_rsa.pub。

GitLab的安裝及使用教程

GitLab的安裝及使用教程

3.在GitLab的主頁中新建一個Project

GitLab的安裝及使用教程

GitLab的安裝及使用教程

4.新增ssh key匯入步驟2中生成的金鑰檔案內容:

GitLab的安裝及使用教程

GitLab的安裝及使用教程

ssh key新增完成:

GitLab的安裝及使用教程

專案地址,該地址在進行clone操作時需要用到:

GitLab的安裝及使用教程

簡單配置

1、配置使用Git倉庫的人員姓名(以上海駐云為例)

git config --global user.name "上海駐雲" 

2、配置使用Git倉庫的人員email,填寫自己的公司郵箱

git config --global user.email "[email protected]" 

3、克隆專案,在本地生成同名目錄,並且目錄中會有所有的專案檔案

git clone [email protected]:root/test.git

GitLab的安裝及使用教程

上傳檔案

1、進入到專案目錄

cd test/ 

2、建立需要上傳到GitLab中的目標檔案

echo “test” >  /root/test.sh

3、將目標檔案或者目錄拷貝到專案目錄下

cp /root/test.sh ./  

GitLab的安裝及使用教程

4、將test.sh檔案加入到索引中

git add test.sh 

5、將test.sh提交到本地倉庫

git commit -m “test.sh”

6、將檔案同步到GitLab伺服器上

git push -u origin master 

GitLab的安裝及使用教程

7、在網頁中檢視上傳的test.sh檔案已經同步到GitLab中

GitLab的安裝及使用教程

 

六、配置gitlab郵件服務

什麼情況下需要配置郵箱服務?

    有合併請求時,郵件通知
    賬號註冊時,郵件驗證
    修改密碼時,通過郵件修改

ok,接下來試試自己設定一個gitlab郵件服務

1.開啟QQ郵箱的smtp服務

不建議使用163郵箱,發幾次之後,就不能傳送
        過程概述:設定--》賬戶--》smtp--》密保驗證--》驗證成功返回一串字串,形狀如(ausdixersybgcgid)
        儲存返回的字串

在qq郵箱的首頁找到設定:

之後在設定中找到賬戶

在賬戶中找到smtp部分,開啟相關服務

設定成功後,會有一串字元形狀如(ausdixersybgcgid)


2.修改gitlab配置

        vim /etc/gitlab/gitlab.rb
        按/後輸入smtp_enable,找到下面這一串文字,進行修改:

改成:

改的具體內容參照如下:
        gitlab_rails['smtp_enable'] = true
        gitlab_rails['smtp_address'] = "smtp.qq.com"
        gitlab_rails['smtp_port'] = 465
        gitlab_rails['smtp_user_name'] = "[email protected]"
        gitlab_rails['smtp_password'] = "開通smtp時返回的字元"
        gitlab_rails['smtp_domain'] = "qq.com"
        gitlab_rails['smtp_authentication'] = "login"
        gitlab_rails['smtp_enable_starttls_auto'] = true
        gitlab_rails['smtp_tls'] = true

 

接下來修改成自己的qq郵箱:

        user['git_user_email'] = "[email protected]"

gitlab_email_from同樣的處理:
        gitlab_rails['gitlab_email_from'] = '[email protected]'
        按esc退出到命令列模式
        之後:wq 儲存並退出

        gitlab-ctl reconfigure


    3.測試郵件服務是否正常

        gitlab-rails console
        Notify.test_email('接收方郵件地址','郵件標題','郵件內容').deliver_now
        按回車,測試傳送。

七、gitlab群組配置

1,設定不允許註冊

gitlab一開始的註冊,只要註冊了就可以成功。

因此需要進行設定,進入root賬號:

找到settings:

在sign up setting中進行設定:

這個功能就可以設定email認證或者是否能夠進行註冊了,我直接取消了註冊。

如果選擇 send confirmaition email 則會發送一個確認資訊給郵件

2,設定group

點選groups

建立一個組(new group):

輸入組的名字:test1 

注意一個可視程度的設定:

private就是組內可見的意思

internal,登入可見

public,不登入就可見,當然企業一般不會用這個

確認即可

另外在member裡可以設定group有哪些成員

注意組員身份有這幾種:

Guest是遊客身份,只能發表評論之類,不能讀寫我們的專案庫

Reporter可以克隆專案,但是不能做專案的提交。

下面的許可權依次增大。owner可以刪除專案

3,建立專案

new 一個project就可以,和github很類似。

注意一點,project也有private許可權。

至於設定ssh和github方法是一樣的。

 

八、gitlab分支管理

其實也是和github一致的,一般開發有主分支(master)、測試分支(test)和開發分支(dev)三條分支。

一般是要保護master分支準確無誤。也就是dev分支先通過測試分支通過後合併到master分支。

可以在project setting 中找到 保護分支:

 

 

參考文章: https://yq.aliyun.com/articles/74395