Gitlab的安裝部署和介紹


在devops工具鏈中有一個最重要代碼托管環節,代碼托管一般公司采用gitlab和svn結合的方式進行代碼托管。今天我們來介紹一下如何進行gitlab的安裝部署。

1.安裝環境建議

Gitlab 不建議用太新版本,可能會有bag還有漢化不太好。還有用虛擬機做實驗的時候內存不要低於2G,不然很多服務起不來報錯。

Gitlab推薦配置 8C 32G  磁盤IO 越高越好,最好物理機+SSD

 環境配置

1、OS:RHEL7.6
2、CPU:盡量使用四核以上
3、MEM:官方建議4GB以上

 這個對配置的要求確實高,如果太低的話,安裝完成后服務真的會跑不起來

2.解決依賴

GitLab 10.x版本后多了一些依賴,我們現在來解決一下環境的依賴,我們是以最小化服務器來進行環境的配置。

[root@centos7 ~]# yum install vim gcc gcc-c++ wget net-tools lrzsz iotop lsof iotop bash-completion -y
[root@centos7
~]# yum install curl policycoreutils openssh-server openssh-clients postfix -y #下面這標紅的是我們做實驗時為了圖方便關閉了防火牆,已防止防火牆問題給我們帶來的不便。
[root@centos7
~]# systemctl disable firewalld [root@centos7 ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/sysconfig/selinux #啟動ssh服務&設置為開機啟動
systemctl enable sshd && sudo systemctl start sshd
#設置postfix開機自啟,並啟動,postfix支持gitlab發信功能
systemctl enable postfix && systemctl start postfix
#上面關閉防火牆,在生產環境中的,是不建議的,所以在生產環境中,我們部署gitlab,需要
開放ssh以及http服務,然后重新加載防火牆列表
firewall-cmd --add-service=ssh --permanent
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
[root@centos7 ~]# hostnamectl set-hostname gitlab.example.com
[root@centos7 ~]# reboot

 

安裝包下載地址:https://packages.gitlab.com/gitlab/gitlab-ce

rpm 包國內下載地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/

ubuntu 國內下載地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/

由於我們這次的部署環境是centos的所以我們采用rpm包的方式進行部署。

 

 點進去之后選擇我們需要的rpm包進行安裝,直接下載,通過xftp傳到服務器上,也可以復制鏈接地址通過服務器直接下載,這里我們用第二個方法。

3.安裝過程

