RHEL7/CentOS7線上和離線安裝GitLab配置使用實踐

wsgzao發表於2018-04-10

前言

GitLab Community Edition 新版本已經整合了CI/CD的支援,從某種程度上來說可以告別對於Jenkins的依賴,我本來計劃是寫基於Docker部署GitLab但感覺還是有點重,對於內網離線環境來說基於容器部署也未必是合理的方案,這裡沿用傳統的部署方式介紹簡單且長期有效的GitLab部署方案供大家參考,或許未來會增加基於容器的部署方案和CI/CD的分享,因需而變。

RHEL7/CentOS7線上和離線安裝GitLab配置實踐,GitLab漢化配置使用小結

更新記錄

2018年04月10日 - 初稿

閱讀原文 - https://wsgzao.github.io/post/gitlab/

擴充套件閱讀

GitLab Installation - https://about.gitlab.com/installation

GitLab簡介

GitLab是利用Ruby On Rails開發的一個開源版本管理系統,實現了一個自託管的Git專案倉庫,是集程式碼託管,測試,部署於一體的開源git倉庫管理軟體,可通過web介面來進行訪問公開的或私人專案。與Github類似,GitLab能夠瀏覽程式碼,管理缺陷和註釋。可以管理團隊對倉庫的訪問,它非常易於瀏覽提交過的版本,並提供一個檔案歷史庫。它還提供一個程式碼片段收集功能可以輕鬆實現程式碼複用,便於日後需要的時候查詢。

Git的家族成員

  • Git:是一種版本控制系統,是一個命令,是一種工具。
  • Gitlib:是用於實現Git功能的開發庫。
  • Github:是一個基於Git實現的線上程式碼託管倉庫,公開專案是免費的,也可以付費建立私人專案。
  • GitLab:是一個基於Git實現的線上程式碼倉庫託管軟體,可以用GitLab搭建一套類似Github的系統。

GitLab對硬體還是有一定要求的,1核心的CPU基本上可以滿足需求,大概支撐100個左右的使用者,不過在執行GitLab網站的同時還需要執行多個後臺job,就會顯得有點捉襟見肘了。需要至少4GB的可定址記憶體(RAM交換)來安裝和使用GitLab,作業系統和任何其他正在執行的應用程式也將使用記憶體,因此請記住,在執行GitLab之前,您至少需要4GB的可用空間。如果使用更少的記憶體,GitLab將在重新配置執行期間給出奇怪的錯誤,我用虛擬機器來分別新建1G,2G記憶體的CentOS系統來裝GitLab,確實非常捉襟見肘啊,傷不起。

Gitlab的服務構成

  • Nginx:靜態web伺服器
  • gitlab-shell:用於處理Git命令和修改authorized keys列表
  • gitlab-workhorse: 輕量級的反向代理伺服器
  • logrotate:日誌檔案管理工具
  • postgresql:資料庫
  • redis:快取資料庫
  • sidekiq:用於在後臺執行佇列任務(非同步執行)
  • unicorn:An HTTP server for Rack applications,GitLab Rails應用是託管在這個伺服器上面的

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映象源並安裝,建議切換國內資源加速訪問
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

# 建立gitlab-ce的repo,使用清華大學加速
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,系統會重定向頁面到重定向到重置密碼頁面,你需要輸入初始化管理員賬號的密碼,管理員的使用者名稱為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映象源並安裝,建議切換國內資源加速訪問
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

# 建立gitlab-ce的repo,使用清華大學加速
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相關rpm到指定目錄
mkdir -p /tmp/repo/gitlab-ce/
yum install --downloadonly --downloaddir=/tmp/repo/gitlab-ce/ gitlab-ce

# 拷貝檔案至內網離線安裝
rpm -ivh /tmp/repo/gitlab-ce/*

#配置並啟動 GitLab
gitlab-ctl reconfigure

# 第一次訪問GitLab,系統會重定向頁面到重定向到重置密碼頁面,你需要輸入初始化管理員賬號的密碼,管理員的使用者名稱為root,重置密碼後新密碼即為剛輸入的密碼。
0.0.0.0:80

複製程式碼

GitLab漢化

如果團隊裡英文水平都不錯的話,是沒必要漢化的,我個人的建議是堅持使用原版不做漢化

GitLab中文社群的專案,v7-v8.8是由Larry Li發起的“GitLab中文社群版專案” https://gitlab.com/larryli/gitlab

從v8.9之後由@xhang開始繼續漢化專案 https://gitlab.com/xhang/gitlab

# 如沒安裝git,需提前安裝
yum install -y git

# 建立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並執行如下語句
gitlab-ctl stop
cp /tmp/gitlab/gitlab/* /opt/gitlab/embedded/service/gitlab-rails/ -rf

# 複製時可能不斷提示是否要覆蓋,這時可能是系統每次執行cp命令時,其實是執行了cp -i命令的別名。出現這種情況可以修改~/.bashrc,在“alias cp=’cp-i’”前加#註釋即可。使用命令或者登出登入即可
source ~/.bashrc

# 接下來可以重新配置和啟動
gitlab-ctl reconfigure
gitlab-ctl restart
複製程式碼

GitLab命令

# 語法
gitlab-ctl command (subcommand)

Service Management Commands

start # 啟動所有服務
stop # 關閉所有服務
restart # 重啟所有服務
status # 檢視所有服務狀態
tail # 檢視日誌資訊
service-list # 列舉所有啟動服務
graceful-kill # 平穩停止一個服務

# 啟動所有服務
gitlab-ctl start 
# 啟動單獨一個服務
gitlab-ctl start nginx
#檢視日誌,檢視所有日誌
gitlab-ctl tail
#檢視具體一個日誌,類似tail -f
gitlab-ctl tail nginx

General Commands

help # 幫助
reconfigure # 修改配置檔案之後,需要重新載入下
show-config # 檢視所有服務配置檔案資訊
uninstall # 解除安裝這個軟體
cleanse # 刪除gitlab資料,重新白手起家

# 顯示所有服務配置檔案
gitlab-ctl show-config

# 解除安裝gitlab
gitlab-ctl uninstall
複製程式碼

QQ郵箱配置

預設情況下,GitLab用qq郵箱註冊是發不出確認郵件的。檢視了網上很多郵箱配置的教程,大部分都是誤導的。像這類軟體,歸根到底總結為一句話:一切以官網文件為準。QQ郵箱最好用企業郵箱,本人用個人郵箱進行測試是有些小問題的,正確配置如下:

# 編輯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

  1. 在瀏覽器的位址列中輸入IP即可登入GitLab的介面,老版本第一次登入使用的使用者名稱和密碼為 root5iveL!fe
  2. 首次登入會強制使用者修改密碼。密碼修改成功後,輸入新密碼進行登入

建立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匯入步驟2中生成的金鑰檔案內容:

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/ 
# 建立需要上傳到GitLab中的目標檔案
echo "test" > test.sh
# 將test.sh檔案加入到索引中
git add test.sh
# 將test.sh提交到本地倉庫
git commit -m "test.sh"
# 將檔案同步到GitLab伺服器上
git push -u origin master
# 在網頁中檢視上傳的test.sh檔案已經同步到GitLab中

複製程式碼

相關文章