一. GitLab簡介
GitLab 是利用 Ruby on Rails 一個開源的版本管理系統,實現一個自托管的 Git 項目倉庫,可通過 Web 界面進行訪問公開的或者私人項目。
與 Github 類似,GitLab 能夠瀏覽源代碼,管理缺陷和注釋。可以管理團隊對倉庫的訪問,它非常易於瀏覽提交過的版本並提供一個文件歷史庫。團隊成員可以利用內置的簡單聊天程序(Wall)進行交流。它還提供一個代碼片段收集功能可以輕松實現代碼復用,
便於日后有需要的時候進行查找
• GitLab 是一個用於倉庫管理系統的開源項目,使用Git作為代碼管理工具,並在此基礎上搭建起來的web服務
• GitLab基於Ruby語言編寫,是烏克蘭的程序猿研發的產品
常用的網站:
官網:https://about.gitlab.com/
國內鏡像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/
二. GitLab部署
1. Gitlab安裝
安裝依賴
[root@m01 ~]# yum install curl policycoreutils openssh-server openssh-clients policycoreutils-python –y
#下載安裝包
[root@m01 ~]# cd /server/tools/
[root@m01 tools]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.6.10-ce.0.el7.x86_64.rpm
# 安裝gitlab
[root@m01 tools]# rpm -ivh gitlab-ce-11.6.10-ce.0.el7.x86_64.rpm
[root@m01 tools]# rpm -qa |grep gitlab
gitlab-ce-11.6.10-ce.0.el7.x86_64
2. Gitlab配置
GitLab 的默認配置文件為於:/etc/gitlab/gitlab.rb,修改下圖所示的 external_url為本機 IP 地址或者一個可以訪問到本機的域名。
修改完配置文件后,使用gitlab-ctl reconfigure重新配置gitlab
[root@m01 tools]# gitlab-ctl reconfigure
[root@m01 tools]# gitlab-ctl status
[root@m01 tools]# gitlab-ctl restart
說明:gitlab服務說明:
GitLab 由主要由以下服務構成,他們共同承擔了 Gitlab 的運作需要
nginx web服務器
gitlab-workhorse 輕量級反向代理服務器
logrotate 日志的分割處理
postgresql gitlab數據庫
redis gitlab緩存
sidekiq gitlab任務隊列
unicorn gitlab服務托管
3. 瀏覽器頁面訪問,首次登陸會要求更改root用戶的密碼,更改完密碼之后,用root登錄
http://10.0.0.61
三、gitlab常用命令
gitlab-ctl start: 啟動全部服務
gitlab-ctl restart: 重啟全部服務
gitlab-ctl stop:停止全部服務
gitlab-ctl reconfigure: 使配置文件生效(只要一修改配置文件就必須執行此命令)
gitlab-ctl show-config :驗證配置文件
gitlab-ctl uninstall: 刪除gitlab(保留數據)
gitlab-ctl cleanse: 刪除所有數據,從新開始
gitlab-ctl tail <service name> 查看服務的日志
四、gitlab的目錄
/var/opt/gitlab/git-data/repositories: 庫默認存儲目錄
/opt/gitlab 應用代碼和相應的依賴程序
/var/opt/gitlab gitlab-ctl reconfigure 命令編譯后的應用數據和配置文件,不需要人為修改配置
/etc/gitlab 配置文件目錄
/var/log/gitlab 此目錄下存放了gitlab各個組件產生的日志
/var/opt/gitlab/backups/ 備份文件生成的目錄
五. GitLab配置
1. 關閉注冊
• 目的:使系統更安全,員工賬號由開發組長統一分配
由於我們Gitlab系統是私有倉庫,一般用戶都是由管理員創建和分派的,所以我們需要關閉注冊。使用 root 用戶登錄,點擊頁面上方的 Adminarea,進入管理員區域頁面,點擊頁面左側菜單欄下方的 Setting,進入 Settings 頁面,下拉頁面找到 Sign-up Restrictions 選項,取消 Sign-up enabled 選項前面的勾選,下拉到頁面的下方,點擊 Save 按鈕完成配置。退回到系統的登錄頁面,發現已經沒有用戶注冊功能。
①. 以管理員登錄
②. 修改系統設置
注意:不勾選的是Sign-up Restrictions,而不是Sign-in Restrictions,否者會無法登錄
③. 最終結果
2. 自定義專屬首頁
①. 修改系統設置
②. 最終結果
3. 創建用戶、用戶組和項目
GitLab 是通過組(group)的概念來統一管理倉庫(project)和用戶(user),通過創建組,在組下再創建倉庫,再將用戶加入到組,從而實現用戶與倉庫的權限管理。
①. 創建用戶組
在管理員頁面點擊頁面頂部的 Admin area 按鈕,進入管理員區域,在頁面右側點擊綠色的 New group 按鈕,進入創建組頁面。在創建組頁面中,組路徑和組名稱為必填項,而且此兩處內容好一致:
注:visibility Level:選擇誰可以訪問該組:我們默認選擇 private 即可
Private:只有授權的用戶才可以看到
Internal:只要是登錄 gitlab 的用戶就可以看到
Public:只要可以訪問 gitlab web 頁面的人就可以看到
點擊頁面下的 create group 按鈕,完成組的創建,進入組管理頁面
②. 創建新用戶
在管理員頁面點擊頁面頂部的 Admin area 按鈕,進入管理員區域,在頁面右側點擊綠色的 New user 按鈕,進入創建用戶頁面:在創建用戶頁面,輸入用戶名昵稱、用戶名、電子郵件、選擇用戶級別。點擊頁面下部的 create user 按鈕,完成用戶創建,進入用戶管理頁面,點擊頁面右上頁的 Edit 按鈕,為用戶設置初始密碼,在此頁面也可同時修改用戶注冊信息。
③. 添加用戶到組
用戶創建完成后,需要對用戶進行授權,從而使用戶可以管理倉庫,有兩種方式,一是將用戶加入到組,這樣用戶可以管理組內的倉庫,二是直接授權用戶管理倉庫。通常我們采用的方式是將用戶加入相應的組,並賦予不同的角色。GitLab 中用戶的角色是系統定義好的,不能更改。
注:關於每一種角色對應的權限,可參見官方文檔相關內容:https://docs.gitlab.com/ee/user/permissions.html
下面我們將剛創建的 learn_gitlab 用戶添加到我們的 group01 組,將賦予 guest權限,在管理員區域,點擊 oldboy 組,進入組管理頁面:選擇我們剛創建的 learn_gitlab 用戶,選擇 guest 角色,然后添加到組。
④. 創建項目
在 GitLab 中,可以創建 project 用來存儲你的程序代碼、作為一個問題跟蹤器、用於代碼協作、用於持續集成中的構建、測試和部署等。
在管理員區域點擊 New project 按鈕,或者點擊導航欄中的 選擇 New project 選項,進入到新建 project 頁面,選擇倉庫所屬的組,輸入倉庫名稱、倉庫描述,選擇可見級別,即可完成倉庫創建。進入倉庫主頁面:頁面左側部分為倉庫操作相關菜單欄,右側空倉庫下顯示如何在命令行連接該倉庫,非空時顯示倉庫內容。
將 git_test 倉庫加入到group01 組后,組內的成員即可以看到該倉庫,我們使用learn_gitlab用戶登錄,即可看到該倉庫。
4. push項目
①. 客戶端生成公鑰
在 GitLab 創建了倉庫,並且授權用戶可以使用倉庫。使用客戶端來連接創建的倉庫。倉庫是私有的,只有授權的用戶才可以訪問到該倉庫,那么只要將客戶端的用戶與GitLab 的用戶綁定,客戶端即可訪問到 GitLab 上的倉庫,建議使用 SSH 方式實現客戶端與 Gitlab 用戶的綁定,具體配置如下:
在客戶端生成 ssh 密鑰對(注 windows 客戶端下只能使用 rsa 加密方式)
[root@m01 tools]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:gwBftc4O7xj0e5IhbEmoQ3/2HXW/+95msVA4X5QUiSQ root@m01
The key's randomart image is:
+---[RSA 2048]----+
| . ... E..oo+|
| o . . .. o.|
| o. . .. |
| . ...+ .o...|
| . o o+.S . .+..|
| o ..B=... . o.|
| . +.o++ . . +|
| ++.o o+|
| . oo =*|
+----[SHA256]-----+
[root@m01 tools]# ll /root/.ssh/id_rsa.pub
-rw-r--r-- 1 root root 390 Jun 21 13:15 /root/.ssh/id_rsa.pub
②. Gitlab配置公鑰
與 GitLab 的 root 用戶綁定,復制用戶的公鑰,在 GitLab 主頁面點擊用戶設置。此處只能添加公鑰,如果添加私鑰系統將報錯,此外一個公鑰在整個 GitLab 系統中只能添加一次,但是一個 GitLab 用戶可以添加多個公鑰。
③. 添加遠程倉庫並推送數據
將GitLab上的git_test倉庫配置為m01上git_test倉庫的遠程倉庫,再使用推送的功能進行推送。
[root@m01 git_test]# git remote add gitlab git@10.0.0.61:group01/git_test.git
[root@m01 git_test]# git remote
gitlab
#數據推送
[root@m01 git_test]# git push -u gitlab master
The authenticity of host '10.0.0.61 (10.0.0.61)' can't be established.
ECDSA key fingerprint is SHA256:0BO/nvgtYtJ/lws3jOlG9OkJ8Vw9m0glK5iIl3SjXtk.
ECDSA key fingerprint is MD5:4c:1a:42:4c:76:58:02:47:04:ff:40:8d:e5:26:6b:69.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.61' (ECDSA) to the list of known hosts.
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 299 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To git@10.0.0.61:group01/git_test.git
* [new branch] master -> master
Branch master set up to track remote branch master from gitlab.
推送完后可以在gitlab倉庫中看到推上來的內容
5. clone項目&push分支
①. 客戶端生成公鑰
使用ssh-keygen -t rsa命令生成
[root@web01 ~]# ll /root/.ssh/id_rsa.pub
-rw-r--r-- 1 root root 392 Jun 11 22:57 /root/.ssh/id_rsa.pub
②, GitLab配置公鑰
③. clone項目
使用 git clone 命令克隆倉庫到 web01本地
[root@web01 server]# mkdir git_data
[root@web01 server]# cd git_data/
[root@web01 git_data]# ll
total 0
[root@web01 git_data]# git clone git@10.0.0.61:group01/git_test.git
Cloning into 'git_test'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
[root@web01 git_data]# ll
total 0
drwxr-xr-x 3 root root 69 Jun 21 13:36 git_test
[root@web01 git_data]# ll git_test/
total 8
-rw-r--r-- 1 root root 12 Jun 21 13:36 test1.txt
-rw-r--r-- 1 root root 7 Jun 21 13:36 test2.txt
-rw-r--r-- 1 root root 0 Jun 21 13:36 test3.txt
[root@web01 git_test]# git remote
origin
已經將 GitLab 上的 git_test 倉庫克隆到了 web01 本地,同時為本地倉庫添加了一個指向 GitLab 上 git_test倉庫的遠程倉庫
④. push分支
web01 的 git_test上創建一個 dev 分支,並將 dev 分支,推送到 GitLab 上
[root@web01 git_test]# git branch dev
[root@web01 git_test]# git branch
dev
* master
[root@web01 git_test]# git checkout dev
Switched to branch 'dev'
[root@web01 git_test]# git status
# On branch dev
nothing to commit, working directory clean
[root@web01 git_test]# touch dev.txt
[root@web01 git_test]# git add dev.txt
[root@web01 git_test]# git commit -m "add dev.txt on branch dev"
[dev 5c52ecf] add dev.txt on branch dev
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev.txt
[root@web01 git_test]# git push -u origin dev
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 297 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote: http://10.0.0.61/group01/git_test/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To git@10.0.0.61:group01/git_test.git
* [new branch] dev -> dev
Branch dev set up to track remote branch dev from origin.
完成后可以在gitlab在看到我們推送上來的分支
6. 分支目錄保護
在實際使用過程中,我們通常會保持 master 分支穩定,用於生產環境的版本發布,只有授權的用戶才可以向 master 合並代碼。要實現此功能,我們需要將 master設置為保護分支,並授權什么用戶可以向 master 用戶推送代碼.
使用 root 用戶點擊 git_test 倉庫頁面左下角的 Settings,設置完成后,在倉庫分支頁面,可看到 master 分支后面出現一個綠色的 protected 標記。
高版本的gitlab默認master受保護
六. GitLab數據備份與恢復
1. 數據備份
備份文件將保存在配置文件中定義的backup_path 中,文件名TIMESTAMP_gitlab_backup.tar,TIMESTAMP 為備份時的時間戳。TIMESTAMP 的格式為:EPOCH_YYYY_MM_DD_Gitlab-version。
默認的備份文件目錄為:/var/opt/gitlab/backups,如果自定義備份目錄需要賦予目錄 git 權限,具體操作如下:
①. 修改配置文件
在配置文件/etc/gitlab/gitlab.rb 最后處加入
gitlab_rails['backup_path'] = '/data/backup/gitlab'
gitlab_rails['backup_keep_time'] = 604800 #備份保留的時間(以秒為單位, 這個是七天默認值)
②. 對文件夾授權
[root@m01 ~]# mkdir -p /data/backup/gitlab
[root@m01 ~]# chown -R git.git /data/backup/gitlab
[root@m01 ~]# gitlab-ctl reconfigure
配置完后,使用 gitlab-ctl reconfigure重新加載配置文件
③. 手動進行第一次備份
使用命令gitlab-rake gitlab:backup:create進行備份
[root@m01 ~]# gitlab-rake gitlab:backup:create
Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
* group01/git_test ... [DONE]
[SKIPPED] Wiki
done
Dumping uploads ...
done
Dumping builds ...
done
Dumping artifacts ...
done
Dumping pages ...
done
Dumping lfs objects ...
done
Dumping container registry images ...
[DISABLED]
Creating backup archive: 1592723065_2020_06_21_11.6.10_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... done. (0 removed)
④. 查看備份結果
[root@m01 ~]# ll /data/backup/gitlab/
total 280
-rw------- 1 git git 286720 Jun 21 15:04 1592723065_2020_06_21_11.6.10_gitlab_backup.tar
⑤. Gitlab定時備份
通過在定時任務里添加:
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
實現定時備份,由於代碼是一個企業非常重要的資產,所以我們要重視 GitLab的備份工作。至少做到每天備份一次,平時要注意檢查備份的完整性。
環境變量 CRON=1 的作用是如果沒有任何錯誤發生時, 抑制備份腳本的所有進度輸出
[root@m01 ~]# crontab -e
crontab: installing new crontab
[root@m01 ~]# crontab -l
#crond-id-0001:time sync by test
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
#crond-id-0002:backup gitlab data
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
2. 數據恢復
GitLab 的恢復只能還原到與備份文件相同的gitlab 版本的系統中,恢復時,停止連接到數據庫的進程(也就是停止數據寫入服務),但是保持 GitLab 是運行的
①. 停止數據寫入服務
[root@m01 ~]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@m01 ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
②. 執行數據恢復
[root@m01 gitlab]# gitlab-rake gitlab:backup:restore BACKUP=1592723065_2020_06_21_11.6.10
[root@m01 gitlab]# ll
total 280
-rw------- 1 git git 286720 Jun 21 15:04 1592723065_2020_06_21_11.6.10_gitlab_backup.tar
drwx------ 7 git git 131 Jun 21 15:14 tmp
[root@m01 gitlab]# cd /tmp
[root@m01 tmp]# ll
total 0
drwx------ 2 git git 38 Jun 21 15:01 gitaly-ruby540531071
③. 重啟gitlab
gitlab-ctl restart
④. 查看恢復結果
[root@m01 ~]# ll /data/backup/gitlab/
total 280
-rw------- 1 git git 286720 Jun 21 15:04 1592723065_2020_06_21_11.6.10_gitlab_backup.tar
drwx------ 7 git git 131 Jun 21 15:14 tmp
[root@m01 ~]# ll /data/backup/gitlab/tmp/
total 0
drwx------ 2 git git 6 Jun 21 15:14 artifacts.1592723666
drwx------ 2 git git 6 Jun 21 15:14 builds.1592723666
drwx------ 2 git git 6 Jun 21 15:14 lfs.1592723666
drwx------ 2 git git 6 Jun 21 15:14 pages.1592723666
drwx------ 4 git git 26 Jun 21 15:14 uploads.1592723666
3. 注意事項
• 如果需要升級Gitlab版本,備份數據建議從網絡導入,以免備份的數據出現不兼容的情況
七、 Gitlab升級
首先,下載新版本的 RPM 包,可以通過官網或者清華鏡像站獲取。
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el8/gitlab-ce-13.0.6-ce.0.el8.x86_64.rpm
其次關閉部分 gitlab 服務
[root@m01 tools]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@m01 tools]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@m01 tools]# gitlab-ctl stop nginx
ok: down: nginx: 1s, normally up
執行升級操作
rpm -Uvh gitlab-ce-13.0.6-ce.0.el8.x86_64.rpm
重新配置 gitlab
gitlab-ctl reconfigure
重啟 gitlab 服務
gitlab-ctl restart
注:升級操作不建議進行。如果確實需要,也可以采取在一台新的服務器上安裝新版本的 Gitlab,然后采用導入庫的方式將舊系統的代碼倉庫導入到新 Gitlab 上