一、前言
GitLab是利用 Ruby on Rails 一個(gè)開(kāi)源的版本管理系統(tǒng),實(shí)現(xiàn)一個(gè)自托管的 Git 項(xiàng)目倉(cāng)庫(kù),可通過(guò) Web 界面進(jìn)行訪問(wèn)公開(kāi)的或者私人項(xiàng)目。
它擁有與 Github 類似的功能,能夠?yàn)g覽源代碼,管理缺陷和注釋??梢怨芾韴F(tuán)隊(duì)對(duì)倉(cāng)庫(kù)的訪問(wèn),它非常易于瀏覽提交過(guò)的版本并提供一個(gè)文件歷史庫(kù)。
團(tuán)隊(duì)成員可以利用內(nèi)置的簡(jiǎn)單聊天程序(Wall)進(jìn)行交流。
它還提供一個(gè)代碼片段收集功能可以輕松實(shí)現(xiàn)代碼復(fù)用,便于日后有需要的時(shí)候進(jìn)行查找。
1、Git的家族成員
- Git:是一種版本控制系統(tǒng),是一個(gè)命令,是一種工具。
- Gitlib:是用于實(shí)現(xiàn)Git功能的開(kāi)發(fā)庫(kù)。
- Github:是一個(gè)基于Git實(shí)現(xiàn)的在線代碼托管倉(cāng)庫(kù),包含一個(gè)網(wǎng)站界面,向互聯(lián)網(wǎng)開(kāi)放。
- GitLab:是一個(gè)基于Git實(shí)現(xiàn)的在線代碼倉(cāng)庫(kù)托管軟件,你可以用gitlab自己搭建一個(gè)類似于Github一樣的系統(tǒng),一般用于在企業(yè)、學(xué)校等內(nèi)部網(wǎng)絡(luò)搭建git私服。
2、Gitlab的服務(wù)構(gòu)成
- Nginx:靜態(tài)web服務(wù)器。
- gitlab-shell:用于處理Git命令和修改authorized keys列表。
- gitlab-workhorse:輕量級(jí)的反向代理服務(wù)器。
- logrotate:日志文件管理工具。
- postgresql:數(shù)據(jù)庫(kù)。
- redis:緩存數(shù)據(jù)庫(kù)。
- sidekiq:用于在后臺(tái)執(zhí)行隊(duì)列任務(wù)(異步執(zhí)行)。
- unicorn:An HTTP server for Rack applications,GitLab Rails應(yīng)用是托管在這個(gè)服務(wù)器上面的。
3、GitLab工作流程
4、GitLab Shell
GitLab Shell有兩個(gè)作用:為GitLab處理Git命令、修改authorized keys列表。
當(dāng)通過(guò)SSH訪問(wèn)GitLab Server時(shí),GitLab Shell會(huì)限制執(zhí)行預(yù)定義好的Git命令(git push, git pull, git annex),調(diào)用GitLab Rails API 檢查權(quán)限,執(zhí)行pre-receive鉤子(在GitLab企業(yè)版中叫做Git鉤子),執(zhí)行你請(qǐng)求的動(dòng)作 處理GitLab的post-receive動(dòng)作,處理自定義的post-receive動(dòng)作。
當(dāng)通過(guò)http(s)訪問(wèn)GitLab Server時(shí),工作流程取決于你是從Git倉(cāng)庫(kù)拉取(pull)代碼還是向git倉(cāng)庫(kù)推送(push)代碼。如果你是從Git倉(cāng)庫(kù)拉取(pull)代碼,GitLab Rails應(yīng)用會(huì)全權(quán)負(fù)責(zé)處理用戶鑒權(quán)和執(zhí)行Git命令的工作;如果你是向Git倉(cāng)庫(kù)推送(push)代碼,GitLab Rails應(yīng)用既不會(huì)進(jìn)行用戶鑒權(quán)也不會(huì)執(zhí)行Git命令,它會(huì)把以下工作交由GitLab Shell進(jìn)行處理:
1.調(diào)用GitLab Rails API
2.檢查權(quán)限執(zhí)行pre-receive鉤子(在GitLab企業(yè)版中叫做Git鉤子)
3.執(zhí)行你請(qǐng)求的動(dòng)作
4.處理GitLab的post-receive動(dòng)作
5.處理自定義的post-receive動(dòng)作
5、GitLab Workhorse
GitLab Workhorse是一個(gè)敏捷的反向代理。它會(huì)處理一些大的HTTP請(qǐng)求,比如文件上傳、文件下載、Git push/pull和Git包下載。其它請(qǐng)求會(huì)反向代理到GitLab Rails應(yīng)用,即反向代理給后端的unicorn。
二、Gitlab 的安裝
1、安裝和配置必要的依賴關(guān)系
yum install -y curl policycoreutils-python openssh-server openssh-clients
2、添加 Gitlab 倉(cāng)庫(kù)
新建/etc/yum.repos.d/gitlab-ce.repo,內(nèi)容為
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
3、安裝 Gitlab (Omnibus方式)
yum makecache
EXTERNAL_URL=”http://git.linuxidc.com” yum install -y gitlab-ce
注:EXTERNAL_URL 指定訪問(wèn)的域名。
如何安裝其他版本,可以通過(guò)清華大學(xué)源選擇對(duì)應(yīng)版本:http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/ 。
4、配置啟動(dòng)
gitlab-ctl reconfigure
三、Gitlab 管理
1、Gitlab備份
使用 Gitlab 一鍵安裝包安裝 Gitlab 非常簡(jiǎn)單, 同樣的備份恢復(fù)與遷移也非常簡(jiǎn)單. 使用一條命令即可創(chuàng)建完整的Gitlab 備份:
gitlab-rake gitlab:backup:create
使用以上命令會(huì)在/var/opt/gitlab/backups目錄下創(chuàng)建一個(gè)名稱類似為1481598919_gitlab_backup.tar的壓縮包, 這個(gè)壓縮包就是 Gitlab 整個(gè)的完整部分, 其中開(kāi)頭的:1481598919是備份創(chuàng)建的日期,/etc/gitlab/gitlab.rb配置文件須備份,/var/opt/gitlab/nginx/conf nginx配置文件,/etc/postfix/main.cfpostfix 郵件配置備份。
2、Gitlab恢復(fù)
Gitlab的從備份恢復(fù)也非常簡(jiǎn)單:
# 停止相關(guān)數(shù)據(jù)連接服務(wù)
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# 從1481598919編號(hào)備份中恢復(fù)
gitlab-rake gitlab:backup:restore BACKUP=1481598919
# 啟動(dòng)Gitlab
sudo gitlab-ctl start
3、Gitlab自動(dòng)備份
實(shí)現(xiàn)每天凌晨2點(diǎn)進(jìn)行一次自動(dòng)備份:通過(guò)crontab使用備份命令實(shí)現(xiàn)
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
四、Gitlab的使用
Git global setup
git config –global user.name “linuxidc”
git config –global user.email “admin@linuxidc.com”
Create a new repository
git clone http://git.linuxidc.com/ios/app1.git
cd app1
touch README.md
git add README.md
git commit -m “add README”
git push -u origin master
Existing folder
cd existing_folder
git init
git remote add origin http://git.linuxidc.com/ios/app1.git
git commit -m “Initial commit”
git push -u origin master
Existing Git repository
cd existing_repo
git remote add origin http://git.linuxidc.com/ios/app1.git
git push -u origin –all
git push -u origin –tags
五、Gitlab 的升級(jí)
因?yàn)槲覀兪褂?Omnibus GitLab package 進(jìn)行安裝,所以我們的升級(jí)相對(duì)比較簡(jiǎn)單,也建議大家使用這種方式安裝,我目前的版本是10.0.4要升級(jí)到11.2.3,這算是大版本升級(jí),根據(jù)官方文檔的要求,我們需要先升級(jí)到10.x的最高版本。
1、升級(jí)過(guò)渡版本 10.8.7
升級(jí)過(guò)程中會(huì)對(duì)數(shù)據(jù)進(jìn)行自動(dòng)備份,不用擔(dān)心數(shù)據(jù)安全。
# 下載對(duì)應(yīng)版本的 rpm 包
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.8.7-ce.0.el7.x86_64.rpm
# 安裝此過(guò)渡版本
rpm -Uvh gitlab-ce-10.8.7-ce.0.el7.x86_64.rpm
2、升級(jí)最新版本 11.2.3
# 下載最新版本的 rpm 包
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.2.3-ce.0.el7.x86_64.rpm
# 安裝最新版本
rpm -Uvh gitlab-ce-11.2.3-ce.0.el7.x86_64.rpm
#升級(jí)過(guò)程
warning: gitlab-ce-11.2.3-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
Preparing… ################################# [100%]
gitlab preinstall: Automatically backing up only the GitLab SQL database (excluding everything else!)
Dumping database …
Dumping PostgreSQL database gitlabhq_production … [DONE]
done
Dumping repositories …
[SKIPPED]
Dumping uploads …
[SKIPPED]
Dumping builds …
[SKIPPED]
Dumping artifacts …
[SKIPPED]
Dumping pages …
[SKIPPED]
Dumping lfs objects …
[SKIPPED]
Dumping container registry images …
[DISABLED]
Creating backup archive: 1535946629_2018_09_03_10.8.7_gitlab_backup.tar … done
Uploading backup archive to remote storage … skipped
Deleting tmp directories … done
done
Deleting old backups … skipping
Updating / installing…
1:gitlab-ce-11.2.3-ce.0.el7 ############################# ( 87%)
……
……
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __
/ /_/ / / /_/ /___/ /_/ / /_/ /
____/_/__/_____/__,_/_.___/
Upgrade complete! If your GitLab server is misbehaving try running
sudo gitlab-ctl restart
before anything else.
If you need to roll back to the previous version you can use the database
backup made during the upgrade (scroll up for the filename).
3、升級(jí)成功
更多GitLab相關(guān)教程見(jiàn)以下內(nèi)容:
CentOS7安裝GitLab、漢化及使用 http://fxwbio.com/Linux/2017-11/148223.htm
CentOS 7安裝部署GitLab服務(wù)器 http://fxwbio.com/Linux/2017-06/144990.htm
CentOS 7.x上GitLab搭建詳細(xì)教程 http://fxwbio.com/Linux/2017-12/149766.htm
CentOS 7安裝部署GitLab服務(wù)器 http://fxwbio.com/Linux/2017-06/144990.htm
CentOS 7使用Docker搭建GitLab服務(wù)器 http://fxwbio.com/Linux/2018-04/151725.htm
Ubuntu 16.04搭建GitLab服務(wù)器 http://fxwbio.com/Linux/2018-01/150319.htm
快速學(xué)會(huì)CentOS配置GitLab http://fxwbio.com/Linux/2018-08/153345.htm