前言
GitLab Community Edition 新版本已經(jīng)集成了CI/CD的支持腋颠,從某種程度上來說可以告別對于Jenkins的依賴肥哎,我本來計劃是寫基于Docker部署GitLab但感覺還是有點重冲粤,對于內(nèi)網(wǎng)離線環(huán)境來說基于容器部署也未必是合理的方案,這里沿用傳統(tǒng)的部署方式介紹簡單且長期有效的GitLab部署方案供大家參考访忿,或許未來會增加基于容器的部署方案和CI/CD的分享是牢,因需而變。
RHEL7/CentOS7在線和離線安裝GitLab配置實踐礁击,GitLab漢化配置使用小結(jié)
更新記錄
2018年04月10日 - 初稿
閱讀原文 - https://wsgzao.github.io/post/gitlab/
擴展閱讀
GitLab Installation - https://about.gitlab.com/installation
GitLab簡介
GitLab是利用Ruby On Rails開發(fā)的一個開源版本管理系統(tǒng)盐杂,實現(xiàn)了一個自托管的Git項目倉庫,是集代碼托管哆窿,測試链烈,部署于一體的開源git倉庫管理軟件,可通過web界面來進行訪問公開的或私人項目更耻。與Github類似测垛,GitLab能夠瀏覽代碼,管理缺陷和注釋秧均∈澄辏可以管理團隊對倉庫的訪問号涯,它非常易于瀏覽提交過的版本,并提供一個文件歷史庫锯七。它還提供一個代碼片段收集功能可以輕松實現(xiàn)代碼復(fù)用链快,便于日后需要的時候查找。
Git的家族成員
- Git:是一種版本控制系統(tǒng)眉尸,是一個命令域蜗,是一種工具。
- Gitlib:是用于實現(xiàn)Git功能的開發(fā)庫噪猾。
- Github:是一個基于Git實現(xiàn)的在線代碼托管倉庫霉祸,公開項目是免費的,也可以付費創(chuàng)建私人項目袱蜡。
- GitLab:是一個基于Git實現(xiàn)的在線代碼倉庫托管軟件丝蹭,可以用GitLab搭建一套類似Github的系統(tǒng)。
GitLab對硬件還是有一定要求的坪蚁,1核心的CPU基本上可以滿足需求奔穿,大概支撐100個左右的用戶,不過在運行GitLab網(wǎng)站的同時還需要運行多個后臺job敏晤,就會顯得有點捉襟見肘了贱田。需要至少4GB的可尋址內(nèi)存(RAM交換)來安裝和使用GitLab,操作系統(tǒng)和任何其他正在運行的應(yīng)用程序也將使用內(nèi)存嘴脾,因此請記住男摧,在運行GitLab之前,您至少需要4GB的可用空間译打。如果使用更少的內(nèi)存彩倚,GitLab將在重新配置運行期間給出奇怪的錯誤,我用虛擬機來分別新建1G,2G內(nèi)存的CentOS系統(tǒng)來裝GitLab扶平,確實非常捉襟見肘啊,傷不起蔬蕊。
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ù)器上面的
GitLab工作流程
[圖片上傳失敗...(image-fdb67f-1523359408993)]
GitLab安裝
在線安裝gitlab-ce
# 安裝必要的依賴包,如果不需要可以考慮跳過
yum install pygpgme yum-utils
# On CentOS 7 (and RedHat/Oracle/Scientific Linux 7), the commands below will also open HTTP and SSH access in the system firewall.
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld
# Next, install Postfix to send notification emails. If you want to use another solution to send emails please skip this step and configure an external SMTP server after GitLab has been installed.
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
# 使用阿里云作加速
cd /etc/yum.repos.d/ && rm -f *.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 添加GitLab鏡像源并安裝岸夯,建議切換國內(nèi)資源加速訪問
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# 創(chuàng)建gitlab-ce的repo麻献,使用清華大學(xué)加速
vim /etc/yum.repos.d/gitlab_gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
#配置并啟動 GitLab
gitlab-ctl reconfigure
# 第一次訪問GitLab,系統(tǒng)會重定向頁面到重定向到重置密碼頁面猜扮,你需要輸入初始化管理員賬號的密碼勉吻,管理員的用戶名為root,重置密碼后新密碼即為剛輸入的密碼旅赢。
0.0.0.0:80
離線安裝gitlab-ce
# 使用阿里云作加速
cd /etc/yum.repos.d/ && rm -f *.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 添加GitLab鏡像源并安裝齿桃,建議切換國內(nèi)資源加速訪問
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# 創(chuàng)建gitlab-ce的repo惑惶,使用清華大學(xué)加速
vim /etc/yum.repos.d/gitlab_gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
#安裝yum-plugin-downloadonly插件
yum install -y yum-plugin-downloadonly
# 下載gitlab-ce相關(guān)rpm到指定目錄
mkdir -p /tmp/repo/gitlab-ce/
yum install --downloadonly --downloaddir=/tmp/repo/gitlab-ce/ gitlab-ce
# 拷貝文件至內(nèi)網(wǎng)離線安裝
rpm -ivh /tmp/repo/gitlab-ce/*
#配置并啟動 GitLab
gitlab-ctl reconfigure
# 第一次訪問GitLab,系統(tǒng)會重定向頁面到重定向到重置密碼頁面短纵,你需要輸入初始化管理員賬號的密碼带污,管理員的用戶名為root,重置密碼后新密碼即為剛輸入的密碼香到。
0.0.0.0:80
GitLab漢化
如果團隊里英文水平都不錯的話鱼冀,是沒必要漢化的,我個人的建議是堅持使用原版不做漢化
GitLab中文社區(qū)的項目悠就,v7-v8.8是由Larry Li發(fā)起的“GitLab中文社區(qū)版項目”
https://gitlab.com/larryli/gitlab
從v8.9之后由@xhang開始繼續(xù)漢化項目
https://gitlab.com/xhang/gitlab
# 如沒安裝git千绪,需提前安裝
yum install -y git
# 創(chuàng)建gitlba漢化包下載目錄
mkdir -p /tmp/gitlab
cd /tmp/gitlab
# 下載最新的漢化包
git clone https://gitlab.com/xhang/gitlab.git
# 如果是要下載老版本的漢化包,需要加上老版本的分支梗脾,如果想下載10.0.2荸型,可以運行如下語句
git clone https://gitlab.com/xhang/gitlab.git -b v10.0.2-zh
# 停止GitLab并執(zhí)行如下語句
gitlab-ctl stop
cp /tmp/gitlab/gitlab/* /opt/gitlab/embedded/service/gitlab-rails/ -rf
# 復(fù)制時可能不斷提示是否要覆蓋,這時可能是系統(tǒng)每次執(zhí)行cp命令時藐唠,其實是執(zhí)行了cp -i命令的別名帆疟。出現(xiàn)這種情況可以修改~/.bashrc,在“alias cp=’cp-i’”前加#注釋即可宇立。使用命令或者注銷登錄即可
source ~/.bashrc
# 接下來可以重新配置和啟動
gitlab-ctl reconfigure
gitlab-ctl restart
GitLab命令
# 語法
gitlab-ctl command (subcommand)
Service Management Commands
start # 啟動所有服務(wù)
stop # 關(guān)閉所有服務(wù)
restart # 重啟所有服務(wù)
status # 查看所有服務(wù)狀態(tài)
tail # 查看日志信息
service-list # 列舉所有啟動服務(wù)
graceful-kill # 平穩(wěn)停止一個服務(wù)
# 啟動所有服務(wù)
gitlab-ctl start
# 啟動單獨一個服務(wù)
gitlab-ctl start nginx
#查看日志踪宠,查看所有日志
gitlab-ctl tail
#查看具體一個日志,類似tail -f
gitlab-ctl tail nginx
General Commands
help # 幫助
reconfigure # 修改配置文件之后,需要重新加載下
show-config # 查看所有服務(wù)配置文件信息
uninstall # 卸載這個軟件
cleanse # 刪除gitlab數(shù)據(jù)妈嘹,重新白手起家
# 顯示所有服務(wù)配置文件
gitlab-ctl show-config
# 卸載gitlab
gitlab-ctl uninstall
QQ郵箱配置
默認(rèn)情況下柳琢,GitLab用qq郵箱注冊是發(fā)不出確認(rèn)郵件的。查看了網(wǎng)上很多郵箱配置的教程润脸,大部分都是誤導(dǎo)的柬脸。像這類軟件,歸根到底總結(jié)為一句話:一切以官網(wǎng)文檔為準(zhǔn)毙驯。QQ郵箱最好用企業(yè)郵箱倒堕,本人用個人郵箱進行測試是有些小問題的,正確配置如下:
# 編輯gitlab.rb
vim /etc/gitlab/gitlab.rb
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "xxxx@xx.com"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = 'xxxx@xx.com'
GitLab使用
登錄GitLab
- 在瀏覽器的地址欄中輸入IP即可登錄GitLab的界面爆价,老版本第一次登錄使用的用戶名和密碼為
root
和5iveL!fe
- 首次登錄會強制用戶修改密碼垦巴。密碼修改成功后,輸入新密碼進行登錄
創(chuàng)建Project
1.安裝Git工具linux:安裝Git铭段,使用自帶的源安裝
yum install git
2.生成密鑰文件:使用ssh-keygen生成密鑰文件.ssh/id_rsa.pub骤宣。
ssh-keygen -t rsa
3.在GitLab的主頁中新建一個Project
4.添加ssh key導(dǎo)入步驟2中生成的密鑰文件內(nèi)容:
Profile Settings => SSH Keys => Add SSH key
簡單配置
# 配置使用Git倉庫的人員姓名
git config --global user.name "wangao"
# 配置使用Git倉庫的人員email,填寫自己的公司郵箱
git config --global user.email "wangao@test.com"
# 克隆項目序愚,在本地生成同名目錄憔披,并且目錄中會有所有的項目文件
git clone git@172.28.70.126:root/test.git
# 進入到項目目錄
cd test/
# 創(chuàng)建需要上傳到GitLab中的目標(biāo)文件
echo "test" > test.sh
# 將test.sh文件加入到索引中
git add test.sh
# 將test.sh提交到本地倉庫
git commit -m "test.sh"
# 將文件同步到GitLab服務(wù)器上
git push -u origin master
# 在網(wǎng)頁中查看上傳的test.sh文件已經(jīng)同步到GitLab中