[root@gitlab ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm
[root@gitlab ~]# yum localinstall gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm             #用yum localinstall 安裝本地rpm包並解決依賴關系
#這里需要我們到配置文件中修改external_url,將url中的域名換成本機ip ,如果有需要的話,我們可以在此配置文件中,修改默認的80端口
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb
[root@gitlab ~]# grep "^[a-Z]" /etc/gitlab/gitlab.rb
external_url 'http://192.168.195.166'
#把端口添加到防火牆 ,我們這里是默認的80端口。
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

1.gitlab郵件配置

到這里基本的gitlab配置已經做完了,不過一般我們還需要做一項基本配置就是gitlab的郵件配置,可以讓gitlab向外發送通知郵件,比如創建賬號或者修改了什么配置都可以給管理源發送郵件。配置方法也是修改配置文件/etc/gitlab/gitlab.rb里的一些配置。配置如下

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "632328143@qq.com"
gitlab_rails['smtp_password'] = "這里填入自己的授權碼"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = :login
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "632328143@qq.com"
user["git_user_email"] = "632328143@qq.com"

2.初始化服務

這里需要注意的是我們在安裝配置gitlab的之前,我們在服務器不要裝任何類似tomcat的服務,因為它會導致端口被占用。

[root@gitlab ~]# gitlab-ctl reconfigure        #修改完配置文件要執行此操作,然后繼續等待服務啟動完成。

 完成后,我們就可以在瀏覽器中輸入本機ip進入gitlab界面了
第一次進入會提示你修改管理員密碼,按照提示修改就行

 

 密碼修改完之后我們會到下面的頁面,這里我們可以用root加你剛剛修改的密碼進行登陸了,這里我們會看到還有一個注冊的頁面,這里一般后面我們建議關掉,因為可能會導致產生一堆垃圾賬號。

 設置:取消勾選

點擊保存:

驗證是否關閉賬號注冊:

4.Gitlab的漢化

雖然不推薦,但是有需求, 基於第三方開發愛好者實現

1.下載語言包替換

https://gitlab.com/xhang/gitlab

 

 

2.變更過程

[root@centos7 ~]# head -1 /opt/gitlab/version-manifest.txt                  #查看當前gitlab的版本
gitlab-ce 11.11.5
[root@centos7 ~]# wget https://gitlab.com/xhang/gitlab/-/archive/v11.11.5-zh/gitlab-v11.11.5-zh.tar.gz     #這里注意下載-zh的包,別下錯了
[root@centos7 ~]# tar -zxvf gitlab-v11.11.5-zh.tar.gz                     #解壓包
[root@centos7 ~]# gitlab-ctl stop                          #停gitlab服務,如果沒有第一次安裝,就不用執行這一步
[root@centos7 ~]# cp -rp /opt/gitlab/embedded/service/gitlab-rails /opt/gitlab-rails.bak       #備份原目錄
[root@centos7 ~]# \cp -rf gitlab-v11.11.5-zh/* /opt/gitlab/embedded/service/gitlab-rails/   #替換文件,復制過程中提示,以下信息是正常的:
cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/log’ with directory ‘gitlab-v11.11.5-zh/log’
cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/tmp’ with directory ‘gitlab-v11.11.5-zh/tmp’ [root@centos7 ~]# gitlab-ctl reconfigure #重新加載配置 [root@centos7 ~]# gitlab-ctl start #啟動

Web 界面更改語言:
右上角的賬戶下拉框選 Settings 然后左側 Preferences 設置項,然后語言選擇中文

 

 保存后刷新界面,可以看到漢化成功

5.gitlab遷移版本庫(保留原版本庫的所有內容)

在生產中有時候我們會遇到需要跨主機遷移Gitlab里某個庫的里面的內容,但是又不能影響原版本庫里的內容,下面我來演示一下實現方法。

1.從原地址克隆一份裸版本庫

[root@github-no ~]# ll                      
total 16
-rw-------. 1 root root 6706 Sep 13  2017 anaconda-ks.cfg
-rw-------. 1 root root 6556 Sep 13  2017 original-ks.cfg
[root@github-no ~]# git clone --bare http://pl.git.iflytek.com/EvidenceGroup-SH/marking-module-server.git      #這里需要我們輸入項目的具有clone代碼的用戶
和密碼,如果需要遷移的版本庫多,可以通過公私鑰ssh的方式克隆代碼
[root@github-no ~]# ll total 20 -rw-------. 1 root root 6706 Sep 13 2017 anaconda-ks.cfg drwxr-xr-x. 7 root root 4096 May 13 10:05 marking-module-server.git -rw-------. 1 root root 6556 Sep 13 2017 original-ks.cfg

2.然后到新的 Git 服務器上創建一個新項目,比如test_46_38

 

3.以鏡像推送的方式上傳代碼到 test_46_38 服務器上。

[root@github-no ~]# cd marking-module-server.git/
[root@github-no marking-module-server.git]# git push --mirror http://172.31.46.38/root/test_46_38.git   #這里輸入test_46_38項目的具有提交代碼權限的用戶和密碼

這個時候新Gitlab上就有了信息了

 

4.刪除本地代碼

[root@github-no marking-module-server.git]# cd ..
[root@github-no ~]# ll -h
total 20K
-rw-------. 1 root root 6.6K Sep 13  2017 anaconda-ks.cfg
drwxr-xr-x. 7 root root 4.0K May 13 10:05 marking-module-server.git
-rw-------. 1 root root 6.5K Sep 13  2017 original-ks.cfg
[root@github-no ~]# rm -rf marking-module-server.git
[root@github-no ~]# ll -h
total 16K
-rw-------. 1 root root 6.6K Sep 13  2017 anaconda-ks.cfg
-rw-------. 1 root root 6.5K Sep 13  2017 original-ks.cfg

5.到新gitlab服務器上找到clone地址,直接clone到本地就可以了

[root@2-no test]# ll
total 0
[root@2-no test]# git clone http://172.31.46.38/root/test_46_38.git
[root@2-no test]# ll
total 0
[root@2-no test]# git clone http://172.31.46.38/root/test_46_38.git

6.讓開發改本地遠程地址

遠程庫地址變了,這個時候還需要開發,改本地遠程地址,不然一切都白費。

以下均以項目git_test為例:
老地址:http://192.168.1.12:9797/john/git_test.git
新地址:http://192.168.100.235:9797/john/git_test.git
遠程倉庫名稱: origin
方法一 通過命令直接修改遠程地址

    進入git_test根目錄
    git remote 查看所有遠程倉庫, git remote xxx 查看指定遠程倉庫地址
    git remote set-url origin http://192.168.100.235:9797/john/git_test.git

方法二 通過命令先刪除再添加遠程倉庫

    進入git_test根目錄
    git remote 查看所有遠程倉庫, git remote xxx 查看指定遠程倉庫地址
    git remote rm origin
    git remote add origin http://192.168.100.235:9797/john/git_test.git

方法三 直接修改配置文件

    進入git_test/.git

    vim config

    [core]
    repositoryformatversion = 0
    filemode = true
    logallrefupdates = true
    precomposeunicode = true
    [remote "origin"]
    url = http://192.168.100.235:9797/shimanqiang/assistant.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
    remote = origin
    merge = refs/heads/master

    修改 [remote “origin”]下面的url即可

方法四 通過第三方git客戶端修改。

以SourceTree為例,點擊 倉庫 -> 倉庫配置 -> 遠程倉庫 即可管理此項目中配置的所有遠程倉庫, 而且這個界面最下方還可以點擊編輯配置文件,同樣可以完成方法三。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM