1. 程式人生 > 其它 >第一章 Git+Gitlab介紹和安裝

第一章 Git+Gitlab介紹和安裝

一、持續整合簡介

1.軟體開發生命週期

軟體開發生命週期又叫做SDLC(Software Development Life Cycle),它是集合了計劃、開發、測試 和部署過程的集合。

1.	需求分析
這是生命週期的第一階段,根據專案需求,團隊執行一個可行性計劃的分析。專案需求可能是公司內部或者客戶提出的。這階段主要是對資訊的收集,也有可能是對現有專案的改善和重新做一個新的專案。還要分析專案的預算多長,可以從哪方面受益及佈局,這也是專案建立的目標。

2.	設計
第二階段就是設計階段,系統架構和滿意狀態(就是要做成什麼樣子,有什麼功能),和建立一個專案計劃。計劃可以使用圖表,佈局設計或者文者的方式呈現。

3.	實現
第三階段就是實現階段,專案經理建立和分配工作給開者,開發者根據任務和在設計階段定義的目標進行開發程式碼。依據專案的大小和複雜程度,可以需要數月或更長時間才能完成。

4.	測試
測試人員進行程式碼測試 ,包括功能測試、程式碼測試、壓力測試等。

5.	進化
最後進階段就是對產品不斷的進化改進和維護階段,根據使用者的使用情況,可能需要對某功能進行修改,bug修復,功能增加等。

2、敏捷開發

	敏捷開發(Agile Development) 的核心是迭代開發(Iterative Development) 與 增量開發 (Incremental Development) 。

1) 迭代開發

	對於大型軟體專案,傳統的開發方式是採用一個大週期(比如一年)進行開發,整個過程就是一次"大 開發";迭代開發的方式則不一樣,它將開發過程拆分成多個小週期,即一次"大開發"變成多次"小開 發",每次小開發都是同樣的流程,所以看上去就好像重複在做同樣的步驟。

2) 增量開發

	軟體的每個版本,都會新增一個使用者可以感知的完整功能。也就是說,按照新增功能來劃分迭代。 

3) 敏捷開發如何迭代

	雖然敏捷開發將軟體開發分成多個迭代,但是也要求,每次迭代都是一個完整的軟體開發週期,必須按照軟體工程的方法論,進行正規的流程管理。

3.持續整合

	持續整合( Continuous integration , 簡稱 CI )指的是,頻繁地(一天多次)將程式碼整合到主幹。 持續整合的目的,就是讓產品可以快速迭代,同時還能保持高質量。它的核心措施是,程式碼整合到主幹之前,必須通過自動化測試。只要有一個測試用例失敗,就不能整合。通過持續整合,團隊可以快速的從一個功能到另一個功能,簡而言之,敏捷軟體開發很大一部分都要歸功於持續整合。在持續整合的過程當中主要包括以下步驟:提交、測試、構建(容器需要構建, 編譯型語言編譯)、部署及回滾。

1) 持續整合要素

1.一個自動構建過程,從檢出程式碼、編譯構建、執行測試、結果記錄、測試統計等都是自動完成的,無需人工干預。 
2.一個程式碼儲存庫,即需要版本控制軟體來保障程式碼的可維護性,同時作為構建過程的素材庫,一般使用SVN或Git。
3.一個持續整合伺服器, Jenkins 就是一個配置簡單和使用方便的持續整合伺服器。

2) 持續整合的好處

1、降低風險,由於持續整合不斷去構建,編譯和測試,可以很早期發現問題,所以修復的代價就少
2、對系統健康持續檢查,減少釋出風險帶來的問題
3、減少重複性工作
4、持續部署,提供可部署單元包
5、持續交付可供使用的版本
6、增強團隊信心

二、Gitlab私有程式碼倉庫

	GitLab 是一個用於倉庫管理系統的開源專案,使用Git作為程式碼管理工具,並在此基礎上搭建起來的web服務。
	
	GitLab和GitHub一樣屬於第三方基於Git開發的作品(私有倉庫),GITLAB免費且開源(基於MIT協議),與Github類似, 可以註冊使用者,任意提交你的程式碼,新增SSHKey等等。不同的是,GitLab是可以部署到自己的伺服器 上,資料庫等一切資訊都掌握在自己手上,適合團隊內部協作開發,你總不可能把團隊內部的智慧總放 在別人的伺服器上吧?簡單來說可把GitLab看作個人版的GitHub。

1.Git的部署

1)Git的簡介

Git 是一個開源的分散式版本控制系統,用於敏捷高效地處理任何或小或大的專案。Git 是 Linus Torvalds 為了幫助管理 Linux 核心開發而是一個開放原始碼的版本控制軟體。Git 與常用的版本控制工具 CVS, Subversion 等不同,它採用了分散式版本庫的方式,不必有伺服器端軟體支援。

