軟件安裝包
鏈接:https://pan.baidu.com/s/1ItDkmvoeQajIjY17J9Jw6w
提取碼:o2rr
1. Devops介紹
01. 運維介紹
02. Devops是什么
03. Devops能干嘛
04. Devops如何實現
2. Git版本控制系統
01. 版本控制系統簡介
02. 為什么需要版本控制系統
03. 常見版本管理工具
04. 牛逼的人不需要解釋
3. Git安裝
01. 系統環境准備
02. Git安裝部署
03. Git初始化
4. Git常規使用
01. 創建數據-提交數據
02. Git四種狀態
03. Git基礎命令
04. Git分支
05. Git標簽使用
5. Github使用
6. Gitlab安裝
7. Gitlab使用
01. 外觀配置
02. Gitlab漢化配置
03. 注冊限制
04. 創建用戶及組
05. 創建用戶
06. 把用戶添加到組
07. 創建項目
08. 推送代碼到Gitlab
09. 開發推送代碼到Gitlab
10. 分支保護
11. 代碼合並
12. Git-gui安裝
8. Gitlab備份與恢復
01. 備份
02. 恢復
9. Jenkins
01. 安裝准備
02 .安裝Jdk和Jenkins
03 .配置Jenkins
04. 插件安裝
05. 創建項目
06. Jenkins獲取Git源代碼
07. 立即構建獲取源代碼
08. Jenkins代碼推送到Web
09. 配置自動觸發構建
10. 測試是否觸發
11. 返回構建狀態
10. 創建Maven項目
01. 部署Maven
02. 編譯測試
03. 部署Tomcat及數據庫
04. 創建一個jeesns項目
05. Jenkins創建一個maven
11. Pipeline項目
01. 基礎概念
02. 創建pipeline項目
1. Devops介紹
01. 運維介紹
天天說運維,究竟是干什么的?先看看工作流程唄。一般來說,運維工程師在一家企業里屬於個位數的崗位,甚至只有一個。面對生產中NNN台服務器,NN個人員,工作量也是非常大的。
代碼上線流程圖

運維工程師三大核心職能

02. Devops是什么
開發 development
運維 operations
03. Devops能干嘛
1 自動化測試
2 持續集成
3 代碼質量管理工具
4 程序員鼓勵師
04. Devops如何實現
既然這么好?為什么有些公司沒有
設計架構規划-代碼的存儲-構建-測試、預生產、部署、監控
2. Git版本控制系統
01. 版本控制系統簡介
vcs `version control system`
版本控制系統是一種記錄一個或若干個文件內容變化,以便將來查閱特定版本內容情況的系統
記錄文件的所有歷史變化
隨時可恢復到任何一個歷史狀態
多人協作開發
02. 為什么需要版本控制系統
03. 常見版本管理工具
SVN
集中式的版本控制系統,只有一個中央數據倉庫,如果中央數據倉庫掛了或者不可訪問,所有的使用者無法使用SVN,無法進行提交或備份文件。

04. 牛逼的人不需要解釋

3. Git安裝
01. 系統環境准備
[root@git ~]# cat /etc/redhat-release #查看系統版本
CentOS Linux release 7.6.1810 (Core)
[root@git ~]# uname -r #查看內核版本
3.10.0-957.el7.x86_64
[root@git ~]# getenforce #確認Selinux關閉狀態
Disabled
[root@git ~]# systemctl status firewalld #關閉防火牆
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
02. Git安裝部署
[root@git ~]# yum install git -y #安裝Git
[root@git ~]# git config
--global use global config file #使用全局配置文件
--system use system config file #使用系統級配置文件
--local use repository config file #使用版本庫級配置文件
[root@git ~]# git config --global user.name "rlb" #配置git使用用戶
[root@git ~]# git config --global user.email "1176494252@qq.com" #配置git使用郵箱
[root@git ~]# git config --global color.ui true #語法高亮
[root@git ~]# git config --list #查看配置列表
user.name=rlb
user.email=1176494252@qq.com
color.ui=true
[root@git ~]# cat .gitconfig
[user]
name = rlb
email = 1176494252@qq.com
[color]
ui = true
03. Git初始化
#初始化工作目錄、對已存在的目錄或者對已存在的目錄都可進行初始化
[root@git ~]# mkdir git_data
[root@git ~]# cd git_data/
#初始化
[root@git ~/git_data]# git init
Initialized empty Git repository in /root/git_data/.git/
#查看工作區狀態
[root@git ~/git_data]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
#隱藏文件介紹:

