概述
- GitLab 是一個(gè)用于倉(cāng)庫管理系統(tǒng)的開源項(xiàng)目,使用Git作為代碼管理工具,并在此基礎(chǔ)上搭建起來的web服務(wù)。
環(huán)境準(zhǔn)備
- 操作系統(tǒng) CentOS7.6.1810
鏡像下載地址: http://mirrors.nju.edu.cn/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso
GitLab搭建
1. 安裝和配置必要的依賴
首先,在系統(tǒng)控制臺(tái)執(zhí)行以下命令打開系統(tǒng)防火墻的http和ssh訪問
[root@localhost ~]# sudo yum install -y curl policycoreutils-python openssh-server
[root@localhost ~]# sudo systemctl enable sshd
[root@localhost ~]# sudo systemctl start sshd
[root@localhost ~]# sudo firewall-cmd --permanent --add-service=http
[root@localhost ~]# sudo systemctl reload firewalld
接下來,安裝Postfix,用于發(fā)送郵件通知。如果想采用其他方式來發(fā)送郵件,可以跳過此步驟,在GitLab安裝完的時(shí)候,再進(jìn)行配置。
[root@localhost ~]# sudo yum install postfix
[root@localhost ~]# sudo systemctl enable postfix
[root@localhost ~]# sudo systemctl start postfix
2.添加GitLab package倉(cāng)庫和安裝GitLab的package
添加package倉(cāng)庫
[root@localhost ~]# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
安裝GitLab的package
[root@localhost ~]# sudo yum install -y gitlab-ee
3.修改GitLab配置
配置文件路徑:/etc/gitlab/gitlab.rb
找到 external_url,修改為想要的域名
external_url = http://gitlab.whenling.com
4.啟動(dòng)GitLab
[root@localhost ~]# sudo gitlab-ctl reconfigure
5.訪問GitLab
訪問地址(GitLab服務(wù)器ip的80端口): http://192.168.1.177

GitLab配置
采用其他端口
修改配置文件/etc/gitlab/gitlab.rb
external_url = http://gitlab.whenling.com:8888
nginx['listen_port'] = 8888
保存退出,讓GitLab執(zhí)行重新配置且重啟服務(wù)
[root@localhost ~]# sudo gitlab-ctl reconfigure
[root@localhost ~]# sudo gitlab-ctl restart
注意開放對(duì)應(yīng)的防火墻端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=8888/tcp --permanent
[root@localhost ~]# sudo systemctl reload firewalld
GitLab郵件服務(wù)
修改配置文件:/etc/gitlab/gitlab.rb
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "375391531@qq.com"
gitlab_rails['smtp_password'] = "ddvwqdmbiwkubjia"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
user['git_user_email'] = "375391531@qq.com"
gitlab_rails['gitlab_email_from'] = '375391531@qq.com'
保存退出,讓GitLab執(zhí)行重新配置
[root@localhost ~]# sudo gitlab-ctl reconfigure
測(cè)試郵件發(fā)送
進(jìn)入控制臺(tái):
[root@localhost ~]# gitlab-rails console
執(zhí)行以下命令
irb(main):001:0> Notify.test_email('156801554@qq.com','the title', 'the content').deliver_now

GitLab使用
創(chuàng)建一個(gè)項(xiàng)目
采用root賬號(hào)登錄

測(cè)試本地是否能提交到GitLab
在本地創(chuàng)建一個(gè)文件夾,初始化git倉(cāng)庫
kongxiangxis-MacBook-Pro:~ kongxiangxi$ mkdir test
kongxiangxis-MacBook-Pro:~ kongxiangxi$ cd test
kongxiangxis-MacBook-Pro:test kongxiangxi$ git init
添加一個(gè)文件
kongxiangxis-MacBook-Pro:test kongxiangxi$ echo "hello world" >> file1
kongxiangxis-MacBook-Pro:test kongxiangxi$ git add *
kongxiangxis-MacBook-Pro:test kongxiangxi$ git commit -m "test"
配置遠(yuǎn)程倉(cāng)庫
kongxiangxis-MacBook-Pro:test kongxiangxi$ git remote add origin http://gitlab.whenling.com:8888/root/gitlab-example.git
kongxiangxis-MacBook-Pro:test kongxiangxi$ git branch --set-upstream-to=origin/master master
拉取遠(yuǎn)程文件
kongxiangxis-MacBook-Pro:test kongxiangxi$ git pull origin master --allow-unrelated-histories
提交本地修改到遠(yuǎn)程倉(cāng)庫
kongxiangxis-MacBook-Pro:test kongxiangxi$ git push --set-upstream origin master
GitLab注冊(cè)需要驗(yàn)證郵箱

選擇后需要點(diǎn)擊下面的 Save Changes 按鈕
采用SSH方式
創(chuàng)建公鑰和私鑰
kongxiangxis-MacBook-Pro:~ kongxiangxi$ ssh-keygen -t rsa

私鑰保存于 /Users/kongxiangxi/.ssh/id_rsa.
公鑰保存于 /Users/kongxiangxi/.ssh/id_rsa.pub.
把公鑰的內(nèi)容添加到GitLab
測(cè)試ssh方式提交
kongxiangxis-MacBook-Pro:~ kongxiangxi$ mkdir testssh
kongxiangxis-MacBook-Pro:~ kongxiangxi$ cd testssh/
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git init
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git remote add origin git@gitlab.whenling.com:dev/group-test.git
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ echo "sss" >> file2.txt
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git add *
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git commit -m "Initial commit"
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git push -u origin master