持續集成之Gitlab安裝與應用

Gitlab 是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自託管的 Git 項目倉庫,可經過Web 界面進行訪問公開的或者私人的項目 Gitlab 擁有與 Github 相似的功能,可以瀏覽源代碼,管理缺陷和註釋。能夠管理團隊對倉庫的訪問,他很是易於瀏覽提交過的版本並提供一個文件歷史庫。他還提供一個代碼片斷收集功能能夠輕鬆實現代碼複用,便於往後有須要的時候進行查找nginx

1、環境準備

若是是測試環境,其內存建議2G及以上,能夠去清華開源鏡像站下載所需gitlab版本,其安裝後,會自動安裝nginx提供web界面,因此要避免80端口占用。git

2、安裝部署gitlab

1. 安裝gitlabgithub

[root@git /]# mkdir git
[root@git /]# cd git/
[root@git git]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm
[root@git git]# rpm -ivh gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm 
#當gitlab安裝完畢後會有一個大狐狸頭
#因爲我不打算作域名解析,因此須要修改其配置文件
[root@git git]# vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.171.134'                # 將本來的域名改成本機IP
[root@git /]# gitlab-ctl reconfigure                 #從新配置gitlab,就算不修改配置文件,也須要在安裝後從新配置gitlab,此處會等一會
[root@git /]# netstat -anput | grep -w 80          #肯定nginx在監聽80端口

2.配置gitlab
客戶端訪問服務器的IP地址,能夠看到如下界面(配置密碼並登錄):
持續集成之Gitlab安裝與應用
持續集成之Gitlab安裝與應用
上傳服務器公鑰(接下來的操做與在github上大同小異),先在服務器上生成密鑰對:web

[root@git /]# ssh-keygen -t rsa -C "848369866@qq.com"
[root@git /]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7mTAcNqGbsRoPPAd2M+xfdVfsa4lLPVs37WHbM+iept0DdSIgVaoz++w4oHTccWcH6K5hvCJo5AvTzIuSn3rAV8E6sROL2yCafcLE+//rSpW5/cenvsuYhNV5jkqfuCs1moVev0BHnW//9XZJpyQFIQ8JNlASFGmRXOycNuP9NeacSo5IoXjRzHTA28674+LZwo6D6+klfAYo75rD7JQ61DhrAEq/xBHQ+5zigo6i95xsRdUMPpKRsb0fruTBYUC0jwNfKzgal1CuGTmqJ+l9MI4cnbHNYk2TYP74NgcUYHfp0n6Lr7HkUDAtsyGbT9zPfatU3nVOihN8efdupb8d 848369866@qq.com

而後回到web界面:
持續集成之Gitlab安裝與應用
持續集成之Gitlab安裝與應用
添加後以下:
持續集成之Gitlab安裝與應用
建立一個庫:
持續集成之Gitlab安裝與應用
持續集成之Gitlab安裝與應用
持續集成之Gitlab安裝與應用
持續集成之Gitlab安裝與應用
回到服務器上進行克隆剛剛建立的庫:sql

[root@git /]# git clone git@192.168.171.134:root/test1.git           # 進行克隆
[root@git /]# cd test1/                 # 能夠看到這裏和剛纔庫裏的東西同樣
[root@git test1]# ls
README.md
#定義用戶名及email
[root@git test1]# git config --global user.name "test"
[root@git test1]# git config --global user.email "test@test.com"
#建立測試文件,並推送到遠端庫中測試
[root@git test1]# echo "aaaa" > test.txt
[root@git test1]# git add test.txt        
[root@git test1]# git commit -m "alter from 192.168.171.134"
[master 81dc1f8] alter from 192.168.171.134
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@git test1]# git push origin master

刷新web界面的庫頁面:
持續集成之Gitlab安裝與應用vim

3、遠端庫的基本操做

當你從遠端倉庫克隆時,實際上git自動把本地的master分支和遠端的master分支對應起來了,而且遠程倉庫的默認名稱是origin。
要查看遠程庫的信息,使用如下命令:服務器

[root@git test1]# git remote                  # 簡略信息
origin
[root@git test1]# git remote -v                # 詳細信息
origin  git@192.168.171.134:root/test1.git (fetch) 
origin  git@192.168.171.134:root/test1.git (push)

推送分支:ssh

[root@git test1]# git push origin master        #推送本地的master分支
[root@git test1]# git push origin dev            #推送本地的dev分支,若遠端沒有dev分支,會自動建立

抓取分支:ide

[root@git test1]# git pull origin dev          #根據提示將遠端的dev分支抓取下來