branches #分支目錄
config #定義項目特有的配置選項
description #僅供git web程序使用
HEAD #指示當前的分支
hooks #包含git鈎子文件
info #包含一個全局排除文件(exclude文件)
objects #存放所有數據內容,有info和pack兩個子文件夾
refs #存放指向數據(分支)的提交對象的指針
index #保存暫存區信息,在執行git init的時候,這個文件還沒有
4. Git常規使用
01. 創建數據-提交數據
02. Git四種狀態
03. Git基礎命令
[root@git ~/git_data]# git status
# On branch master #位於分支 master
#
# Initial commit #初始提交
#
nothing to commit (create/copy files and use "git add" to track)
#無文件要提交(創建/拷貝文件並使用 "git add" 建立跟蹤)
#創建測試文件
[root@git ~/git_data]# touch a b c
[root@git ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a
# b
# c
nothing added to commit but untracked files present (use "git add" to track)
#提交文件
[root@git ~/git_data]# git add a
[root@git ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: a
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b
# c
#查看提交的內容
[root@git ~/git_data]# ll .git/
total 16
drwxr-xr-x 2 root root 6 2019-10-20 11:53 branches
-rw-r--r-- 1 root root 92 2019-10-20 11:53 config
-rw-r--r-- 1 root root 73 2019-10-20 11:53 description
-rw-r--r-- 1 root root 23 2019-10-20 11:53 HEAD
drwxr-xr-x 2 root root 242 2019-10-20 11:53 hooks
-rw-r--r-- 1 root root 96 2019-10-20 12:08 index #git add a 把文件提交到了暫存區
drwxr-xr-x 2 root root 21 2019-10-20 11:53 info
drwxr-xr-x 5 root root 40 2019-10-20 12:08 objects
drwxr-xr-x 4 root root 31 2019-10-20 11:53 refs
[root@git ~/git_data]# git add . #使用git add . 或者 * 添加目錄中所有改動過的文件
[root@git ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: a
# new file: b
# new file: c
#
#將c文件從暫存區刪除
[root@git ~/git_data]# git rm --cached c
rm 'c'
[root@git ~/git_data]# ll
total 0
-rw-r--r-- 1 root root 0 2019-10-20 12:07 a
-rw-r--r-- 1 root root 0 2019-10-20 12:07 b
-rw-r--r-- 1 root root 0 2019-10-20 12:07 c
[root@git ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: a
# new file: b
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# c
#刪除工作區文件
[root@git ~/git_data]# rm -f c
#刪除文件
1.先從暫存區撤回到工作區、然后直接刪除文件
[root@git ~/git_data]# git rm --cached c
[root@git ~/git_data]# rm -f c
2.直接從暫存區域同工作區域一同刪除文件命令
[root@git ~/git_data]# git rm -f b
#提交到本地倉庫
[root@git ~/git_data]# git commit -m "new file a"
[master (root-commit) c6b0ac2] new file a
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
[root@git ~/git_data]# git status
# On branch master
nothing to commit, working directory clean
#修改文件名稱兩種方法
1.本地重命名
[root@git ~/git_data]# mv a a.txt
[root@git ~/git_data]# git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: a
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a.txt
no changes added to commit (use "git add" and/or "git commit -a")
#從暫存區刪除a文件
[root@git ~/git_data]# git rm --cached a
rm 'a'
[root@git ~/git_data]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: a
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a.txt
#提交到暫存區
[root@git ~/git_data]# git add a.txt
[root@git ~/git_data]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: a -> a.txt #識別到a和a.txt相同為重命名
#
#提交到本地倉庫
[root@git ~/git_data]# git commit -m "commit a.txt"
[master 64e0f41] commit a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
rename a => a.txt (100%)
2.直接用git命令重命名
[root@git ~/git_data]# git mv a.txt a
[root@git ~/git_data]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: a.txt -> a #工作區域和暫存區域的文件同時修改文件名稱
#
#提交到本地倉庫
[root@git ~/git_data]# git commit -m "rename a.txt a"
[master 1a85735] rename a.txt a
1 file changed, 0 insertions(+), 0 deletions(-)
rename a.txt => a (100%)
git status #只能查看區域狀態的不同,不能查看文件內容的變化。
git diff #查看內容的不同
[root@git ~/git_data]# echo aaa > a
#比對工作目錄與暫存區的文件的不同
[root@git ~/git_data]# git diff a
diff --git a/a b/a
index e69de29..72943a1 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+aaa
#提交a文件到暫存區域、在用git diff是相同的
[root@git ~/git_data]# git add a
#比對的是暫存區和本地倉庫文件的不同處
[root@git ~/git_data]# git diff --cached a
diff --git a/a b/a
index e69de29..72943a1 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+aaa
#提交后在比對則暫存區和本地倉庫內容相同
[root@git ~/git_data]# git commit -m "modified a aaa"
[master 18c89e4] modified a
1 file changed, 1 insertion(+)
git commit #相當於虛擬機的鏡像、任何操作都被做了一次快照,可恢復到任意一個位置
#查看歷史的git commit快照操作
[root@git ~/git_data]# git log
commit 18c89e4fc7bb29f9998ceb5781e792ace28c9910
Author: rlb <1176494252@qq.com>
Date: Sun Oct 20 17:52:58 2019 +0800
modified a aaa
commit 1a85735ffcc03feb5e64dfd5860da0a34e003ae8
Author: rlb <1176494252@qq.com>
Date: Sun Oct 20 17:49:39 2019 +0800
rename a.txt a
commit 64e0f41387c3da32259ef8fd12ed282a3e6e5857
Author: rlb <1176494252@qq.com>
Date: Sun Oct 20 17:46:55 2019 +0800
commit a.txt
commit c6b0ac28147c1697e77eb421a8e492cac8a0e810
Author: rlb <1176494252@qq.com>
Date: Sun Oct 20 17:41:46 2019 +0800
new file a
#使用一行簡單的顯示commit信息
[root@git ~/git_data]# git log --oneline
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a
#顯示當前的指針指向哪里,在哪個快照下工作
[root@git ~/git_data]# git log --oneline --decorate
18c89e4 (HEAD, master) modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a
#顯示具體內容的變化
[root@git ~/git_data]# git log -p
#只顯示1條內容
[root@git ~/git_data]# git log -1
commit 18c89e4fc7bb29f9998ceb5781e792ace28c9910
Author: rlb <1176494252@qq.com>
Date: Sun Oct 20 17:52:58 2019 +0800
modified a
#恢復歷史數據
1.只更改了當前目錄
[root@git ~/git_data]# echo "333" >> a
[root@git ~/git_data]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
no changes added to commit (use "git add" and/or "git commit -a")
#從暫存區覆蓋本地工作目錄
[root@git ~/git_data]# git checkout -- a
[root@git ~/git_data]# cat a
aaa
2.修改了本地目錄且同時提交到了暫存區
[root@git ~/git_data]# echo ccc >> a #添加新內容
[root@git ~/git_data]# git add . #提交到暫存區
#比對暫存區和本地倉庫的內容
[root@git ~/git_data]# git diff --cached
diff --git a/a b/a
index 72943a1..959479a 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
aaa
+ccc
[root@git ~/git_data]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
#本地倉庫覆蓋暫存區域
[root@git ~/git_data]# git reset HEAD a
Unstaged changes after reset:
M a
#比對工作區與暫存區的不同
[root@git ~/git_data]# git diff a
diff --git a/a b/a
index 72943a1..959479a 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
aaa
+ccc
#比對暫存區與本地倉庫的不同
[root@git ~/git_data]# git diff --cached a
3.修改了工作目錄后提交到了暫存區和本地倉庫后進行數據恢復
[root@git ~/git_data]# echo bbb >>a #添加新的內容
[root@git ~/git_data]# git commit -am "add bbb" #提交到本地倉庫
[master 6118c8d] add bbb
1 file changed, 2 insertions(+)
[root@git ~/git_data]# echo ccc >>a
#這時候發現改錯代碼了,想還原某一次提交的文件快照
[root@git ~/git_data]# git commit -am "add ccc"
[master cc5c366] add ccc
1 file changed, 1 insertion(+)
[root@git ~/git_data]# git log --oneline
cc5c366 add ccc
6118c8d add bbb
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a
Git服務程序中有一個叫做HEAD的版本指針,當用戶申請還原數據時,其實就是將HEAD指針指向到某個特定的提交版本,但是因為Git是分布式
版本控制系統,為了避免歷史記錄沖突,故使用了SHA-1計算出十六進制的哈希字串來區分每個提交版本,另外默認的HEAD版本指針會指向到最近的一次提交版本記錄
[root@git ~/git_data]# git reset --hard 18c89e4
HEAD is now at 18c89e4 modified a
剛剛的操作實際上就是改變了一下HEAD版本指針的位置,就是你將HEAD指針放在那里,那么你的當前工作版本就會定位在那里,要想把內容再還原到最新提交的版本,先看查看下提交版本號
#打開發現回退錯了,應該回退到bbb版本
[root@git ~/git_data]# cat a
aaa
#這時候查看log沒有commit bbb的歷史了
[root@git ~/git_data]# git log --oneline
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a
怎么搞得?竟然沒有了add bbb這個提交版本記錄?
原因很簡單,因為我們當前的工作版本是歷史的一個提交點,這個歷史提交點還沒有發生過add bbb 更新記錄,所以當然就看不到了,要是想"還原到未來"的歷史更新點,可以用git reflog命令來查看所有的歷史記錄:
#使用git reflog 可查看總歷史內容
[root@git ~/git_data]# git reflog
18c89e4 HEAD@{0}: reset: moving to 18c89e4
cc5c366 HEAD@{1}: commit: add ccc
6118c8d HEAD@{2}: commit: add bbb
18c89e4 HEAD@{3}: commit: modified a
1a85735 HEAD@{4}: commit: rename a.txt a
64e0f41 HEAD@{5}: commit: commit a.txt
c6b0ac2 HEAD@{6}: commit (initial): new file a
#然后使用reset回到bbb的版本內容下
[root@git ~/git_data]# git reset --hard 6118c8d
HEAD is now at 6118c8d add bbb
[root@git ~/git_data]# cat a
aaa
bbb
04. Git分支
分支即是平行空間,假設你在為某個手機系統研發拍照功能,代碼已經完成了80%,但如果將這不完整的代碼直接提交到git倉庫中,又有可能影響到其他人的工作,此時我們便可以在該軟件的項目之上創建一個名叫"拍照功能"的分支,這種分支只會屬於你自己,而其他人看不到,等代碼編寫完成后再與原來的項目主分支合並下即可,這樣即能保證代碼不丟失,又不影響其他人的工作。

一般在實際的項目開發中,我們要盡量保證master分支是非常穩定的,僅用於發布新版本,平時不要隨便直接修改里面的數據文件,而工作的時候則可以創建不同的工作分支,等到工作完成后在合並到master分支上面,所以團隊的合作分支看起來會像上面的圖那樣。
[root@git ~/git_data]# git log --oneline --decorate
ef16b0b (HEAD, master) add ddd #默認分支指向你最后一次的提交 HEAD頭、指針
6118c8d add bbb #HEAD 指針指向哪個分支、說明你當前在哪個分支下工作
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a
#新建testing分支
[root@git ~/git_data]# git branch testing
# *號在哪里就說明當前在哪個分支上如下圖所示:
[root@git ~/git_data]# git branch
* master
testing

#通過命令查看分支指向
[root@git ~/git_data]# git log --oneline --decorate
ef16b0b (HEAD, testing, master) add ddd
6118c8d add bbb
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a
#切換到testing分支、對應的HEAD指針也指向了testing
[root@git ~/git_data]# git checkout testing
Switched to branch 'testing'
#HEAD指針指向了testing
[root@git ~/git_data]# git branch
master
* testing

[root@git ~/git_data]# touch test
[root@git ~/git_data]# git add .
[root@git ~/git_data]# git commit -m 'commit test'
[testing 0924a70] commit test
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test
[root@git ~/git_data]# git log --oneline --decorate
0924a70 (HEAD, testing) commit test
ef16b0b (master) add ddd
6118c8d add bbb
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a

[root@git ~/git_data]# git checkout master
Switched to branch 'master'
[root@git ~/git_data]# git branch
* master
testing
[root@git ~/git_data]# ll #正常情況下是沒有test文件的、保證master分支是線上環境的
total 4
-rw-r--r-- 1 root root 16 Oct 20 18:36 a

[root@git ~/git_data]# touch master
[root@git ~/git_data]# git add .
[root@git ~/git_data]# git commit -m "commit master"
[master 8efaada] commit master
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 master
[root@git ~/git_data]# ll
total 4
-rw-r--r-- 1 root root 16 Oct 20 18:36 a
-rw-r--r-- 1 root root 0 Nov 16 12:21 master

#合並分支
[root@git ~/git_data]# git merge testing
Merge branch 'testing'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
merge testing to master #提示輸入描述信息,相當於git的-m參數
#查看日志記錄
[root@git ~/git_data]# git log --oneline --decorate
0fcf3ce (HEAD, master) Merge branch 'testing'
8efaada commit master
0924a70 (testing) commit test
ef16b0b add ddd
6118c8d add bbb
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a

#合並沖突
[root@git ~/git_data]# echo "master" >> a
[root@git ~/git_data]# git commit -am "modified a master"
[master a3d84f3] modified a master
1 file changed, 1 insertion(+)
[root@git ~/git_data]# git checkout testing
Switched to branch 'testing'
[root@git ~/git_data]# git branch
master
* testing
[root@git ~/git_data]# cat a
aaa
ccc
bbb
ddd
[root@git ~/git_data]# echo "testing" >>a
[root@git ~/git_data]# git commit -am "modified a on testing branch"
[testing 23dec52] modified a on testing branch
1 file changed, 1 insertion(+)
[root@git ~/git_data]# git checkout master
Switched to branch 'master'
[root@git ~/git_data]# git merge testing
Auto-merging a
CONFLICT (content): Merge conflict in a
Automatic merge failed; fix conflicts and then commit the result.
[root@git ~/git_data]# cat a #沖突的文件自動標識到文件里,手動更改沖突要保留的代碼
aaa
ccc
bbb
ddd
<<<<<<< HEAD
master
=======
testing
>>>>>>> testing
[root@git ~/git_data]# git commit -am "merge testing to master" #提交
[master 5beb7bb] merge testing to master
[root@git ~/git_data]# git log --oneline --decorate
5beb7bb (HEAD, master) merge testing to master
23dec52 (testing) modified a on testing branch
a3d84f3 modified a master
0fcf3ce Merge branch 'testing'
8efaada commit master
0924a70 commit test
ef16b0b add ddd
6118c8d add bbb
18c89e4 modified a
1a85735 rename a.txt a
64e0f41 commit a.txt
c6b0ac2 new file a
#刪除分支
[root@git ~/git_data]# git branch -d testing
Deleted branch testing (was 23dec52).
[root@git ~/git_data]# git branch
* master
05. Git標簽使用
標簽也是指向了一次commit提交,是一個里程碑式的標簽,回滾打標簽直接加標簽號,不加唯一字符串不好記
# -a指定標簽名字 -m 指定說明文字
[root@git ~/git_data]# git tag -a v1.0 -m "aaa bbb master tesing version v1.0"
# 查看標簽
[root@git ~/git_data]# git tag
v1.0
# 指定某一次的提交為標簽
[root@git ~/git_data]# git tag -a v2.0 0924a70 -m "add bbb version v2.0"
# 查看v1.0的信息 git show 加標簽查看
[root@git ~/git_data]# git reset --hard v2.0
HEAD is now at 0924a70 commit test
[root@git ~/git_data]# ll
total 4
-rw-r--r-- 1 root root 16 Nov 16 15:04 a
-rw-r--r-- 1 root root 0 Nov 16 12:22 test
# 刪除標簽
[root@git ~/git_data]# git tag -d v2.0
Deleted tag 'v2.0' (was b0b964c)
[root@git ~/git_data]# git tag
v1.0
5. Github使用
Github顧名思義是一個Git版本庫的托管服務,是目前全球最大的軟件倉庫,擁有上百萬的開發者用戶,也是軟件開發和尋找資源的最佳途徑,Github不僅可以托管各種Git版本倉庫,還擁有了更美觀的Web界面,您的代碼文件可以被任何人克隆,使得開發者為開源項貢獻代碼變得更加容易,當然也可以付費購買私有庫,這樣高性價比的私有庫真的是幫助到了很多團隊和企業
1、注冊用戶 # 課前注冊好用戶
2、配置ssh-key
3、創建項目
4、克隆項目到本地
5、推送新代碼到github


#查看遠程倉庫
[root@git ~/git_data]# git remote
#添加遠程倉庫
[root@git ~/git_data]# git remote add origin git@github.com:qiuzengjia/git_data.git
[root@git ~/git_data]# git remote
origin
#將代碼推送到遠程倉庫,認證失敗
[root@git ~/git_data]# git push -u origin master
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
#生成SSH密鑰對
[root@git ~/git_data]# cd
[root@git ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:OTcZp1vPfXHDbMLB+3D/wW6/YSScpbC/JjMDIcqwCqo root@git
The key's randomart image is:
+---[RSA 2048]----+
| |
| . |
| ...o . |
| . . o == O |
| + . S *..X O.|
|. . o + +.oX.=|
|o . o .o*+|
|.. = .+.=|
|E *..o=|
+----[SHA256]-----+
[root@git ~]# cat .ssh/id_rsa
id_rsa id_rsa.pub
[root@git ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCkLibocGGe+jyPY2wNE50DogmPJnkjISWzSZjz8I5YBJT6bOsfA8spR/8CmGV+hmEk/ESbTbKx2yr+NeQYqoXF+8PVmKsiV51pUc+8pYYOgqzOwGKXZ6REdPFo6TeuzTwYtY2TFEoY2IolCeupKWUqA4dVOewJ/SfmAFqWcwa3fsbwH3EezO8tMNzKFz2wOYvSdYxZtEx6uKQYdcScQeJ6plAKl2kdqQ6ya/c+AR3B76pM5ItErxE5M2cVc35EcgEJLh71aXddJuepcXy76Rk0o/ISrMCcq/5wEdALYDlDEa+djO2oKaAoJcUmTEBl0n9hos3ifnCxK1d5KSINxQ5F root@git #將其復制粘貼到遠程倉庫中



#重新進行推送
[root@git ~]# cd git_data/
[root@git ~/git_data]# git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Counting objects: 17, done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (17/17), 1.25 KiB | 0 bytes/s, done.
Total 17 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To git@github.com:qiuzengjia/git_data.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
#下圖可以看到,代碼已經推送成功了。

#克隆遠程倉庫到本地進行測試
[root@git ~/git_data]# cd /opt/ #將其克隆到/opt目錄
[root@git /opt]# git clone git@github.com:qiuzengjia/git_data.git
Cloning into 'git_data'...
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (7/7), done.
Receiving objects: 100% (17/17), done.
Resolving deltas: 100% (1/1), done.
remote: Total 17 (delta 1), reused 17 (delta 1), pack-reused 0
[root@git /opt]# ll git_data/
total 4
-rw-r--r-- 1 root root 16 Nov 16 18:04 a
-rw-r--r-- 1 root root 0 Nov 16 18:04 test
6. Gitlab安裝
GitLab簡介
GitLab 是一個用於倉庫管理系統的開源項目。使用Git作為代碼管理工具,並在此基礎上搭建起來的web服務。可通過Web界面進行訪問公開的或者私人項目。它擁有與Github類似的功能,能夠瀏覽源代碼,管理權限和注釋。可以管理團隊對倉庫的訪問,它非常易於瀏覽提交過的版本並提供一個文件歷史庫。團隊成員可以利用內置的簡單聊天程序(Wall)進行交流。它還提供一個代碼片段收集功能可以輕松實現代碼復用。
常用的網站:
官網:https://about.gitlab.com/
國內鏡像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/
安裝環境:
1、CentOS 6或者7
2、2G內存(實驗)生產(至少4G)
3、安裝包:gitlab-ce-10.2.2-ce
4、禁用防火牆,關閉selinux
https://about.gitlab.com/installation/ # git官網




[root@git ~]# yum install -y curl policycoreutils-python openssh-server # 安裝依賴
#上傳gitlab安裝包
下載方式可通過國內清華源gitlab-ce社區版本下載
[root@git ~]# ll
drwxr-xr-x 3 root root 39 Nov 16 15:04 git_data
-rw-r--r-- 1 root root 389758391 Aug 22 2018 gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
#rpm安裝,發現缺少依賴
[root@git ~]# rpm -ivh gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
warning: gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
error: Failed dependencies:
policycoreutils-python is needed by gitlab-ce-10.2.2-ce.0.el7.x86_64
#安裝依賴之后再次安裝
[root@git ~]# yum install -y policycoreutils-python
[root@git ~]# vim /etc/gitlab/gitlab.rb # gitlab 配置文件
更改url地址為本機IP地址 external_url 'http://10.0.0.100'
# 更改配置文件后需重新配置
[root@git ~]# gitlab-ctl reconfigure
/opt/gitlab/ # gitlab的程序安裝目錄
/var/opt/gitlab # gitlab目錄數據目錄
/var/opt/gitlab/git-dfata # 存放倉庫數據
gitlab-ctl status # 查看目前gitlab所有服務運維狀態
gitlab-ctl stop # 停止gitlab服務
gitlab-ctl stop nginx # 單獨停止某個服務
gitlab-ctl tail # 查看所有服務的日志
Gitlab的服務構成:
nginx: #靜態web服務器
gitlab-workhorse: #輕量級的反向代理服務器
logrotate: #日志文件管理工具
postgresql: #數據庫
redis: #緩存數據庫
sidekiq: #用於在后台執行隊列任務(異步執行)。(Ruby)
unicorn:An HTTP server for Rack applications,GitLab Rails應用是托管在這個服務器上面的。(Ruby Web Server,主要使用Ruby編寫)
#通過瀏覽器輸入IP地址進行訪問gitlab
使用默認用戶登錄: root
密碼: 12345678


7. Gitlab使用
01. 外觀配置

02. Gitlab漢化配置
1、下載漢化補丁
git clone https://gitlab.com/xhang/gitlab.git
2、查看全部分支版本
git branch -a
3、對比版本、生成補丁包
git diff remotes/origin/10-2-stable remotes/origin/10-2-stable-zh > ../10.2.2-zh.diff
4、停止服務器
gitlab-ctl stop
5、打補丁
patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < /tmp/10.2.2-zh.diff
6、啟動和重新配置
gitlab-ctl start
gitlab-ctl reconfigure
03. 注冊限制

04. 創建用戶及組

05. 創建用戶



06. 把用戶添加到組

07. 創建項目



返回首頁,進入項目

#刪除github的倉庫
[root@git ~/git_data]# git remote remove origin
[root@git ~/git_data]# git remote
#添加gitlab的遠程倉庫,進行代碼上傳
[root@git ~/git_data]# git remote add origin git@10.0.0.100:OPS/git_test.git
[root@git ~/git_data]# git push -u origin master
The authenticity of host '10.0.0.100 (10.0.0.100)' can't be established.
ECDSA key fingerprint is SHA256:6gbyCCIw3zFuNSUR2Y7UOG8fbSrj/BVUaeXwllvrGXM.
ECDSA key fingerprint is MD5:95:10:02:7c:71:73:c6:4a:b2:f9:d8:88:5d:4a:3d:e0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.100' (ECDSA) to the list of known hosts.
Counting objects: 17, done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (17/17), 1.25 KiB | 0 bytes/s, done.
Total 17 (delta 1), reused 0 (delta 0)
To git@10.0.0.100:OPS/git_test.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

08. 推送代碼到Gitlab
[root@git ~/git_data]# echo oldboy >>a.txt
[root@git ~/git_data]# git add .
[root@git ~/git_data]# git commit -m "create a.txt"
[master a5c15de] create a.txt
1 file changed, 1 insertion(+)
create mode 100644 a.txt
[root@git ~/git_data]# git push -u origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.100:OPS/git_test.git
0924a70..a5c15de master -> master
Branch master set up to track remote branch master from origin.
#刷新Gitlab倉庫

09. 開發推送代碼到Gitlab

#克隆一台服務器,作為開發人員使用,並進行生成密鑰文件,進行密鑰認證
[root@dev ~]# 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:qx5BldjQ4qy+HTSH9zpg8nVQqJDNP/M8gfq/4k+E09c root@dev
The key's randomart image is:
+---[RSA 2048]----+
| +.=.o |
| o =.= . |
| +.+ o |
| .+.*o. . |
| .=.S*o.. E |
| o.+=.==. |
| . +o+..o. |
| ...++o |
| .o+.o=+. |
+----[SHA256]-----+
[root@dev ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDa4HBEOk10oode36lPcrybpjK++3bsjbaO67CjzY9S8w2WbAFfsVUwM+Hz8uRlehfjiCxuXF/oQgK2TATDdVYw89CrUMMmjuR1nRKKslA/Xhj0XxIjtUY+86UpVYp0t3963mmHrvoe45DTHYHIQqHEP8FM1fi5T6EzYrVhWFmmjqQYbVYQL+4oHwJBsCvQ4XZ6PCgTBlw2D8s1jk2Sx2vTsPLURA/+iL7WqzM5bGQLcAP5R3yG09v5O0+C/RLIHMw/qY54DaxVGTXhs0PnrfQhdzlUcKREsGkF7Ya3khyfss1jY+/QslS3Y6be9s8sfBKuWwx3Llob2nkgKiZOpQ+5 root@dev #復制其公鑰

#進行將代碼克隆下來,進行更改
[root@dev ~]# git clone git@10.0.0.100:OPS/git_test.git
Cloning into 'git_test'...
The authenticity of host '10.0.0.100 (10.0.0.100)' can't be established.
ECDSA key fingerprint is SHA256:6gbyCCIw3zFuNSUR2Y7UOG8fbSrj/BVUaeXwllvrGXM.
ECDSA key fingerprint is MD5:95:10:02:7c:71:73:c6:4a:b2:f9:d8:88:5d:4a:3d:e0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.100' (ECDSA) to the list of known hosts.
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 20 (delta 1), reused 0 (delta 0)
Receiving objects: 100% (20/20), done.
Resolving deltas: 100% (1/1), done.
[root@dev ~]# ll
drwxr-xr-x 3 root root 52 Nov 17 17:21 git_test
[root@dev ~]# ll git_test/
total 8
-rw-r--r-- 1 root root 16 Nov 17 17:21 a
-rw-r--r-- 1 root root 7 Nov 17 17:21 a.txt
-rw-r--r-- 1 root root 0 Nov 17 17:21 test
#修改代碼
[root@dev ~]# cd git_test/
[root@dev ~/git_test]# echo dev01 >> a
[root@dev ~/git_test]# git commit -am "dev01 add a"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@dev.(none)')
#配置郵件及用戶信息
[root@dev ~/git_test]# git config --global user.email "dev@example.com"
[root@dev ~/git_test]# git config --global user.name "dev01"
[root@dev ~/git_test]# git commit -am "dev01 add a"
[master 6cc6aff] dev01 add a
1 file changed, 1 insertion(+)
#推送到遠程倉庫
[root@dev ~/git_test]# git push -u origin master
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 305 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.100:OPS/git_test.git
a5c15de..6cc6aff master -> master
Branch master set up to track remote branch master from origin.

10. 分支保護
[root@dev ~/git_test]# git branch dev
[root@dev ~/git_test]# git push -u origin dev
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote: http://10.0.0.100/OPS/git_test/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To git@10.0.0.100:OPS/git_test.git
* [new branch] dev -> dev
Branch dev set up to track remote branch dev from origin.
#登錄root用戶,進行分支保護


11. 代碼合並
[root@dev ~/git_test]# git branch -d dev
Deleted branch dev (was 6cc6aff).
[root@dev ~/git_test]# git checkout -b dev
Switched to a new branch 'dev'
[root@dev ~/git_test]# git branch
* dev
master
#推送到遠程倉庫
[root@dev ~/git_test]# git push -u origin dev
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 311 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.100/OPS/git_test/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To git@10.0.0.100:OPS/git_test.git
6cc6aff..315f127 dev -> dev
Branch dev set up to track remote branch dev from origin.
#dev分支上面有a文件的最新信息,而master中沒有


進行合並分支



登錄root用戶進行處理請求



12. Git-gui安裝





















修改代碼




8. Gitlab備份與恢復
對gitlab進行備份將會創建一個包含所有庫和附件的歸檔文件。對備份的恢復只能恢復到與備份時的gitlab相同的
版本。將gitlab遷移到另一台服務器上的最佳方法就是通過備份和還原。
gitlab提供了一個簡單的命令行來備份整個gitlab,並且能靈活的滿足需求。
備份文件將保存在配置文件中定義的backup_path中,文件名為TIMESTAMP_gitlab_backup.tar,TIMESTAMP為備份時的時間戳。TIMESTAMP的格式為:EPOCH_YYYY_MM_DD_Gitlab‐version。如果自定義備份目錄需要賦予git權限。
01. 備份
#配置文件中加入
[root@git ~/git_data]# vim /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true #開啟備份
gitlab_rails['backup_path'] = '/data/backup/gitlab' #備份目錄
gitlab_rails['backup_archive_permissions'] = 0644 #生成的備份文件權限
gitlab_rails['backup_keep_time'] = 604800 #備份保留的時間(以秒為單位,這是七天默認值)
[root@git ~/git_data]# mkdir /data/backup/gitlab
[root@git ~/git_data]# chown ‐R git.git /data/backup/gitlab
#完成后執行下面命令進行生效
[root@git ~/git_data]# gitlab‐ctl reconfigure
#手動備份
[root@git ~/git_data]# gitlab-rake gitlab:backup:create
#檢查結果
[root@git ~/git_data]# ll /data/opt/gitlab/
total 112
-rw-r--r-- 1 git git 112640 Nov 17 18:22 1573986174_2019_11_17_10.2.2_gitlab_backup.tar
自動備份需要寫一個備份腳本及定時任務
#Gitlab恢復操作
Gitlab只能還原到與備份文件相同的gitlab版本。
#誤刪除數據
[root@git ~/git_test]# rm -rf ./*
#提交推送
[root@git ~/git_test]# git commit -am "add 123 "
[master 35ef629] add 123
3 files changed, 8 deletions(-)
delete mode 100644 a
delete mode 100644 a.txt
delete mode 100644 test
[root@git ~/git_test]# git push -u origin master
Counting objects: 3, done.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 182 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@10.0.0.100:OPS/git_test.git
02250b6..35ef629 master -> master
Branch master set up to track remote branch master from origin.

02. 恢復
[root@git ~/git_test]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@git ~/git_test]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 1s, normally up
[root@git ~/git_test]# gitlab-ctl status
run: gitaly: (pid 78774) 15714s; run: log: (pid 78466) 15758s
run: gitlab-monitor: (pid 78790) 15713s; run: log: (pid 78565) 15746s
run: gitlab-workhorse: (pid 78762) 15714s; run: log: (pid 78419) 15776s
run: logrotate: (pid 106694) 1364s; run: log: (pid 78447) 15764s
run: nginx: (pid 78432) 15770s; run: log: (pid 78431) 15770s
run: node-exporter: (pid 78515) 15752s; run: log: (pid 78514) 15752s
run: postgres-exporter: (pid 78818) 15712s; run: log: (pid 78692) 15728s
run: postgresql: (pid 78190) 15819s; run: log: (pid 78189) 15819s
run: prometheus: (pid 78806) 15713s; run: log: (pid 78646) 15734s
run: redis: (pid 78130) 15825s; run: log: (pid 78129) 15825s
run: redis-exporter: (pid 78626) 15740s; run: log: (pid 78625) 15740s
down: sidekiq: 8s, normally up; run: log: (pid 78402) 15782s
down: unicorn: 20s, normally up; run: log: (pid 78363) 15788s
#Gitlab的恢復操作會先將當前所有的數據清空,然后再根據備份數據進行恢復
[root@git ~/git_test]# ll /data/opt/gitlab/
total 112
-rw------- 1 git git 112640 Nov 17 18:22 1573986174_2019_11_17_10.2.2_gitlab_backup.tar
[root@git ~/git_test]# gitlab-rake gitlab:backup:restore BACKUP=1573986174_2019_11_17_10.2.2
#啟動gitlab
[root@git ~/git_test]# gitlab-ctl start
ok: run: gitaly: (pid 78774) 16168s
ok: run: gitlab-monitor: (pid 78790) 16167s
ok: run: gitlab-workhorse: (pid 78762) 16168s
ok: run: logrotate: (pid 106694) 1818s
ok: run: nginx: (pid 78432) 16224s
ok: run: node-exporter: (pid 78515) 16206s
ok: run: postgres-exporter: (pid 78818) 16166s
ok: run: postgresql: (pid 78190) 16273s
ok: run: prometheus: (pid 78806) 16167s
ok: run: redis: (pid 78130) 16279s
ok: run: redis-exporter: (pid 78626) 16194s
ok: run: sidekiq: (pid 109932) 1s
ok: run: unicorn: (pid 109939) 0s
#檢查是否恢復成功
[root@git ~/git_test]# gitlab-rake gitlab:check SANITIZE=true

#web界面數據已經恢復,從新克隆到本地
[root@git ~]# git clone git@10.0.0.100:OPS/git_test.git
Cloning into 'git_test'...
remote: Counting objects: 27, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 27 (delta 1), reused 27 (delta 1)
Receiving objects: 100% (27/27), done.
Resolving deltas: 100% (1/1), done.
[root@git ~/git_test]# ll
total 8
-rw-r--r-- 1 root root 27 Nov 17 19:00 a
-rw-r--r-- 1 root root 7 Nov 17 19:00 a.txt
-rw-r--r-- 1 root root 0 Nov 17 19:00 test
9. Jenkins
https://jenkins.io
Jenkins是一個開源軟件項目,是基於Java開發的一種持續集成工具,用於監控持續重復的工作,指在提供一個開
放易用的軟件平台,使軟件的持續集成變成可能。
01. 安裝准備
| 主機 | IP | 內存 | 硬盤 |
| Jenkins | 10.0.0.201 | 2G | 50G+ |
| nexus | 10.0.0.202 | 2G | 50G+ |
02 .安裝Jdk和Jenkins
#上傳JDK和Jenkins安裝包,使用rpm ‐ivh進行安裝,安裝完JDK運行Java測試是否安裝成功
[root@jenkins ~]# ll
-rw-r--r-- 1 root root 170023183 2018-08-14 11:05 jdk-8u181-linux-x64.rpm
-rw-r--r-- 1 root root 74141787 2018-08-13 20:23 jenkins-2.99-1.1.noarch.rpm
[root@jenkins ~]# rpm -ivh jdk-8u181-linux-x64.rpm
warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jdk1.8-2000:1.8.0_181-fcs ################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
[root@jenkins ~]# rpm -ivh jenkins-2.99-1.1.noarch.rpm
warning: jenkins-2.99-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jenkins-2.99-1.1 ################################# [100%]
03 .配置Jenkins
#啟動用戶修改為root
[root@jenkins ~]# grep 'root' /etc/sysconfig/jenkins
JENKINS_USER="root"
#啟動
[root@jenkins ~]# systemctl start jenkins
#查看端口
[root@jenkins ~]# netstat -lntp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 8504/java
#查看進程
[root@jenkins ~]# ps aux |grep jenkins
root 8504 8.8 11.6 2618104 235556 ? Ssl 15:29 0:25 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
#訪問頁面進行配置
http://10.0.0.201:8080
#查看密碼
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
e8e69c5646cc4f3b88315fb20156ce6c



修改密碼

04. 插件安裝
#插件安裝(跳過安裝插件,直接上傳插件到目錄)
1.自動安裝可選插件
2.手動下載插件上傳安裝
3.插件放入插件目錄
[root@jenkins ~]# cd /var/lib/jenkins/
[root@jenkins jenkins]# ll #jobs為每次構建后構建的結果目錄,plugins為插件目錄
總用量 36
‐rw‐‐‐‐‐‐‐ 1 root root 1822 8月 26 00:35 config.xml
‐rw‐‐‐‐‐‐‐ 1 root root 156 8月 26 00:31 hudson.model.UpdateCenter.xml
‐rw‐‐‐‐‐‐‐ 1 root root 1712 8月 26 00:32 identity.key.enc
‐rw‐‐‐‐‐‐‐ 1 root root 94 8月 26 00:32 jenkins.CLI.xml
‐rw‐r‐‐r‐‐ 1 root root 4 8月 26 00:35 jenkins.install.InstallUtil.lastExecVersion
‐rw‐r‐‐r‐‐ 1 root root 4 8月 26 00:35 jenkins.install.UpgradeWizard.state
drwxr‐xr‐x 2 root root 6 8月 26 00:31 jobs
drwxr‐xr‐x 3 root root 18 8月 26 00:32 logs
‐rw‐‐‐‐‐‐‐ 1 root root 907 8月 26 00:32 nodeMonitors.xml
drwxr‐xr‐x 2 root root 6 8月 26 00:32 nodes
drwxr‐xr‐x 2 root root 6 8月 26 00:31 plugins
‐rw‐‐‐‐‐‐‐ 1 root root 64 8月 26 00:31 secret.key
‐rw‐r‐‐r‐‐ 1 root root 0 8月 26 00:31 secret.key.not‐so‐secret
drwx‐‐‐‐‐‐ 4 root root 4096 8月 26 00:32 secrets
drwxr‐xr‐x 2 root root 23 8月 26 00:32 userContent
drwxr‐xr‐x 3 root root 18 8月 26 00:34 users
#上傳插件包解壓到plugins
[root@jenkins jenkins]# cd plugins/
[root@jenkins plugins]# ll
total 160580
-rw-r--r-- 1 root root 164431230 2018-08-14 21:00 plugins.tar.gz
[root@jenkins plugins]# tar xf plugins.tar.gz
[root@jenkins plugins]# rm -f plugins.tar.gz
[root@jenkins plugins]# mv plugins/* ./
[root@jenkins plugins]# rm -rf plugins/
#重啟生效
[root@jenkins plugins]# systemctl restart jenkins.service
#4.jenkins主要的目錄
/usr/lib/jenkins/: #jenkins安裝目錄,WAR包會放在這里
/etc/sysconfig/jenkins: #jenkins配置文件,"端口","JENKINS_HOME"等都可以在這里配置
/var/lib/jenkins/: #默認的JENKINS_HOME
/var/log/jenkins/jenkins.log: #Jenkins日志文件

05. 創建項目


進行構建


進入控制台


[root@jenkins plugins]# ll /var/lib/jenkins/workspace/freestyle-job
total 0




[root@jenkins plugins]# ll /var/lib/jenkins/workspace/freestyle-job
total 0
-rw-r--r-- 1 root root 0 2019-11-19 16:19 test.txt
06. Jenkins獲取Git源代碼
#這里我們有碼雲導入一個HTML頁面的監控平台到gitlab倉庫,打開碼雲,找到一個大轉盤項目,將其代碼路徑進行復制

在Gitlab上面新創建一個項目倉庫。將源代碼導入進去。


#dev用戶端配置從git獲取代碼。
[root@dev ~]# git clone git@10.0.0.100:OPS/dzp.git
Cloning into 'dzp'...
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 19 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (19/19), 104.36 KiB | 0 bytes/s, done.
Resolving deltas: 100% (2/2), done.
[root@dev ~]# ll
drwxr-xr-x 6 root root 87 Nov 19 20:41 dzp
drwxr-xr-x 3 root root 52 Nov 17 17:21 git_test
[root@dev ~]# cd dzp/
[root@dev ~/dzp]# ll
total 8
drwxr-xr-x 2 root root 25 Nov 19 20:41 css
drwxr-xr-x 2 root root 84 Nov 19 20:41 img
drwxr-xr-x 2 root root 41 Nov 19 20:41 js
-rw-r--r-- 1 root root 2170 Nov 19 20:41 lottery.html
-rw-r--r-- 1 root root 113 Nov 19 20:41 README.md
#dev用戶修改了源代碼
[root@dev ~/dzp]# vim lottery.html
[root@dev ~/dzp]#
[root@dev ~/dzp]#
[root@dev ~/dzp]# git commit -am "modify html"
[master 1e2125e] modify html
1 file changed, 5 insertions(+), 5 deletions(-)
[root@dev ~/dzp]# git push -u origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 365 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@10.0.0.100:OPS/dzp.git
3935eb7..1e2125e master -> master
Branch master set up to track remote branch master from origin.
#Jenkins配置從Git獲取代碼,由於我們dev用戶不是配置在Jenkins上,所以需認證即可下載代碼。進行面認證方法,需要配置deploy key
[root@jenkins ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:1z+H0mc9hUwOWiO3mFOGswuOW9jNr2bmOwRE50eECQU root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
| .E+o+o |
| .ooo |
| . = O . |
| . # B . |
| S.B o + .|
| = =.o o .o|
| o +.+ . =.=|
| o =. . =.|
| . =++. |
+----[SHA256]-----+
[root@jenkins ~]# cat .ssh/
id_rsa id_rsa.pub known_hosts
[root@jenkins ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCY3WrgTCmZeapjV9Tx6GmmHX+SVigxHnz37f8iUUjAexmR2/gSAjvsY3ez5iITmamf2I62+5n+gx9f1OPXUsUAzOApD6R8zHmvzQ/yheDO1y5XHcmRtklN1HpOq2g0PChrRjrr4QcXITKafU30OjTu4Fx3vndxqD/3RpHD3IVSZGAsiJC6T+C/PYW8YfeLNEAA7O3QKskjuSkoH2PZyF7qQgwjxfudno1g3qLQkmB+CjFFwgj0vkam/W4OwUwapC1O591CU7+VmSWL/z8uMTpSV+FQkyH04qS/HSs27pfpAI0wpBB/gBxx+wU8FCUh++2SWpuJR8/TPqkS8YKhfl+p root@jenkins #復制該串代碼


#Jenkins配置從Git獲取代碼,配置好后保存

07. 立即構建獲取源代碼


#Jenkins服務器查看代碼
[root@jenkins ~]# ll /var/lib/jenkins/workspace/freestyle-job/
total 8
drwxr-xr-x 2 root root 25 2019-11-19 21:12 css
drwxr-xr-x 2 root root 84 2019-11-19 21:12 img
drwxr-xr-x 2 root root 41 2019-11-19 21:12 js
-rw-r--r-- 1 root root 2205 2019-11-19 21:12 lottery.html
-rw-r--r-- 1 root root 113 2019-11-19 21:12 README.md
08. Jenkins代碼推送到Web
#寫一個腳本把從git倉庫里獲取的代碼上傳到web服務器站點目錄下
[root@jenkins ~]# mkdir -p /server/script
[root@jenkins ~]# cd /server/script
[root@jenkins script]# vim deploy.sh
[root@jenkins script]# cat deploy.sh
#!/bin/sh
Date=$(date +%s)
Code_Dir=/var/lib/jenkins/workspace/freestyle-job
Web_Dir=/code
Ip=10.0.0.202
Code_Tar() {
cd $Code_Dir && tar zcf /opt/web_${Date}.tar.gz ./*
}
Scp_Code_Web() {
scp /opt/web_${Date}.tar.gz root@${Ip}:/opt
}
Code_Tar_Xf() {
ssh root@$Ip " cd /opt && mkdir web_$Date && tar xf web_${Date}.tar.gz -C web_$Date "
}
Ln_Html() {
ssh root@$Ip " rm -rf $Web_Dir && ln -s /opt/web_$Date /code "
}
Code_Tar;
Scp_Code_Web;
Code_Tar_Xf;
Ln_Html
#分發公鑰
[root@jenkins script]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.202
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.202 (10.0.0.202)' can't be established.
ECDSA key fingerprint is SHA256:K8NapPzlTxhCMXC/bRFTtI9mdwr63FH4Wu7psrXXqBs.
ECDSA key fingerprint is MD5:73:9f:67:f1:5d:39:10:3d:b2:be:f7:c1:66:aa:00:6e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.202's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.0.0.202'"
and check to make sure that only the key(s) you wanted were added.
[root@jenkins script]# ssh root@10.0.0.202
Last login: Tue Nov 19 15:10:55 2019 from 10.0.0.1
[root@nexus ~]# logout
Connection to 10.0.0.202 closed.
#Jenkins上面添加腳本進行構建


查看網站是否更新成功

09. 配置自動觸發構建


Gitlab上面操作



10. 測試是否觸發
[root@dev ~/dzp]# vim lottery.html
[root@dev ~/dzp]# git commit -am "modify html test"
[master d8849aa] modify html test
1 file changed, 3 insertions(+), 3 deletions(-)
[root@dev ~/dzp]# git push -u origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 303 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@10.0.0.100:OPS/dzp.git
1e2125e..d8849aa master -> master
Branch master set up to track remote branch master from origin.


11. 返回構建狀態
#Jenkins配置Jenkins返回構建狀態到Gitlab
#先獲得gitlab的token



將獲得的token值進行復制備用





進行測試

添加構建后操作



檢查結果


10. 創建Maven項目
Maven是一個項目管理和綜合工具。Maven提供給開發人員構建一個完整的生命周期框架。
開發團隊可以自動完成該項目的基礎設施建設,Maven使用標准的目錄結構和默認構建生命周期。
Apache的開源項目主要服務於Java平台的構建、依賴管理、項目管理。
Project Object Model,項目對象模型。通過xml格式保存的pom.xml文件。該文件用於管理:源代碼、配置文
件、開發者的信息和角色、問題追蹤系統、組織信息、項目授權、項目的url、項目的依賴關系等等。該文件是由開發維護,我們運維人員可以不用去關心。
mvn package #會去maven的中央倉庫去下載需要的依賴包和插件到.m2目錄下
5、創建Maven私服nexus
部署私服 xenus 下載https://www.sonatype.com/download‐oss‐sonatype
配置倉庫兩個選項
1、項目下的pom.xml配置、只生效當前的項目
2、在maven配置全局所有項目生效
上傳JDK和nexus安裝包
rpm ‐ivh jdk‐8u121‐linux‐x64.rpm
mv nexus‐3.13.0‐01 /usr/local/
ln ‐s /usr/local/nexus‐3.13.0‐01 /usr/local/nexus
/usr/local/nexus/bin/nexus start
10.0.0.202:8081 admin admin123
配置Maven全局配置文件
/usr/local/maven/conf/settings.xml
6.創建一個Maven項目
創建前上傳代碼到gitlab服務器,把java項目添加到gitlab倉庫中
01. 部署Maven
官網: http://maven.apache.org/download.cgi
清華鏡像: https://mirrors.tuna.tsinghua.edu.cn/apache/maven/
#上傳軟件包
[root@jenkins ~]# ll
-rw-r--r-- 1 root root 8491533 2018-08-27 14:38 apache-maven-3.3.9-bin.tar.gz
#解壓
[root@jenkins ~]# tar xf apache-maven-3.3.9-bin.tar.gz
#改變目錄位置
[root@jenkins maven]# mv apache-maven-3.3.9 /usr/local/maven-3.3.9
#軟連接
[root@jenkins maven]# ln -s /usr/local/maven-3.3.9 /usr/local/maven
[root@jenkins ~]# cd /usr/local/maven
[root@jenkins maven]# ll
total 32
drwxr-xr-x 2 root root 97 2019-11-20 21:55 bin
drwxr-xr-x 2 root root 42 2019-11-20 21:55 boot
drwxr-xr-x 3 root root 63 2015-11-11 00:38 conf
drwxr-xr-x 3 root root 4096 2019-11-20 21:55 lib
-rw-r--r-- 1 root root 19335 2015-11-11 00:44 LICENSE
-rw-r--r-- 1 root root 182 2015-11-11 00:44 NOTICE
-rw-r--r-- 1 root root 2541 2015-11-11 00:38 README.txt
#設置環境變量
[root@jenkins maven]# echo "export PATH=/usr/local/maven/bin/:$PATH" >>/etc/profile
[root@jenkins maven]# source /etc/profile
#查看結果
[root@jenkins maven]# mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_181, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_181-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
02. 編譯測試
#上傳一個簡單的java項目包hello‐world.tar.gz進行解壓
[root@jenkins ~]# ll
-rw-r--r-- 1 root root 1325 2018-08-22 13:43 hello-world.tar.gz
[root@jenkins ~]# tar xf hello-world.tar.gz
[root@jenkins ~]# cd hello-world/
validate(驗證): 驗證項目正確,並且所有必要信息可用。
compile(編譯): 編譯項目源碼
test(測試): 使用合適的單元測試框架測試編譯后的源碼。
package(打包): 源碼編譯之后,使用合適的格式(例如JAR格式)對編譯后的源碼進行打包。
integration‐test(集成測試): 如果有需要,把包處理並部署到可以運行集成測試的環境中去。
verify(驗證): 進行各種測試來驗證包是否有效並且符合質量標准。
install(安裝): 把包安裝到本地倉庫,使該包可以作為其他本地項目的依賴。
deploy(部署): 在集成或發布環境中完成,將最終軟件包復制到遠程存儲庫,以與其他開發人員和項目共享。
mvn clean (清除) : 清除上次編譯的結果
#測試
[root@jenkins hello-world]# mvn test
#打包
[root@jenkins hello-world]# mvn package #會去maven的中央倉庫去下載需要的依賴包和插件到.m2目錄下
#打包結果
[root@jenkins hello-world]# ll target/
total 8
drwxr-xr-x 3 root root 17 2019-11-20 22:13 classes
-rw-r--r-- 1 root root 3130 2019-11-20 23:39 hello-world-1.0-SNAPSHOT.jar
drwxr-xr-x 2 root root 28 2019-11-20 23:38 maven-archiver
drwxr-xr-x 3 root root 35 2019-11-20 22:13 maven-status
-rw-r--r-- 1 root root 2872 2019-11-20 23:38 original-hello-world-1.0-SNAPSHOT.jar
drwxr-xr-x 2 root root 125 2019-11-20 22:14 surefire-reports
drwxr-xr-x 3 root root 17 2019-11-20 22:13 test-classes
03. 部署Tomcat及數據庫
#上傳壓縮包
[root@tomcat ~]# ll
-rw-r--r-- 1 root root 9128610 Mar 27 2019 apache-tomcat-8.0.27.tar.gz
-rw-r--r-- 1 root root 170023183 Aug 14 2018 jdk-8u181-linux-x64.rpm
#安裝JDK
[root@tomcat ~]# rpm -ivh jdk-8u181-linux-x64.rpm
warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jdk1.8-2000:1.8.0_181-fcs ################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
#解壓Tomcat
[root@tomcat ~]# mkdir /application
[root@tomcat ~]# tar xf apache-tomcat-8.0.27.tar.gz -C /application
#創建軟連接
[root@tomcat ~]# ln -s /application/apache-tomcat-8.0.27 /application/tomcat
#tomcat啟動加速的方法
[root@tomcat ~]# vim /usr/java/jdk1.8.0_181-amd64/jre/lib/security/java.security
117 securerandom.source=file:/dev/urandom #修改之后
#啟動tomcat
[root@tomcat ~]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
#檢查端口
[root@tomcat ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6758/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7134/master
tcp6 0 0 :::8009 :::* LISTEN 24383/java
tcp6 0 0 :::8080 :::* LISTEN 24383/java
tcp6 0 0 :::22 :::* LISTEN 6758/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7134/master
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 24383/java
#瀏覽器使用8080端口訪問

#為項目准備好數據庫jeesns,設置數據庫root用戶密碼為root
#安裝數據庫
[root@tomcat ~]# yum install mariadb-server -y
[root@tomcat ~]# systemctl start mariadb.service
[root@tomcat ~]# mysqladmin password 'root'
#創建jeesns庫
[root@tomcat ~]# mysql -uroot -proot -e "create database jeesns;"
#dev上面上傳一個項目
[root@git ~]# ll
-rw-r--r-- 1 root root 15376795 2019-03-27 17:33 jeesns.tar.gz
[root@git ~]# tar xf jeesns.tar.gz
[root@git ~]# cd jeesns/
[root@git ~/jeesns]# git remote
origin
[root@git ~/jeesns]# git remote remove origin
[root@git ~/jeesns]# ll jeesns-web/database/
total 40
-rwxr-xr-x 1 root root 28667 2018-11-19 15:01 jeesns.sql
-rw-r--r-- 1 root root 3491 2018-11-19 15:01 update_1.2.0to1.2.1.sql
-rw-r--r-- 1 root root 1026 2018-11-19 15:01 update_1.2.1to1.3.sql
-rw-r--r-- 1 root root 1344 2018-11-19 15:01 update_1.3to1.3.1.sql
#將該數據庫傳輸到tomcat節點進行導入
[root@git ~/jeesns]# scp jeesns-web/database/jeesns.sql root@10.0.0.80:~
#導入數據庫
[root@tomcat ~]# mysql -uroot -proot jeesns < jeesns.sql
[root@tomcat ~]# mysql -uroot -proot -e "use jeesns; show tables"
+---------------------------+
| Tables_in_jeesns |
+---------------------------+
| tbl_action |
| tbl_action_log |
| tbl_ads |
| tbl_archive |
| tbl_archive_favor |
| tbl_article |
| tbl_article_cate |
| tbl_article_comment |
| tbl_checkin |
| tbl_config |
| tbl_group |
| tbl_group_fans |
| tbl_group_topic |
| tbl_group_topic_comment |
| tbl_group_topic_type |
| tbl_group_type |
| tbl_link |
| tbl_member |
| tbl_member_fans |
| tbl_member_level |
| tbl_member_token |
| tbl_memgroup |
| tbl_message |
| tbl_picture |
| tbl_picture_album |
| tbl_picture_album_comment |
| tbl_picture_album_favor |
| tbl_picture_comment |
| tbl_picture_favor |
| tbl_picture_tag |
| tbl_score_detail |
| tbl_score_rule |
| tbl_tag |
| tbl_validate_code |
| tbl_weibo |
| tbl_weibo_comment |
| tbl_weibo_favor |
| tbl_weibo_topic |
+---------------------------+
#打包jeesns項目
[root@git ~/jeesns]# mvn package
#打包之后生成的war包
[root@git ~/jeesns]# ll jeesns-web/target/
total 25496
drwxr-xr-x 4 root root 181 2019-11-20 23:19 classes
drwxr-xr-x 3 root root 25 2019-11-20 23:19 generated-sources
drwxr-xr-x 5 root root 104 2019-11-20 23:19 jeesns-web
-rw-r--r-- 1 root root 26106028 2019-11-20 23:20 jeesns-web.war
drwxr-xr-x 2 root root 28 2019-11-20 23:19 maven-archiver
drwxr-xr-x 3 root root 35 2019-11-20 23:19 maven-status
#將其手動傳輸到tomcat節點上面測試
[root@git ~/jeesns]# scp jeesns-web/target/jeesns-web.war root@10.0.0.80:/application/tomcat/webapps/ROOT.war
#瀏覽器刷新測試

04. 創建一個jeesns項目
#清除上次編譯的結果
[root@git ~/jeesns]# mvn clean
[root@git ~/jeesns]# git remote remove origin
[root@git ~/jeesns]#
[root@git ~/jeesns]#
[root@git ~/jeesns]# git remote add origin git@10.0.0.100:OPS/jeesns.git
[root@git ~/jeesns]# git add .
[root@git ~/jeesns]# git commit -m "Initial commit"
# On branch master
nothing to commit, working directory clean
[root@git ~/jeesns]# git push -u origin master
Counting objects: 1946, done.
Compressing objects: 100% (1862/1862), done.
Writing objects: 100% (1946/1946), 7.09 MiB | 7.25 MiB/s, done.
Total 1946 (delta 285), reused 0 (delta 0)
remote: Resolving deltas: 100% (285/285), done.
To git@10.0.0.100:OPS/jeesns.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
05. Jenkins創建一個maven
#此處如果出現報錯,請添加Deploy Keys



#發現已經打包成功了
[root@jenkins ~]# ll /var/lib/jenkins/workspace/maven-job/jeesns-web/target/jeesns-web.war
-rw-r--r-- 1 root root 26106007 2019-11-23 11:49 /var/lib/jenkins/workspace/maven-job/jeesns-web/target/jeesns-web.war
#jenkins用戶給tomcat節點進行分發公鑰
[root@jenkins ~]# ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.80
#構建后操作
ssh root@10.0.0.80 "mv /application/tomcat/webapps/ROOT.war /tmp/$BUILD_ID-ROOT.war"
scp /var/lib/jenkins/workspace/maven-job/jeesns-web/target/jeesns-web.war root@10.0.0.80:/application/tomcat/webapps/ROOT.war


[root@tomcat ~]# ll /tmp/
total 50992
-rw-r--r-- 1 root root 26106004 2019-11-23 01:28 2-ROOT.war

11. Pipeline項目
01. 基礎概念
CI/CD持續集成/持續部署
持續集成(Continuous integration)是一種軟件開發實踐,即團隊開發成員經常集成它們的工作,通過每個成員
每天至少集成一次,也就意味着每天可能會發生多次集成。每次集成都通過自動化的構建(包括編譯,發布,自動
化測試)來驗證,從而盡早地發現集成錯誤。
比如(你家裝修廚房,其中一項是鋪地磚,邊角地磚要切割大小。如果一次全切割完再鋪上去,發現尺寸有誤的話
浪費和返工時間就大了,不如切一塊鋪一塊。這就是持續集成。)
持續部署(continuous deployment)是通過自動化的構建、測試和部署循環來快速交付高質量的產品。某種程度上代表了一個開發團隊工程化的程度,畢竟快速運轉的互聯網公司人力成本會高於機器,投資機器優化開發流程化
相對也提高了人的效率。
比如(裝修廚房有很多部分,每個部分都有檢測手段,如地磚鋪完了要測試漏水與否,線路鋪完了要通電測試電路
通順,水管裝好了也要測試冷水熱水。如果全部裝完了再測,出現問題可能會互相影響,比如電路不行可能要把地
磚給挖開……。那么每完成一部分就測試,這是持續部署。)
持續交付 Continuous Delivery:頻繁地將軟件的新版本,交付給質量團隊或者用戶,以供評審盡早發現生產環境中存在的問題;如果評審通過,代碼就進入生產階段.
比如(全部裝修完了,你去驗收,發現地磚顏色不合意,水池太小,灶台位置不對,返工嗎?所以不如沒完成一部
分,你就去用一下試用驗收,這就是持續交付。)
敏捷思想中提出的這三個觀點,還強調一件事:通過技術手段自動化這三個工作。加快交付速度。
1.什么是pipeline
Jenkins 2.0的精髓是Pipeline as Code,是幫助Jenkins實現CI到CD轉變的重要角色。什么是Pipeline,簡單來說,就是一套運行於Jenkins上的工作流框架,將原本獨立運行於單個或者多個節點的任務連接起來,實現單個
任務難以完成的復雜發布流程。Pipeline的實現方式是一套Groovy DSL,任何發布流程都可以表述為一段Groovy
腳本,並且Jenkins支持從代碼庫直接讀取腳本,從而實現了Pipeline as Code的理念。
2.Pipeline 概念
Pipeline 是一個用戶定義的 CD 流水線模式。Pipeline 代碼定義了通常包含構建、測試和發布步驟的完整的構
建過程。
Node
node 是一個機器,它是 Jenkins 環境的一部分,並且能夠執行 Pipeline。同時,node 代碼塊也是腳本式
Pipeline 語法的關鍵特性。
Stage
Stage 塊定義了在整個 Pipeline 中執行的概念上不同的任務子集(例如"構建","測試"和"部署"階段),
許多插件使用它來可視化或呈現 Jenkins 管道狀態/進度。
Step
一項任務。從根本上講,一個步驟告訴 Jenkins 在特定時間點(或過程中的"步驟")要做什么。例如,使用
sh step:sh 'make' 可以執行 make 這個 shell 命令。
3.jenkins file
聲明式
腳本式
腳本式語法格式:
pipeline{
agent any
stages{
stage("get code"){
steps{
echo "get code from scm"
}
}
stage("package"){
steps{
echo "packge code"
}
}
stage("deploy"){
steps{
echo "deploy packge to node1"
}
}
}
}
02. 創建pipeline項目


在倉庫創建一個Jenkinsfile文件進行調用



編輯Jenkinsfile文件
pipeline{
agent any
stages{
stage("get code"){
steps{
echo "get code"
}
}
stage("unit test"){
steps{
echo "unit test"
}
}
stage("package"){
steps{
sh 'tar zcf /opt/web-${BUILD_ID}.tar.gz ./* --exclude=./.git --exclude=Jenkinsfile'
}
}
stage("deploy"){
steps{
sh 'ssh 10.0.0.80 "mkdir /opt/web-${BUILD_ID}"'
sh 'scp /opt/web-${BUILD_ID}.tar.gz 10.0.0.80:/opt'
sh 'ssh 10.0.0.80 "tar xf /opt/web-${BUILD_ID}.tar.gz -C /code"'
}
}
}
}

執行構建報錯

修改腳本再次構建

