2)Git的安裝

#1.安裝
[root@m01 ~]# yum -y install git

#2.檢視Git版本
[root@m01 ~]# git --version
git version 1.8.3.1

#3.修改基本配置
[root@m01 ~]# git config  --global user.name  "jh"
[root@m01 ~]# git config  --global  user.email "[email protected]"

#4.檢視配置
[root@m01 ~]# git config  -l
user.name=jh
[email protected]

3)倉庫簡介

	對應的就是一個目錄,這個目錄中的所有檔案被git管理起來。以後會將一個專案的根目錄,作為倉庫。倉庫中的每個檔案的改動 都由git跟蹤。

4)新建本地倉庫

#1.新建倉庫
[root@m01 ~]# mkdir test

#2.初始化空的 Git 版本庫
[root@m01 ~]# cd test/ 
[root@m01 ~/test]# git init
Initialized empty Git repository in /root/test/.git/

#3.檢視倉庫
[root@m01 ~/test]# ll -a
total 0
drwxr-xr-x  3 root root  18 Dec 22 19:33 .
dr-xr-x---. 4 root root 215 Dec 22 19:33 ..
drwxr-xr-x  7 root root 119 Dec 22 19:33 .git

5)建立遠端倉庫

#1.Github上建立倉庫

#2.Gitee上建立倉庫(以此為例)
選擇語言,其他按照自己需求的來.

6)連結遠端倉庫

#1.通過HTTPS連線遠端倉庫
[root@m01 ~/test]# git remote  add  origin https://gitee.com/jhno1/oldboy-jenkins.git

ps: 點選克隆/下載選擇HTTPS方式,複製連結

#2.通過ssh方式連線
1.生成公鑰對
[root@m01 ~]# ssh-keygen 
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:CLeRI42hKnREMfbbwLYXmrpmx4NvYHHJvgI8NMPkDaM root@m01
The key's randomart image is:
+---[RSA 2048]----+
|  .*o            |
| +o.++ .         |
|=.=.==B.         |
|EB.o.BB=.        |
|= o +=ooS        |
|.+ o...          |
|  +.+ .          |
|   =.*           |
   o.=..          |
+----[SHA256]-----+    

2.檢視公鑰
[root@m01 ~]# cat  /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1EV7n0r+ma/6Lj5FKM1QaWy2w7au2qvvFY/+bmhcFv86b
XXn3AVKgWhSrzqZPZzSkiuFAATzXqjGl8WJ9EYBamCgYqV/bzysS5BPI2f31zjkgX2XKNzcMHzXKw6GEG4X0
0qVmwJq2ajSxSrnaLzZRpV9lTdDtSO8WIVZGxWRPiU3aZvq/tFW08fQBKKcV5AlY04WVlRCZZKpKhZ1opmGK
oqmJ6nh/vJVeCohvrdMhjDtj6zvciHf3sJGAxY6zmH6PaeV+lZS+3pbGR24KIEdARoJuCpSw2NbBo40dn9Mn
Apsl0K+l2L57Aw+kvmXXd886ZnIIMLwopKumEM4n+xeB root@m01

3.複製公鑰配置到Gitee倉庫
# 管理--> 部署公鑰管理-->新增公鑰

4.返回克隆下載複製ssh連結執行
[root@m01 ~/test]# git remote  add origin [email protected]:jhno1/oldboy-jenkins.git

#檢視遠端連線
[root@m01 ~/test]# git remote  -v
origin	https://gitee.com/jhno1/oldboy-jenkins.git (fetch)
origin	https://gitee.com/jhno1/oldboy-jenkins.git (push)

#刪除遠端連線
[root@m01 ~/test]# git remote rm origin

2.Git檔案的推送

1)從遠端倉庫獲取

# 當本地沒有程式碼倉庫時
[root@m01 ~/test]# git clone  https://gitee.com/jhno1/oldboy-jenkins.git
Cloning into 'oldboy-jenkins'...
Username for 'https://gitee.com': jhno1
Password for 'https://[email protected]': 
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.

# 當本地已經有了程式碼倉庫,現在需要同步遠端倉庫內容
[root@m01 ~/test]# git pull origin master
The authenticity of host 'gitee.com (212.64.62.183)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,212.64.62.183' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From gitee.com:jhno1/oldboy-jenkins

2)將本地檔案推送到遠端

#1.檢視工作目錄的狀態
[root@m01 ~/test]# git status  
# On branch master
# Untracked files:
    (use "git add <file>..." to include in what will be committed)
#
#	oldboy-jenkins/
nothing added to commit but untracked files present (use "git add" to track)

