1. GitLab簡介
GitLab 是一個(gè)用于倉庫管理系統(tǒng)的開源項(xiàng)目贤姆。使用Git作為代碼管理工具,并在此基礎(chǔ)上搭建起來的web服務(wù)并思÷可通過Web界面進(jìn)行訪問公開的或者私人項(xiàng)目。它擁有與Github類似的功能宋彼,能夠?yàn)g覽源代碼弄砍,管理缺陷和注釋∈涮椋可以管理團(tuán)隊(duì)對(duì)倉庫的訪問音婶,它非常易于瀏覽提交過的版本并提供一個(gè)文件歷史庫。團(tuán)隊(duì)成員可以利用內(nèi)置的簡單聊天程序(Wall)進(jìn)行交流莱坎。它還提供一個(gè)代碼片段收集功能可以輕松實(shí)現(xiàn)代碼復(fù)用衣式。
常用的網(wǎng)站:
官網(wǎng):https://about.gitlab.com/
國內(nèi)鏡像:https://mirrors.tuna.tsinghua.edu.cn/gitlab‐ce/yum/
安裝環(huán)境:
1、 CentOS 6或者7
2、 2G內(nèi)存(實(shí)驗(yàn))生產(chǎn)(至少4G)
3碴卧、 安裝包:gitlab‐ce‐10.2.2‐ce
4弱卡、 禁用防火墻,關(guān)閉selinux
2. 安裝配置
https://about.gitlab.com/installation/#centos‐7 # git官網(wǎng)
yum install ‐y curl policycoreutils‐python openssh‐server # 安裝依賴
2.1 官方安裝文檔
gitlab官網(wǎng):https://about.gitlab.com/
選擇需要在那個(gè)系統(tǒng)上安裝住册,我們這里選擇CentOS7
2.2 安裝和配置文件
[root@git ~]#: rpm -ivh gitlab-ce-11.9.9-ce.0.el7.x86_64.rpm
警告:gitlab-ce-11.9.9-ce.0.el7.x86_64.rpm: 頭V4 RSA/SHA1 Signature, 密鑰 ID f27eab47: NOKEY
準(zhǔn)備中... ################################# [100%]
正在升級(jí)/安裝...
1:gitlab-ce-11.9.9-ce.0.el7 ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.
*. *.
*** ***
***** *****
.****** *******
******** ********
,,,,,,,,,***********,,,,,,,,,
,,,,,,,,,,,*********,,,,,,,,,,,
.,,,,,,,,,,,*******,,,,,,,,,,,,
,,,,,,,,,*****,,,,,,,,,.
,,,,,,,****,,,,,,
.,,,***,,,,
,*,.
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __ \
/ /_/ / / /_/ /___/ /_/ / /_/ /
\____/_/\__/_____/\__,_/_.___/
Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url` 配置你的URL在external_url選項(xiàng)里
configuration in /etc/gitlab/gitlab.rb file. 通過這個(gè)文件里配置你的URL
Then, you can start your GitLab instance by running the following command:
sudo gitlab-ctl reconfigure 用這個(gè)命令重新配置gitlab
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
[root@git ~]#: vim /etc/gitlab/gitlab.rb
13 external_url 'http://10.0.0.20' 配置你的ip地址
[root@git ~]#: gitlab-ctl reconfigure
看到這個(gè)結(jié)果就顯示配置成功
Running handlers:
Running handlers complete
Chef Client finished, 473/1268 resources updated in 03 minutes 36 seconds
gitlab Reconfigured!
Gitlab配置文件說明
gitlab‐ctl reconfigure # 更改配置文件后需重新配置
/opt/gitlab/ # gitlab的程序安裝目錄
/var/opt/gitlab # gitlab目錄數(shù)據(jù)目錄
/var/opt/gitlab/git‐dfata # 存放倉庫數(shù)據(jù)
gitlab‐ctl status # 查看目前gitlab所有服務(wù)運(yùn)維狀態(tài)
gitlab‐ctl stop # 停止gitlab服務(wù)
gitlab‐ctl stop nginx # 單獨(dú)停止某個(gè)服務(wù)
gitlab‐ctl tail # 查看所有服務(wù)的日志
Gitlab的服務(wù)構(gòu)成:
nginx: 靜態(tài)web服務(wù)器
gitlab‐workhorse: 輕量級(jí)的反向代理服務(wù)器
logrotate:日志文件管理工具
postgresql:數(shù)據(jù)庫
redis:緩存數(shù)據(jù)庫
sidekiq:用于在后臺(tái)執(zhí)行隊(duì)列任務(wù)(異步執(zhí)行),(Ruby)
unicorn:An HTTP server for Rack applications婶博,GitLab Rails應(yīng)用是托管在這個(gè)服務(wù)器上面的。(Ruby
Web Server,主要使用Ruby編寫)
Gitlab漢化:
1荧飞、下載漢化補(bǔ)丁
[root@git ~]#:git clone https://gitlab.com/xhang/gitlab.git
2凡人、查看版本信息
[root@git ~]#: cat gitlab/VERSION
11.10.0
[root@git ~]#: cd gitlab
3、對(duì)比版本叹阔、生成補(bǔ)丁包
[root@git gitlab]#: git diff v11.10.0 v11.10.0-zh > ../11.10.0-zh.diff
4挠轴、停止服務(wù)器
gitlab‐ctl stop
5、打補(bǔ)丁
patch ‐d /opt/gitlab/embedded/service/gitlab‐rails ‐p1 < /root/11.10.0‐zh.diff
6耳幢、啟動(dòng)和重新配置
gitlab‐ctl start
gitlab‐ctl reconfigure
下面在瀏覽器中輸入剛才配置的URL信息登陸
2.3 設(shè)置外觀
重新登陸一下看看效果
3. Gitlab配置
如果之前沒有g(shù)itlab岸晦,就要按照先創(chuàng)建組再創(chuàng)建項(xiàng)目,創(chuàng)建人員再添加到組里睛藻。也就是說項(xiàng)目屬于組的委煤,把要管理項(xiàng)目的人員也添加到組里,那這個(gè)人員就可以管理這個(gè)項(xiàng)目修档。
3.1 創(chuàng)建一個(gè)組
3.2 創(chuàng)建一個(gè)項(xiàng)目
這個(gè)就跟之前創(chuàng)建github的界面差不多了
3.3 刪除之前github創(chuàng)建的
[root@git ~]#: cd git_data/
[root@git git_data]#: git remote
origin
[root@git git_data]#: git remote remove origin
[root@git git_data]#: git remote
3.4 創(chuàng)建遠(yuǎn)程倉庫
[root@git git_data]#: git remote add origin git@10.0.0.20:test/git_data.git
3.5 添加ssh認(rèn)證并推送代碼
[root@git ~]#: cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC41tBVR4hlbt3UBdRHDZrHlZWPVUtukum6Etseub4N826QhKeZ/BZmtJu/dUgi322odazuObUU6Z1ee5PV37DLREyayrevkcG/VwOycGaq4J78029wMW5Kq1mDvHGlXgw2x5kFl0s+6dtMSECudzuOAZYrS9QgaCP+h/hSB8JKD/rKW56QvEQ9hpK/wRdgF/xNgDao0EPxwjGgALZbIPYzu41jqGjFvavf1Ucu5EicJelw8ZDc6e3MQ8RvioC42vlroC87B/6tsQjLlp5Y1WZFBm9tpzgWoZ2EKhzRROC7LVEbPF98cM1o1DxL5kMfM7T3bP7G1dE8LrQ/3HsNk5NH root@git
[root@git git_data]#: git push -u origin master
The authenticity of host '10.0.0.20 (10.0.0.20)' can't be established.
ECDSA key fingerprint is SHA256:D7wN1reekHLE8oukJlQ+vEI/uqO4ideb7yfInM9VSu8.
ECDSA key fingerprint is MD5:8f:3e:85:3a:bc:5b:c2:e1:e4:d2:76:11:8a:71:b7:c9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.20' (ECDSA) to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 199 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.20:test/git_data.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
我們這么做的目的就是實(shí)現(xiàn)共同開發(fā),讓所有的開發(fā)人員都能連接帶我的gitlab
3.6 開通用戶
設(shè)置密碼
新開一個(gè)窗口用dev用戶登陸
3.7 把用戶添加到組里
4. 開啟另外一臺(tái)jenkins服務(wù)器添加ssh
[root@jenkins ~]#: 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:z7THM9Ox8COx9A/GmTCm/u3kMwZrETXBPdP5+DqL+P4 root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
| ..oo|
| ++o|
| . o+|
| . . .|
| S . B... |
| + *o@ =.|
| = O+&. |
| . oo@*+ |
| o==+E=.|
+----[SHA256]-----+
[root@jenkins ~]#: cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/+BBsKUrJ5dICUQVXOC2EpUdEmzbTJRv78XOWNsE1jvEGTq7yG7cILw2Gij9Nd8cuD7phQ609vW+eJRlZEsBv8T9jNIMcw6gmGs6POIxpjiYKhWCf+NQO4l15HwWF63iD1toT/11LGn7t83hpokF6NWElKxdw1RcGtFO+Fjf90EV9v/XjttOWG2u4kJ+HuNf38XWeiZkGAnpki3vV8jqc9nS9BYDfUsNKTlkUuWstomniycJw+H4TiPEUIxOxz/oQpP5R6CmOwHypca1hXWNDBgf0cXFv90SErTQu0kLGSY60RRM+tKuF5hdFpaUk0m7Oqvc3AHlo6KwNXltMUGpB root@jenkins
添加ssh密鑰
克隆代碼
[root@jenkins ~]#: git clone git@10.0.0.20:test/git_data.git
Cloning into 'git_data'...
The authenticity of host '10.0.0.20 (10.0.0.20)' can't be established.
ECDSA key fingerprint is SHA256:D7wN1reekHLE8oukJlQ+vEI/uqO4ideb7yfInM9VSu8.
ECDSA key fingerprint is MD5:8f:3e:85:3a:bc:5b:c2:e1:e4:d2:76:11:8a:71:b7:c9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.20' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0
4.1 jenkins配置git
[root@jenkins git_data]#: git config --global user.email "dev@email.com"
[root@jenkins git_data]#: git config --global user.name "dev"
推送代碼
[root@jenkins git_data]#: echo 11 > test.txt
[root@jenkins git_data]#: git commit -am 'add'
[root@jenkins git_data]#: git push -u origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To git@10.0.0.20:test/git_data.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@10.0.0.20:test/git_data.git'
測(cè)試環(huán)境下gitlab默認(rèn)開啟了保護(hù)分支府框,不讓dev用戶推送代碼到master主分支上吱窝。
關(guān)閉保護(hù)分支在管理端操作
再次推送
[root@jenkins git_data]#: git push -u origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.20:test/git_data.git
5929a40..ac73233 master -> master
Branch master set up to track remote branch master from origin.
5 保護(hù)分支
不允許開發(fā)用戶直接往master推送。只能推送到dev分支上迫靖,然后進(jìn)行請(qǐng)求合并院峡,用管理員用戶在進(jìn)行代碼合并。
管路員用戶端操作
5.1 創(chuàng)建dev分支推送代碼
[root@jenkins git_data]#: git checkout -b dev
Switched to a new branch 'dev'
[root@jenkins git_data]#: echo 22 >> test.txt
[root@jenkins git_data]#: git commit -am 'add 2'
[dev 2562c60] add 2
1 file changed, 1 insertion(+)
[root@jenkins git_data]#: git push -u origin dev
Counting objects: 5, done.
Writing objects: 100% (3/3), 231 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote: http://10.0.0.20/test/git_data/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To git@10.0.0.20:test/git_data.git
* [new branch] dev -> dev
Branch dev set up to track remote branch dev from origin.
6 合并分支
6.1 第一次合并分支dev用戶操作
管理端
6.2 第二次合并
刪除dev用戶并切換到master
[root@jenkins git_data]#: git checkout master
Switched to branch 'master'
[root@jenkins git_data]#: git branch
dev
* master
[root@jenkins git_data]#: git branch -d dev
warning: deleting branch 'dev' that has been merged to
'refs/remotes/origin/dev', but not yet merged to HEAD.
Deleted branch dev (was 2562c60).
[root@jenkins git_data]#: git branch
* master
拉取新的代碼系宜,在創(chuàng)建因分支前一定要保持master代碼時(shí)最新的
[root@jenkins git_data]#: cat test.txt
11
[root@jenkins git_data]#: git pull
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From 10.0.0.20:test/git_data
ac73233..9fa10c8 master -> origin/master
* [new branch] revert-9fa10c88 -> origin/revert-9fa10c88
Updating ac73233..9fa10c8
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
[root@jenkins git_data]#: cat test.txt
11
22
創(chuàng)建新分支推送代碼
[root@jenkins git_data]#: git checkout -b dev
Switched to a new branch 'dev'
[root@jenkins git_data]#: echo 111 > test.txt
[root@jenkins git_data]#: git commit -am 'add test'
[root@jenkins git_data]#: git push -u origin dev
管理端操作