實戰一:企業級Gitlab部署及配置
Gitlab服務的安裝文檔:https://about.gitlab.com/install/
環境要求:https://docs.gitlab.com/ee/install/requirements.html
國外安裝包下載地址:https://packages.gitlab.com/gitlab/gitlab-ce
rpm包國內下載地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/
已經下載了兩種不同后綴的gitlab安裝包,注意:不要下載最新版本的,最新版本可能存在bug問題,11.11.5,中的5就是修改5次的版本。
gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm #此次安裝此gitlab包 gitlab-ce_11.11.5-ce.0_amd64.ded
1、安裝gitlab的虛擬機配置要求:內存4G,處理器雙核。

2、此次試驗安裝rpm后綴的包,將ded結尾的安裝包放到/usr/local/src/目錄下:
[root@gitlab src]# pwd /usr/local/src [root@gitlab src]# ls gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm
3、開始安裝gitlab
[root@gitlab src]# yum install gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm -y
4、以下是安裝過程自動刪除的目錄:
/etc/gitlab #配置文件目錄 /run/gitlab # 運行pid 目錄 /opt/gitlab #安裝目錄 /var/opt/gitlab # 數據目錄 /var/log/gitlab # 日志 目錄
5、配置gitlab文件:/etc/gitlab/gitlab.rb
external_url 'http://192.168.7.100' # 需要訪問的IP地址,或者解析的域名也可以。 gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "smtp.qq.com" gitlab_rails['smtp_port'] = 465 gitlab_rails['smtp_user_name'] = "974212253qq.com" # 設置開發的郵箱地址,防止忘記密碼需要修改密碼 gitlab_rails['smtp_password'] = "xxx" # 授權碼 gitlab_rails['smtp_domain'] = "qq.com" gitlab_rails['smtp_authentication'] = :login gitlab_rails['smtp_enable_starttls_auto'] = true gitlab_rails['smtp_tls'] = true gitlab_rails['gitlab_email_from'] = "974212253@qq.com" user["git_user_email"] = "974212253@qq.com" # 配置公司郵箱

6、啟動gitlab服務,啟動需要2分鍾左右,啟動后查看80端口是否已經被監聽。
[root@gitlab local]# gitlab-ctl reconfigure

7、訪問gitlab網站,輸入需要訪問的IP地址:192.168.7.100,此時進入頁面就會讓你修改當前的密碼,默認登陸用戶是root,到此gitlab的安裝完成。

實戰二:Gitlab創建項目、克隆文件、上傳代碼
1、創建group組
1、使用管理員root創建組,一個組里面可以有多個項目分支,可以將開發添加到組里面進行設置權限不同的組就是公司不同的開發項目或者服務模塊,
不同的組添加不同的開發即可實現對開發設置權限的管理。

2、創建group組名linux_gitlab,並設置為私有信息

2、創建用戶名
1、創建用戶

2、創建一個user1用戶名。

3、添加用戶名密碼
1、由於新建的user1用戶沒有密碼,需要我們去創建,第一種創建是選擇Edit修改,設置user1密碼。

2、第二種方法修改gitlab密碼,輸入創建的user1用戶,點擊忘記密碼,然后跳轉到需要輸入的郵箱地址之后,就會發送郵件對其當前賬號進行修改密碼。

4、將創建的user1用戶關聯到創建的組內linux_gitlab
1、點擊創建好的linux_gitlab組

2、點擊add users to group,將創建的用戶添加到組內

5、選擇linux_gitlab組之后,創建新的項目。
1、登陸創建的user1用戶賬號,然后選擇之前創建的linux_gitlab組,創建項目。
2、選擇linux_gitlab組之后創建新的項目。

3、創建一個web1項目

4、創建一個測試內容

5、創建文件內容,並注明文件信息。

6、此時就可以看到提交后文件的內容

7、復制web1項目要克隆的URL地址。

6、克隆前面創建的項目地址。
1、安裝git包,並在另一台主機上進行克隆測試。
# yum install git -y
2、使用git命令開始克隆web1項目的數據
[root@jenkins ~]# git clone http://192.168.7.100/linux_gitlab/web1.git Cloning into 'web1'... Username for 'http://192.168.7.100': root # 輸入管理員賬號和密碼。 Password for 'http://root@192.168.7.100': remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done.
3、在web1目錄下可以看到克隆后的文件內容
[root@jenkins ~]# cd web1/ # 切換到web1目錄下 [root@jenkins web1]# ls index.html [root@jenkins web1]# cat index.html # 查看文件信息 linux web1 V1[root@jenkins web1]#
7、將linux系統上的文件傳到gitlab網站上
1、在web1項目里的index.html文件添加內容,並將其信息傳遞到gitlab網站上。
[root@jenkins web1]# cd web1/ # 切換到克隆的項目目錄下。 [root@jenkins web1]# cat index.html linux web1 V1 index V222 # 在index.html文件中添加內容。 [root@jenkins web1]# git add index.html # 添加index.html文件到緩存中 [root@jenkins web1]# git commit -m "v2" # 提交index.html文件。 [root@jenkins web1]# git push # 將index.html文件傳到gitlab網頁上。 warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git) Username for 'http://192.168.7.100': root # 輸入gitlab賬號 Password for 'http://root@192.168.7.100': Counting objects: 5, done. Writing objects: 100% (3/3), 254 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To http://192.168.7.100/linux_gitlab/web1.git 5e5c4e8..93667aa master -> master
2、在gitlab網站上查看上傳上去的結果。

