1、GitLab介紹
1.1 GitLab簡介
GitLab 是一個用於倉庫管理系統的開源項目。
1.2 含義
使用git作為代碼管理工具,並在此基礎上搭建起來的web服務。
1.3 應用特點
1)web框架使用ruby on rails
2)基於MIT代碼發布協議
3)需要gitolite協調工作
1.4 架構
1)前端:Nginx,用於git tool走http或https協議
2)后端:gitlab服務,采用ruby on Rails框架,通過unicorn實現后台服務及多進程
3)SSHD:開啟sshd服務,用於用戶上傳ssh key進行版本克隆及上傳。注:用戶上傳的ssh key是保存到git賬戶中
4)數據庫:目前僅支持MySQL和PostgreSQL
5)Redis:用於存儲用戶session和任務,任務包括新建倉庫、發送郵件等等
6)Sidekiq:Rails框架自帶的,訂閱redis中的任務並執行
2、GitLab部署配置(RPM安裝)
官網部署說明:
https://about.gitlab.com/downloads/#centos6
http://www.gitlab.cc/downloads/#centos6(中文)
手冊:
http://doc.gitlab.com/omnibus/
資料:
http://www.tuicool.com/articles/ZR7rmuz
官網支持的方式:
1)包含一切的rpm包(官方推薦)
https://about.gitlab.com/downloads/
2)手動安裝(深入了解)
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md
3)第三方docker鏡像
https://github.com/sameersbn/docker-gitlab
資料:http://www.tuicool.com/articles/bYbi2mJ
2.1 安裝配置依賴項
如果已經安裝了Postfix來發送郵件請在安裝期間選擇 'Internet Site'. 你也可以用Sendmail或者使用自定義的SMTP服務器來代替Postfix. 如果希望使用 Exim, 請把它當做SMTP來配置.在Centos 6和7上, 下面的命令也會配置系統防火牆,把HTTP和SSH端口開放.
yum install curl openssh-server postfix cronie
service postfix start
chkconfig postfix on
lokkit -s http -s ssh
2.2 添加並安裝GitLab軟件包
curl http://packages.gitlab.cc/install/gitlab-ce/script.rpm.sh | sudo bash
yum install gitlab-ce
如果不習慣這種通過管道命令安裝的方式,可以在這里找到完整的安裝腳本.或者你可以選擇對應系統的GitLab安裝包 並使用下面的命令進行安裝
curl -LJO http://mirror.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-XXX.rpm/download
rpm -i gitlab-ce-XXX.rpm
(推薦使用gitlab-ce鏡像站,下載后保留使用300M左右)
國內yum源:
2.3 配置和使用GitLab
gitlab-ctl reconfigure
2.4 在瀏覽器訪問GitLab主機名
Username: root
Password: 5iveL!fe
2.5 修改域名
默認的配置文件保存在 /etc/gitlab/gitlab.rb ,執行:
vim /etc/gitlab/gitlab.rb
external_url 'http://your.domain'
gitlab-ctl reconfigure
有一定概率出現 502 錯誤,刷新瀏覽器或者再次更新配置即可。
2.6 界面漢化(測試通過)
問題:保證版本一致性,否則上傳代碼會觸發bug
由於服務對象是廣大師生,為了降低新手上手的難度,所有進行漢化也是非常有必要的。好在國內有人已經進行了這方面的工作,我們只需要共享其成果即可(歡迎向原項目提交高質量翻譯)。
首先確認版本:
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
並確認當前漢化版本的VERSION 是否相同,當前最新的漢化版本為8.6。如果安裝版本小於當前漢化版本,請先升級。如果安裝版本大於當前漢化版本,請在本項目中提交新的issue。如果版本相同,首先在本地 clone 倉庫。
# GitLab.com 倉庫
git clone https://gitlab.com/larryli/gitlab.git
# 或 Coding.net 鏡像
git clone https://git.coding.net/larryli/gitlab.git
根據我的測試,Coding.net的鏡像不完整,clone之后無法checkout。然后比較漢化分支和原分支,導出 patch用的diff文件。
# 8.1 版本的漢化補丁
git diff origin/8-6-stable..8-6-zh > ../8.6.diff (已經生成可以直接進行下面的操作)
然后上傳 8.6.diff 文件到服務器。
# 停止 gitlab
gitlab-ctl stop
patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < 8.6.diff
確定沒有.rej文件,重啟 GitLab即可。
gitlab-ctl start
如果漢化中出現問題,請重新安裝 GitLab( 注意備份數據 )。
2.7管理
1)啟動、停止、重啟組件
# 啟動所有 GitLab 組件:
gitlab-ctl start
# 停止所有 GitLab 組件:
gitlab-ctl stop
# 重啟所有 GitLab 組件:
gitlab-ctl restart
2)常用管理命令
1、如果更改了主配置文件 [gitlab.rb文件]
=====
需要以下操作
1、gitlab-ctl reconfigure 使配置文件生效 但是會初始化除了gitlab.rb之外的所有文件
2、gitlab-ctl show-config 驗證配置文件
3、gitlab-ctl restart 重啟gitlab服務
=====
# gitlab-ctl stop unicorn 停止組件內某一個服務
# gitlab-ctl status unicorn 查看狀態
# gitlab-ctl kill unicorn kill掉某一個服務
# gitlab-ctl status unicorn 再次查看狀態
# gitlab-ctl start unicorn 啟動服務
# gitlab-rake gitlab:env:info 系統信息監測
# gitlab-rake gitlab:check 各種狀態監測
/var/opt/gitlab/git-data/repositories/root:庫默認存儲目錄
/opt/gitlab:是gitlab的應用代碼和相應的依賴程序
/var/opt/gitlab:此目錄下是運行gitlab-ctl reconfigure命令編譯后的應用數據和配置文件,不需要人為修改配置
/etc/gitlab:此目錄下存放了以omnibus-gitlab包安裝方式時的配置文件,這里的配置文件才需要管理員手動編譯配置
/var/log/gitlab:此目錄下存放了gitlab各個組件產生的日志
/var/opt/gitlab/backups/:備份文件生成的目錄
2.8 備份和恢復
1)備份
備份GitLab repositories and GitLab metadata
在 crontab 中加入如下命令:
0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create
案例:
[root@vm10-253-26-148 gitlab-workhorse]# /usr/bin/gitlab-rake gitlab:backup:create Dumping database ... Dumping PostgreSQL database gitlabhq_production ... [DONE] done Dumping repositories ... * root/jsws ... [DONE] * root/jsws.wiki ... [SKIPPED] done Dumping uploads ... done Dumping builds ... done Dumping artifacts ... done Dumping lfs objects ... done Creating backup archive: 1461569556_gitlab_backup.tar ... done Uploading backup archive to remote storage ... skipped Deleting tmp directories ... done done done done done done done Deleting old backups ... Skipping
默認生成備份文件的路徑:
[root@vm10-253-26-148 gitlab-workhorse]# cd /var/opt/gitlab/backups/
[root@vm10-253-26-148 backups]# ls -al
total 88
drwx------ 2 git root 4096 Apr 25 15:32 .
drwxr-xr-x 13 root root 4096 Apr 22 15:25 ..
-rw------- 1 git git 81920 Apr 25 15:32 1461569556_gitlab_backup.tar # 備份文件是一個歸檔文件,且開頭是unix時間
在實際的生產環境中備份策略建議:本地保留三天,在異地備份永久保存
這里以unix時間戳來標記備份的時間,這個時間戳對人來說不好讀懂,可使用date命令把其轉換成人可讀的格式,如下:
root@test1:~/backup# date -d @1453432213
2016年 01月 22日 星期五 11:10:13 CST
修改備份目錄
/etc/gitlab/gitlab.rb文件中啟用以下兩項:
gitlab_rails['backup_path'] = "/data/git-backups"
# limit backup lifetime to 7 days - 604800 seconds
gitlab_rails['backup_keep_time'] = 604800
創建備份目錄,修改屬主和屬組:
root@test1:~# mkdir /data/git-backups
chown -R git.git /data/git-backups
手動進行一次備份,測試一下備份是否有效,測試備份正常,添加定時任務即可
2)恢復
首先進入備份 GitLab 的目錄,這個目錄是配置文件中的 gitlab_rails['backup_path'] ,默認為 /var/opt/gitlab/backups 。然后停止 unicorn 和 sidekiq ,保證數據庫沒有新的連接,不會有寫數據情況。
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
然后恢復數據,1406691018為備份文件的時間戳
gitlab-rake gitlab:backup:restore BACKUP=1406691018
2.9 GitLab命令
[root@vm10-253-26-148 ~]# git git gitk gitlab-rails git-receive-pack git-upload-archive git-cvsserver gitlab-ctl # GitLab服務管理 gitlab-rake git-shell git-upload-pack [root@vm10-253-26-148 ~]# gitlab-ctl I don't know that command. /opt/gitlab/embedded/bin/omnibus-ctl: command (subcommand) deploy-page Put up the deploy page remove-accounts Delete *all* users and groups used by this package upgrade Run migrations after a package upgrade General Commands: cleanse Delete *all* gitlab data, and start from scratch. help Print this help message. reconfigure Reconfigure the application. show-config Show the configuration that would be generated by reconfigure. uninstall Kill all processes and uninstall the process supervisor (data will be preserved). Service Management Commands: graceful-kill Attempt a graceful stop, then SIGKILL the entire process group. hup Send the services a HUP. int Send the services an INT. kill Send the services a KILL. once Start the services if they are down. Do not restart them if they stop. restart Stop the services if they are running, then start them again. service-list List all the services (enabled services appear with a *.) start Start services if they are down, and restart them if they stop. status Show the status of all the services. stop Stop the services, and do not restart them. tail Watch the service logs of all enabled services. term Send the services a TERM.
2.10 gitlab-ce官方文檔查看備錄(重要)
文章地址:http://www.178linux.com/7985
2.11 郵箱設置
1)GitLab中使用postfix進行郵件發送。因此,可以卸載系統中自帶的sendmail。
使用yum list installed查看系統中是否存在sendmail,若存在,則使用yum remove sendmail指令進行卸載。
2)測試系統是否可以正常發送郵件。
echo "Test mail from postfix" | mail -s "Test Postfix" xxx@xxx.com
注:上面的xxx@xxx.com為你希望收到郵件的郵箱地址。
當郵箱收到系統發送來的郵件時,將系統的地址復制下來,如:root@iZ23syflhhzZ.localdomain,打開/etc/gitlab/gitlab.rb,將
# gitlab_rails['gitlab_email_from'] = 'gitlab@example.com'
修改為
gitlab_rails['gitlab_email_from'] = 'root@iZ23syflhhzZ.localdomain'
保存后,執行sudo gitlab-ctl reconfigure重新編譯GitLab。如果郵箱的過濾功能較強,請添加系統的發件地址到郵箱的白名單中,防止郵件被過濾。
Note:系統中郵件發送的日志可通過`tail /var/log/maillog`命令進行查看。
2.12 開啟HTTPS
2.13 安裝過程出現的問題
1)在瀏覽器中訪問GitLab出現502錯誤
原因:內存不足。
解決辦法:檢查系統的虛擬內存是否隨機啟動了,如果系統無虛擬內存,則增加虛擬內存,再重新啟動系統。
2)80端口沖突
原因:Nginx默認使用了80端口。
解決辦法:為了使Nginx與Apache能夠共存,並且為了簡化GitLab的URL地址,Nginx端口保持不變,修改Apache的端口為4040。這樣就可以直接用使用ip訪問Gitlab。而禪道則可以使用4040端口進行訪問,像這樣:xxx.xx.xxx.xx:4040/zentao。具體修改的地方在/etc/httpd/conf/httpd.conf這個文件中,找到Listen 80這一句並將之注釋掉,在底下添加一句Listen 4040,保存后執行service httpd restart重啟apache服務即可。
#Listen 80
Listen 4040
3)8080端口沖突
原因:由於unicorn默認使用的是8080端口。
解決辦法:打開/etc/gitlab/gitlab.rb,打開# unicorn['port'] = 8080的注釋,將8080修改為9090,保存后運行sudo gitlab-ctl reconfigure即可。
4)STMP設置
配置無效,暫時不知道原因。
5)GitLab頭像無法正常顯示
原因:gravatar被牆
解決辦法:
編輯 /etc/gitlab/gitlab.rb,將
#gitlab_rails['gravatar_plain_url'] = 'http://gravatar.duoshuo.com/avatar/%{hash}?s=%{size}&d=identicon'
修改為:
gitlab_rails['gravatar_plain_url'] = 'http://gravatar.duoshuo.com/avatar/%{hash}?s=%{size}&d=identicon'
然后在命令行執行:
gitlab-ctl reconfigure
gitlab-rake cache:clear RAILS_ENV=production