當咱們從遠程庫克隆時,默認狀況下,只能看到master分支,可使用git branch命令確認。
解決多人協做容易產生的問題
當咱們整個小組對同一個分支進行開發時,若是在你提交以前,你的同事已經修改了分支的內容並推送到遠端倉庫,而碰巧你也對一樣的文件作了修改,並試圖推送,那麼會推送失敗,由於你的同事的最新提交的數據和你試圖提交的數據有衝突(你本地的內容比遠端倉庫的舊了),解決的辦法會在提示你推送失敗的返回信息中給出,這裏咱們模擬一下這一過程。gitlab

[root@git /]# mkdir test2
[root@git /]# cd test2/
[root@git test2]# git clone git@192.168.171.134:root/test1.git
[root@git test2]# cd test1/
[root@git test1]# git checkout -b dev 
Switched to a new branch 'dev'
[root@git test1]# echo "bbbb" > tmp.txt
[root@git test1]# git add tmp.txt
[root@git test1]# git commit -m "commit from /test2"
[dev ba44ec3] commit from /test2
 1 file changed, 1 insertion(+)
 create mode 100644 tmp.txt
[root@git test1]# git push origin dev

回到web界面進行刷新,便可看到新提交的分支:
持續集成之Gitlab安裝與應用
上面的操做是在/test2目錄下進行操做的,那麼如今操做/root目錄下的遠程倉庫:

[root@git /]# cd root/test1/                #進入root目錄下的遠程倉庫並建立dev分支,推送內容至遠端倉庫
[root@git test1]# git checkout -b dev 
Switched to a new branch 'dev'
[root@git test1]# echo "ccccc" > root.txt
[root@git test1]# git add root.txt
[root@git test1]# git commit -m "commit from /root"
[dev f8fd78d] commit from /root
 1 file changed, 1 insertion(+)
 create mode 100644 root.txt
[root@git test1]# git push origin dev             #此時咱們推送,就會提示如下錯誤
To git@192.168.171.134:root/test1.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@192.168.171.134:root/test1.git' 
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
#沒法推送一些引用到'git@192.168.171.134:root/test1.git' 
#提示遠程版本庫有咱們本地版本庫沒有的提交,因此須要先將遠端版本庫pull下來,再提交
[root@git test1]# git pull origin dev            #根據提示將遠端的dev分支pull下來
[root@git test1]# ls
README.md  root.txt  test.txt  tmp.txt
[root@git test1]# git push origin dev           #而後再次將本地的dev分支推送到gitlab,便可成功

此時,web界面的dev分支就有了咱們在/test2目錄和/root目錄下提交的全部內容,以下:
持續集成之Gitlab安裝與應用
可是master分支,仍然是最初的文件,以下:
持續集成之Gitlab安裝與應用
如今進行遠程版本庫的分支合併,以下:

[root@git test1]# git checkout master            # 切換到master分支
Switched to branch 'master'
[root@git test1]# git merge dev            # 合併dev分支
Updating 81dc1f8..386d906
Fast-forward
 root.txt | 1 +
 tmp.txt  | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 root.txt
 create mode 100644 tmp.txt
[root@git test1]# git pull 
Already up-to-date.
[root@git test1]# git add *
[root@git test1]# git commit -m "提交"
[root@git test1]# git push origin master          # 推送到遠端庫

持續集成之Gitlab安裝與應用
如今已經擁有了dev分支的全部內容,那麼接下來就演示如何刪除遠程版本庫的dev分支:

[root@git test1]# git branch -d dev              # 刪除本地的dev分支
Deleted branch dev (was 386d906).
[root@git test1]# git branch -r -d origin/dev           # #刪除指定的遠程分支 
Deleted remote branch origin/dev (was 386d906).
[root@git test1]# git push origin :dev          # #將刪除的分支提交到遠程版本庫中
To git@192.168.171.134:root/test1.git
 - [deleted]         dev

至此,遠端版本庫中的dev分支就被刪除了,以下:
持續集成之Gitlab安裝與應用

4、重置gitlab管理員密碼

[root@git /]# gitlab-rails console production          #執行該命令,只有第一個命令字能夠tab出來
-------------------------------------------------------------------------------------
 GitLab:       11.9.8 (48528bc)
 GitLab Shell: 8.7.1
 postgresql:   9.6.11
-------------------------------------------------------------------------------------
Loading production environment (Rails 5.0.7.1)
irb(main):001:0> user = User.where(id:1).first
=> #<User id:1 @root>
irb(main):002:0> user.password='test1234'
=> "test1234"
irb(main):003:0> user.password_confirmation='test1234'
=> "test1234"
irb(main):004:0> user.save
Enqueued ActionMailer::DeliveryJob (Job ID: 86d1357c-d974-4bd9-ad0c-03b4496d9327) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f33612fa170 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):005:0> true
=> true
irb(main):006:0> exit

至此,再次登陸,就須要使用新密碼test1234進行登陸了。

相關文章
相關標籤/搜索