三、Gitlab基礎使用
1、gitlab常用命令
1、以下是不常用命令。
gitlab-rails # 用於啟動控制台進行特殊操作,比如修改管理員密碼、打開數據庫控制台(gitlab-rails dbconsole)等。 # 不常用 gitlab-psql # 數據庫命令行
2、gitlab-ctl用法:
# gitlab-ctl stop #停止gitlab # gitlab-ctl restart #重啟gitlab # gitlab-ctl status # 查看gitlab狀態 # gitlab-ctl tail nginx # 查看nginx組件的日志 # gitlab-ctl reconfigure # 修改完配置文件,以及第一次啟動gitlab,都會使用此命令。
2、s添加SSH-Keys公鑰,在jenkins主機上實現非密碼登陸克隆項目文件
1、修改jenkins主機的hostname,並生成公鑰對。
[root@jenkins ~]# hostnamectl set-hostname jenkins # 將第二個主機的hostname改為jenkins [root@jenkins ~]# ssh-keygen # 然后生成公鑰對。
2、查看/root/.ssh目錄下的id_rsa.pub的公鑰對內容。
[root@jenkins ~]# cd /root/.ssh [root@jenkins .ssh]# ls id_rsa id_rsa.pub known_hosts [root@jenkins .ssh]# cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKD7eISTB6VcDpF4h6oN8zusJ7GJMGd0GXZsjoQr3knjURN6PvUMmzP2UkwjRAryPS5e9LkQOHHy82CwNSFPV0Ow1npOAF8V3CyvOBcRhXhRZO4/PWP1lb6cqs5BeYg0gEQFIGpEt7kdSa9n5+6cEuzuzHfwFIXIHYFOrHOZeI6JPIzJR5Ww4u7N59MMCH3EKhFRF7QH92QcAEjvDYv0ncz1ymG5TkK72d2SniSwFTF4nOJWL6nGfTtnCQzlrMDR8GKfrbdkE6weoV6KHz21vGzHCUE/eM2aiIgFUvPbFbURWzsixzyq12mksejp1HnMD9T/VgHHT+nbMiUAVERv45 root@jenkins
3、在gitlab網站上,將jenkins主機的公鑰對復制到SSH-Keys對應的位置。
4、在web1項目上復制SSH的地址鏈接。

5、在jenkins主機上通過SSH地址進行克隆web1項目的文件,此時就是免秘鑰登錄,克隆web1項目的文件,如果有web1目錄,就無法克隆。
[root@jenkins ~]# git clone git@192.168.7.100:linux_gitlab/web1.git [root@jenkins ~]# ls anaconda-ks.cfg web1 # 切換到項目目錄下 [root@jenkins ~]# cd web1/ [root@jenkins web1]# ls index.html [root@jenkins web1]# cat index.html #查看項目下默認的文件名內容 linux web1 V1 index V222
3、git命令使用
1、git命令用法
git config --global user.name name # 設置全局用戶名 git config --global user.email xxx@xx.com # 設置全局郵箱 git config --global --list # 列出用戶全局設置 git add index.html # 添加指定文件、目錄或當前目錄下所有數據到暫存區 git commit m "11" # 提交文件到工作區 git stat us #查看工作區的狀態 git push # 提交代碼到服務器 git pull # 獲取代碼到本地 git log # 查看操作日志 vim .gitignore # 定義忽略文件 git reset --hard HEAD^^ # git版本回滾,HEAD為當前版本,加一個為上一個,為上上一個版本 git reflog # 獲取每次提交的ID,可以使用 hard 根據提交的ID進行版本回退 git reset --hard 5ae4b06 # 回退到指定id的版本 git branch # 查看當前所處的分支 git checkout -b develop # 創建develop,並切換到一個新分支 git checkout develop # 切換到develop分支
示例一:回滾命令用法:
[root@jenkins web1]# git reset --hard HEAD^ # 將部署的代碼回滾到上一版本,用法最多
HEAD is now at 5e5c4e8 Add index.html V1 version
[root@jenkins web1]# cat index.html
linux web1 V1[root@jenkins web1]#
[root@jenkins web1]# git reflog
93667aa HEAD@{0}: reset: moving to 93667aa
5e5c4e8 HEAD@{1}: reset: moving to HEAD^
93667aa HEAD@{2}: clone: from git@192.168.7.100:linux_gitlab/web1.git
[root@jenkins web1]# cat index.html # 查看文件內容
linux web1 V1
index V222
index V23
[root@jenkins web1]# git reflog # 查看ID號
93667aa HEAD@{0}: reset: moving to 93667aa
5e5c4e8 HEAD@{1}: reset: moving to HEAD^
93667aa HEAD@{2}: clone: from git@192.168.7.100:linux_gitlab/web1.git
[root@jenkins web1]# git reset --hard 93667aa # 指定ID號,回滾到上一個版本
HEAD is now at 93667aa v2
[root@jenkins web1]# cat index.html
linux web1 V1
index V222
示例二:創建branch分支(領導層才會使用)
1、創建分支

2、分支命名

3、在linux主機上進行克隆,並切換到分支內,查看當前所處的分支
[root@jenkins ~]# git clone git@192.168.7.100:linux_gitlab/web1.git # 克隆web1項目的文件 Cloning into 'web1'... remote: Enumerating objects: 13, done. remote: Counting objects: 100% (13/13), done. remote: Compressing objects: 100% (5/5), done. remote: Total 13 (delta 0), reused 0 (delta 0) Receiving objects: 100% (13/13), done. [root@jenkins ~]# cd web1/ # 切換到web1目錄下 [root@jenkins web1]# git checkout develop # 切換到develop分支內 Branch develop set up to track remote branch develop from origin. Switched to a new branch 'develop' [root@jenkins web1]# git branch # 查看此時所處的分支名稱 * develop master