#2.建立新檔案
[root@m01 ~/test]# cd oldboy-jenkins/
[root@m01 ~/test/oldboy-jenkins]# vim 1.txt
<h1> 你是</h1>                                                                               
"1.txt" [New] 1L, 17C written                                     

#3.將檔案新增到暫存區
[root@m01 ~/test/oldboy-jenkins]# git add 1.txt
[root@m01 ~/test/oldboy-jenkins]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   1.txt

#4.提交更改,新增備註資訊(此時將暫存區的資訊提交到本地倉庫)
[root@m01 ~/test/oldboy-jenkins]# git commit  -m "zhehsi。。。"
[master 729e679] zhehsi。。。
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
 
#5. 將本地倉庫的檔案push到遠端倉庫(若 push 不成功,可加 -f 進行強推操作)
[root@m01 ~/test/oldboy-jenkins]# git push  origin  master 
Username for 'https://gitee.com': jhno1
Password for 'https://[email protected]': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/jhno1/oldboy-jenkins.git
   c671512..729e679  master -> master

3)檢視檔案變化

# 檢視某個檔案
[root@m01 ~/test/oldboy-jenkins]# git diff 1.txt

# 檢視所有變化
[root@m01 ~/test/oldboy-jenkins]# git diff

3.Git分支

# 檢視分支
[root@m01 ~/test/oldboy-jenkins]# git branch 
* master

# 檢視遠端分支
[root@m01 ~/test/oldboy-jenkins]# git branch  -a
* master

# 建立分支
[root@m01 ~/test/oldboy-jenkins]# git branch  test
[root@m01 ~/test/oldboy-jenkins]# git branch  -a
* master
  test

# 切換分支
[root@m01 ~/test/oldboy-jenkins]# git checkout  test 
Switched to branch 'test'
[root@m01 ~/test/oldboy-jenkins]# git branch -a
  master
* test

# 打標籤
[root@m01 ~/test/oldboy-jenkins]# git tag -a v1.1.5-stable -m "做了很多事" 
[root@m01 ~/test/oldboy-jenkins]# git tag
v1.1.5-stable

# 檢視標籤內容
[root@m01 ~/test/oldboy-jenkins]# git show v1.1.5-stable
tag v1.1.5-stable
Tagger: jh <[email protected]>
Date:   Tue Dec 22 20:26:52 2020 +0800
做了很多事

# 按照分支拉取程式碼
[root@m01 ~/test/oldboy-jenkins]# git clone -b [分支名稱] [git連結]

# 按照標籤拉取程式碼
[root@m01 ~/test/oldboy-jenkins]# git clone [git連結]
[root@m01 ~/test/oldboy-jenkins]# git tag [tag_name]

4.遠端倉庫

5.Gitlab的安裝

#1.安裝相關依賴
[root@m01 ~]# yum -y install policycoreutils openssh-server openssh-clients postfix

#2.關閉防火牆
[root@m01 ~]# systemctl disable firewalld

#3.關閉selinux
[root@m01 ~]# sed -i 's#enforcing#disabled#g' /etc/sysconfig/selinux

#4.下載gitlab包,並且安裝
[root@m01 ~]# rz gitlab-ce-13.0.14-ce.0.el7.x86_64.rpm

網站:https://about.gitlab.com

#5.安裝gitlab
[root@m01 ~]# yum -y localinstall gitlab-ce-13.0.14-ce.0.el7.x86_64.rpm 

#6.配置gitlab
[root@m01 ~]# vim /etc/gitlab/gitlab.rb
external_url 'http://10.0.0.61'
nginx['listen_port'] = 80

#7.重新整理配置(預設啟動)
[root@m01 ~]# gitlab-ctl reconfigure

#8. 啟動gitlab
[root@m01 ~]# gitlab-ctl start

#9.登入
10.0.0.61

6.Gitlab新增組、建立使用者、新增專案

1)建立組

	使用管理員 root 建立組,一個組裡面可以有多個專案分支,可以將開發新增到組裡面進行設定許可權, 不同的組就是公司不同的開發專案或者服務模組,不同的組新增不同的開發即可實現對開發設定許可權的管理。

2)建立使用者

3)將使用者繫結使用者組

4)新增使用者有5種角色

1.	Guest:可以建立issue、發表評論,不能讀寫版本庫 

2.	Reporter:可以克隆程式碼,不能提交,QA、PM 可以賦予這個許可權 

3.	Developer:可以克隆程式碼、開發、提交、push,普通開發可以賦予這個許可權 

4.	Maintainer:可以建立專案、新增tag、保護分支、新增專案成員、編輯專案,核心開發可以賦予這個 許可權  - Visibility Level、刪除專案、遷移專案、管理組成員,開發組組 長可以賦予這個許可權

5.	Owner:可以設定專案訪問許可權 

5)建立專案