一、前言
GitLab是利用 Ruby on Rails 一個開源的版本管理系統(tǒng),實現(xiàn)一個自托管的 Git 項目倉庫,可通過 Web 界面進行訪問公開的或者私人項目。
它擁有與 Github 類似的功能,能夠瀏覽源代碼,管理缺陷和注釋??梢怨芾韴F隊對倉庫的訪問,它非常易于瀏覽提交過的版本并提供一個文件歷史庫。
團隊成員可以利用內(nèi)置的簡單聊天程序(Wall)進行交流。
它還提供一個代碼片段收集功能可以輕松實現(xiàn)代碼復(fù)用,便于日后有需要的時候進行查找。
1、Git的家族成員
- Git:是一種版本控制系統(tǒng),是一個命令,是一種工具。
- Gitlib:是用于實現(xiàn)Git功能的開發(fā)庫。
- Github:是一個基于Git實現(xiàn)的在線代碼托管倉庫,包含一個網(wǎng)站界面,向互聯(lián)網(wǎng)開放。
- GitLab:是一個基于Git實現(xiàn)的在線代碼倉庫托管軟件,你可以用gitlab自己搭建一個類似于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:輕量級的反向代理服務(wù)器。
- logrotate:日志文件管理工具。
- postgresql:數(shù)據(jù)庫。
- redis:緩存數(shù)據(jù)庫。
- sidekiq:用于在后臺執(zhí)行隊列任務(wù)(異步執(zhí)行)。
- unicorn:An HTTP server for Rack applications,GitLab Rails應(yīng)用是托管在這個服務(wù)器上面的。
3、GitLab工作流程
4、GitLab Shell
GitLab Shell有兩個作用:為GitLab處理Git命令、修改authorized keys列表。
當(dāng)通過SSH訪問GitLab Server時,GitLab Shell會限制執(zhí)行預(yù)定義好的Git命令(git push, git pull, git annex),調(diào)用GitLab Rails API 檢查權(quán)限,執(zhí)行pre-receive鉤子(在GitLab企業(yè)版中叫做Git鉤子),執(zhí)行你請求的動作 處理GitLab的post-receive動作,處理自定義的post-receive動作。
當(dāng)通過http(s)訪問GitLab Server時,工作流程取決于你是從Git倉庫拉取(pull)代碼還是向git倉庫推送(push)代碼。如果你是從Git倉庫拉取(pull)代碼,GitLab Rails應(yīng)用會全權(quán)負責(zé)處理用戶鑒權(quán)和執(zhí)行Git命令的工作;如果你是向Git倉庫推送(push)代碼,GitLab Rails應(yīng)用既不會進行用戶鑒權(quán)也不會執(zhí)行Git命令,它會把以下工作交由GitLab Shell進行處理:
1.調(diào)用GitLab Rails API
2.檢查權(quán)限執(zhí)行pre-receive鉤子(在GitLab企業(yè)版中叫做Git鉤子)
3.執(zhí)行你請求的動作
4.處理GitLab的post-receive動作
5.處理自定義的post-receive動作
5、GitLab Workhorse
GitLab Workhorse是一個敏捷的反向代理。它會處理一些大的HTTP請求,比如文件上傳、文件下載、Git push/pull和Git包下載。其它請求會反向代理到GitLab Rails應(yīng)用,即反向代理給后端的unicorn。
二、Gitlab 的安裝
1、安裝和配置必要的依賴關(guān)系
yum install -y curl policycoreutils-python openssh-server openssh-clients
2、添加 Gitlab 倉庫
新建/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 指定訪問的域名。
如何安裝其他版本,可以通過清華大學(xué)源選擇對應(yīng)版本:http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/ 。
4、配置啟動
gitlab-ctl reconfigure
三、Gitlab 管理
1、Gitlab備份
使用 Gitlab 一鍵安裝包安裝 Gitlab 非常簡單, 同樣的備份恢復(fù)與遷移也非常簡單. 使用一條命令即可創(chuàng)建完整的Gitlab 備份:
gitlab-rake gitlab:backup:create
使用以上命令會在/var/opt/gitlab/backups目錄下創(chuàng)建一個名稱類似為1481598919_gitlab_backup.tar的壓縮包, 這個壓縮包就是 Gitlab 整個的完整部分, 其中開頭的:1481598919是備份創(chuàng)建的日期,/etc/gitlab/gitlab.rb配置文件須備份,/var/opt/gitlab/nginx/conf nginx配置文件,/etc/postfix/main.cfpostfix 郵件配置備份。
2、Gitlab恢復(fù)
Gitlab的從備份恢復(fù)也非常簡單:
# 停止相關(guān)數(shù)據(jù)連接服務(wù)
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# 從1481598919編號備份中恢復(fù)
gitlab-rake gitlab:backup:restore BACKUP=1481598919
# 啟動Gitlab
sudo gitlab-ctl start
3、Gitlab自動備份
實現(xiàn)每天凌晨2點進行一次自動備份:通過crontab使用備份命令實現(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 的升級
因為我們使用 Omnibus GitLab package 進行安裝,所以我們的升級相對比較簡單,也建議大家使用這種方式安裝,我目前的版本是10.0.4要升級到11.2.3,這算是大版本升級,根據(jù)官方文檔的要求,我們需要先升級到10.x的最高版本。
1、升級過渡版本 10.8.7
升級過程中會對數(shù)據(jù)進行自動備份,不用擔(dān)心數(shù)據(jù)安全。
# 下載對應(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
# 安裝此過渡版本
rpm -Uvh gitlab-ce-10.8.7-ce.0.el7.x86_64.rpm
2、升級最新版本 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
#升級過程
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、升級成功
更多GitLab相關(guān)教程見以下內(nèi)容:
CentOS7安裝GitLab、漢化及使用 http://www.sfodin.cn/Linux/2017-11/148223.htm
CentOS 7安裝部署GitLab服務(wù)器 http://www.sfodin.cn/Linux/2017-06/144990.htm
CentOS 7.x上GitLab搭建詳細教程 http://www.sfodin.cn/Linux/2017-12/149766.htm
CentOS 7安裝部署GitLab服務(wù)器 http://www.sfodin.cn/Linux/2017-06/144990.htm
CentOS 7使用Docker搭建GitLab服務(wù)器 http://www.sfodin.cn/Linux/2018-04/151725.htm
Ubuntu 16.04搭建GitLab服務(wù)器 http://www.sfodin.cn/Linux/2018-01/150319.htm
快速學(xué)會CentOS配置GitLab http://www.sfodin.cn/Linux/2018-08/153345.htm