01 jenkins 部署


01.Devops是什么

? DevOps(Development和Operations的组合词)是一组过程、方法与系统的统称,用于促进开发(应用程序/软件工程)、技术运营和质量保障(QA)部门之间的沟通、协作与整合。

? 它是一种重视“软件开发人员(Dev)”和“IT运维技术人员(Ops)”之间沟通合作的文化、运动或惯例。透过自动化“软件交付”和“架构变更”的流程,来使得构建、测试、发布软件能够更加地快捷、频繁和可靠。

? 它的出现是由于软件行业日益清晰地认识到:为了按时交付软件产品和服务,开发和运维工作必须紧密合作。

02.Devops能干嘛

提高产品质量

1 自动化测试

2 持续集成

3 代码质量管理工具

03.Devops如何实现

设计架构规划-代码的存储-构建-测试、预生产、部署、监控

服务器准备:

 服务器 虚拟机 纯干净的系统 
 IP 主机名 配置 
 10.0.0.200 Gitlab  1核2G 20G硬盘     代码提交
 10.0.0.201 Jenkins 1核1G 20G硬盘     代码持续集成
 10.0.0.202 Nexus   1核2G 20G硬盘      
 10.0.0.203 Sonar   1核2G 20G硬盘     代码质量检测
 10.0.0.7 Web       1核1G 20G硬盘

02.Git版本控制系统

01.版本控制系统简介

? Git(读音为/g?t/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 [1] 也是[Linus Torvalds](https://baike.baidu.com/item/Linus Torvalds/9336769)为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件。

? Torvalds 开始着手开发 Git 是为了作为一种过渡方案来替代 BitKeeper [1] 。

02.为什么需要版本控制系统

? 方便版本切换,回退

image-20220408192404415

03.常见版本管理工具 :?

SVN ~集中式的版本控制系统

集中式的版本控制系统,只有一个中央数据仓库,如果中央数据仓库挂了或者不可访问,所有的使用者无法使用SVN,无法进行提交或备份文件。

image-20220408193640250

git ~分布式的版本控制系统

	分布式的版本控制系统,在每个使用者电脑上就有一个完整的数据仓库,没有网络依然可以使用Git。
	为了团队协作,会将本地数据同步到Git 服务器或者 GitHub等仓库

image-20220408193714920

03 .Git安装

01. 系统环境准备

[root@localhost ~ 19:07:18]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~ 19:47:04]# uname -r
3.10.0-1160.el7.x86_64
[root@localhost ~ 19:47:08]# getenforce 
Disabled
[root@localhost ~ 19:47:21]# systemctl stop firewalld

02. Git安装部署

yum install -y git
[root@localhost ~ 19:48:52]# git config
用法:git config [选项]

配置文件位置
    --global              使用全局配置文件
    --system              使用系统级配置文件
    --local               使用版本库级配置文件
#配置git使用用户
[root@localhost ~ 19:49:08]# git config --global user.name "abao"
#配置git使用邮箱
[root@localhost ~ 19:50:14]# git config --global user.email "1428363671@qq.com"
[root@localhost ~ 19:51:30]# git config --global color.ui true
[root@localhost ~ 19:51:14]# git config --list
user.name=abao
user.email=1428363671@qq.com
[root@localhost ~ 19:51:22]# cat .gitconfig 
[user]
	name = abao
	email = 1428363671@qq.com
[root@localhost ~ 19:51:40]# cat .gitconfig 
[user]
	name = abao
	email = 1428363671@qq.com
[color]
	ui = ture

03.git初始化

[root@localhost ~ 19:51:43]# mkdir git_data
[root@localhost ~ 19:53:36]# cd git_data
[root@localhost git_data 19:53:39]# git init
初始化空的 Git 版本库于 /root/git_data/.git/
[root@localhost git_data 19:53:43]# git status

04 .Git常规使用

01. 创建数据-提交数据

image-20220408195513003

02. git四种状态

Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged
Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件
Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改
Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified

03. git基础命令

[root@localhost git_data 20:06:07]# touch a b c
[root@localhost git_data 20:07:04]# ls
a  b  c
[root@localhost git_data 20:07:05]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add ..." 以包含要提交的内容)
#
#	a
#	b
#	c
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost git_data 20:07:08]# git add a
[root@localhost git_data 20:07:22]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached ..." 撤出暂存区)
#
#	新文件:    a
#
# 未跟踪的文件:
#   (使用 "git add ..." 以包含要提交的内容)
#
#	b
#	c
[root@localhost git_data 20:07:27]# ll .git/
总用量 16
drwxr-xr-x 2 root root   6 4月   8 19:53 branches
-rw-r--r-- 1 root root  92 4月   8 19:53 config
-rw-r--r-- 1 root root  73 4月   8 19:53 description
-rw-r--r-- 1 root root  23 4月   8 19:53 HEAD
drwxr-xr-x 2 root root 242 4月   8 19:53 hooks
-rw-r--r-- 1 root root  96 4月   8 20:07 index  #把文件提交到暂存区
drwxr-xr-x 2 root root  21 4月   8 19:53 info
drwxr-xr-x 5 root root  40 4月   8 20:07 objects
drwxr-xr-x 4 root root  31 4月   8 19:53 refs

01 使用git add . 或者* 添加目录中所有改动的文件

[root@localhost git_data 20:07:44]# git add .   
[root@localhost git_data 20:08:14]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached ..." 撤出暂存区)
#
#	新文件:    a
#	新文件:    b
#	新文件:    c

02 删除文件

#方式1.先从暂存区撤回到工作区,然后直接删除文件
[root@localhost git_data 20:08:19]# git rm --cached c
rm 'c'
[root@localhost git_data 20:09:08]# rm -f c

#方式2 直接从暂存区同工作区域一同删除文件命令
[root@localhost git_data 20:09:14]# git rm -f b
rm 'b'

03 git commit提交到本地仓库

[root@localhost git_data 20:09:24]# git commit -m "commit a"
[master(根提交) dc3642f] commit a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
[root@localhost git_data 20:09:40]# git status 
# 位于分支 master
无文件要提交,干净的工作区

04 修改文件名称的两种方法

1.
[root@localhost git_data 20:09:48]# mv a a.txt
[root@localhost git_data 20:10:02]# git status 
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#	删除:      a
#
# 未跟踪的文件:
#   (使用 "git add ..." 以包含要提交的内容)
#
#	a.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost git_data 20:10:05]# git rm --cached a
rm 'a'
[root@localhost git_data 20:10:21]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD ..." 撤出暂存区)
#
#	删除:      a
#
# 未跟踪的文件:
#   (使用 "git add ..." 以包含要提交的内容)
#
#	a.txt
[root@localhost git_data 20:10:23]# git add a.txt
[root@localhost git_data 20:10:55]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD ..." 撤出暂存区)
#
#	重命名:    a -> a.txt
#
2.直接用git 命令重命名
[root@localhost git_data 20:10:57]# git mv a.txt a

05 git status 只能查看区域状态不同,不能查看文件内容的变化

[root@localhost git_data 20:11:14]# git status 
# 位于分支 master
无文件要提交,干净的工作区

06 git diff 对比本地工作目录和暂存区文件的不同

[root@localhost git_data 20:33:31]# echo aaa >a
[root@localhost git_data 20:33:52]# git add .
[root@localhost git_data 20:34:06]# echo ccc >a

07 本地区和暂存区的不同

[root@localhost git_data 20:34:16]# git diff a
diff --git a/a b/a
index 72943a1..b2a7546 100644
--- a/a
+++ b/a
@@ -1 +1 @@
-aaa
+ccc
[root@localhost git_data 20:34:18]# git add .

08 本地区和暂存区相同

[root@localhost git_data 20:34:32]# git diff a

09 git commit 相当于虚拟机镜像,任何操作都被做了一次快照,可恢复到任意一个位置

[root@localhost git_data 20:12:07]# git commit -m "modified a"
[master f533f80] modified a
 1 file changed, 1 insertion(+)
[root@localhost git_data 20:12:47]# git diff --cached a

10 git log 查看历史的git commit 快照操作

[root@localhost git_data 20:13:02]# git log
commit f533f8018d4fa7ce0b7f6d6b46b02b7413cf91ed
Author: abao <1428363671@qq.com>
Date:   Fri Apr 8 20:12:47 2022 +0800

    modified a

commit dc3642fed2ba89af39ea4077ed85c0a09e1a4c7a
Author: abao <1428363671@qq.com>
Date:   Fri Apr 8 20:09:40 2022 +0800

    commit a

11 git log --online 简单的显示git commit信息

[root@localhost git_data 20:13:16]# git log --oneline 
f533f80 modified a
dc3642f commit a

12 显示当前指针指向哪里

[root@localhost git_data 20:13:44]# git log --oneline --decorate
f533f80 (HEAD, master) modified a
dc3642f commit a

13 显示具体内容的变化

[root@localhost git_data 20:13:56]# git log -p
commit f533f8018d4fa7ce0b7f6d6b46b02b7413cf91ed
Author: abao <1428363671@qq.com>
Date:   Fri Apr 8 20:12:47 2022 +0800

    modified a

diff --git a/a b/a
index e69de29..58c9bdf 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+111

commit dc3642fed2ba89af39ea4077ed85c0a09e1a4c7a
Author: abao <1428363671@qq.com>
Date:   Fri Apr 8 20:09:40 2022 +0800

    commit a

diff --git a/a b/a
new file mode 100644
index 0000000..e69de29

14 显示一条内容

[root@localhost git_data 20:14:08]# git log -1
commit f533f8018d4fa7ce0b7f6d6b46b02b7413cf91ed
Author: abao <1428363671@qq.com>
Date:   Fri Apr 8 20:12:47 2022 +0800

    modified a

15 恢复历史数据

1.只更改当前目录
[root@localhost git_data 20:14:12]# echo "333" >>a
[root@localhost git_data 20:14:38]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#	修改:      a
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost git_data 20:14:43]# git checkout -- a
[root@localhost git_data 20:15:03]# git status
# 位于分支 master
无文件要提交,干净的工作区

2. 修改了本地目录且同时提交到暂存区
[root@localhost git_data 20:15:13]# echo ccc >a
[root@localhost git_data 20:15:25]# git add .
[root@localhost git_data 20:15:30]# git diff --cached 
diff --git a/a b/a
index 58c9bdf..b2a7546 100644
--- a/a
+++ b/a
@@ -1 +1 @@
-111
+ccc
[root@localhost git_data 20:15:40]# git reset HEAD a
重置后撤出暂存区的变更:
M	a
[root@localhost git_data 20:16:03]# git diff a
diff --git a/a b/a
index 58c9bdf..b2a7546 100644
--- a/a
+++ b/a
@@ -1 +1 @@
-111
+ccc
[root@localhost git_data 20:16:13]# git diff --cached a

3.修改了工作目录后提交到了暂存区和本地仓库进行数据恢复
PS:如遇回退错了,又因为回退后找不到一些版本。可通过git reflog 查看历史版本
[root@localhost git_data 20:16:37]# echo bbb >a
[root@localhost git_data 20:16:48]# git commit -m "add bbb"
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#	修改:      a
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost git_data 20:17:03]# echo ccc >>a
[root@localhost git_data 20:17:13]# git commit -m "add ccc"
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#	修改:      a
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost git_data 20:17:18]# git log --oneline 
f533f80 modified a
dc3642f commit a
[root@localhost git_data 20:17:24]# echo eee >>a
[root@localhost git_data 20:17:38]# git commit -m "add eee"
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#	修改:      a
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost git_data 20:17:42]# git log --oneline 
f533f80 modified a
dc3642f commit a
[root@localhost git_data 20:17:44]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#	修改:      a
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost git_data 20:18:10]# git reset --hard dc3642f
HEAD 现在位于 dc3642f commit a
[root@localhost git_data 20:18:26]# cat a 
[root@localhost git_data 20:18:36]# ls
a
[root@localhost git_data 20:18:38]# git reset --hard f533f80
HEAD 现在位于 f533f80 modified a
[root@localhost git_data 20:18:52]# cat a 
111
[root@localhost git_data 20:18:54]# git log --oneline 
f533f80 modified a
dc3642f commit a
[root@localhost git_data 20:19:03]# git reflog 
f533f80 HEAD@{0}: reset: moving to f533f80
dc3642f HEAD@{1}: reset: moving to dc3642f
f533f80 HEAD@{2}: commit: modified a
dc3642f HEAD@{3}: commit (initial): commit a
[root@localhost git_data 20:19

04.git标签使用

标签也是指向了一次commit提交,是一个里程碑式的标签,回滚打标签直接加标签号,不需要加唯一字符串不好记

[root@localhost git_data 20:41:46]# git tag -a v1.0 -m "aaa bbb master tesing version v1.0" # -a指定标签名字 -m 指定说明文字
[root@localhost git_data 21:16:59]# git tag -a v2.0 dc3642f -m "add bbb  version v1.0"   # 指定某一次的提交为标签
[root@localhost git_data 21:17:58]# git show v1.0   # 查看v1.0的信息 git show 加标签查看
tag v1.0
Tagger: abao <1428363671@qq.com>
Date:   Fri Apr 8 21:16:50 2022 +0800

aaa bbb master tesing version v1.0

commit f533f8018d4fa7ce0b7f6d6b46b02b7413cf91ed
Author: abao <1428363671@qq.com>
Date:   Fri Apr 8 20:12:47 2022 +0800

    modified a

diff --git a/a b/a
index e69de29..58c9bdf 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+111
[root@localhost git_data 21:18:12]# git reset --hard v2.0  # 直接还原数据到v2.0
HEAD 现在位于 dc3642f commit a
[root@localhost git_data 21:18:51]# git tag -d v2.0   # 删除标签 -d参数
已删除 tag 'v2.0'(曾为 f345044)

05. github使用

01.Github介绍

? Github顾名思义是一个Git版本库的托管服务,是目前全球最大的软件仓库,拥有上百万的开发者用户,也是软件开发和寻找资源的最佳途径,Github不仅可以托管各种Git版本仓 库,还拥有了更美观的Web界面,您的代码文件可以被任何人克隆,使得开发者为开源项贡献代码变得更加容易,当然也可以付费购买私有库,这样高性价比的私有库真的是帮助到了很多团队和企业

02.github使用

1、注册用户

2、配置ssh-key

3、创建项目

4、克隆项目到本地

5、推送新代码到github

06. gitlab安装

01.GitLab简介

02.环境准备

安装环境:
1、 CentOS 6或者7 
2、 2G内存(实验)生产(至少4G) 
3、 安装包:gitlab-ce-10.2.2-ce 
4、 禁用防火墙,关闭selinux

yum install -y curl policycoreutils-python openssh-server # 安装依赖
wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm/download.rpm  #下载rpm 包
rpm -ivh gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm 

[root@localhost gitlab 00:03:46]# cat /etc/gitlab/gitlab.rb |grep -v "#" |grep -Ev "^$"   #修改配置文件
external_url 'http://10.0.0.140'
unicorn['listen'] = '10.0.0.140'
unicorn['port'] = 8081
[root@localhost gitlab 00:04:03]# gitlab-ctl reconfigure  #初始化配置
[root@localhost gitlab 00:04:03]# gitlab-ctl status

输入网址,创建密码 登录:用户名:root 密码:12345678

image-20220409000724624

03.gitlab汉化:

04. gitlab配置使用

05. gitlab备份

07. JenKins

01.Jenkins介绍

02.安装Jenkins

JDK 安装

# 安装
tar xf jdk-8u181-linux-x64.tar.gz -C /usr/local/src/
ln -sv /usr/local/src/jdk1.8.0_181 /usr/local/jdk
cat > /etc/profile.d/jdk.sh << \EOF
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$PATH
EOF
source /etc/profile.d/jdk.sh
java -version

jenkins 安装

rpm -ivh jenkins-2.99-1.1.noarch.rpm
JENKINS_USER="root"
ln -sv /usr/local/jdk/bin/java /usr/bin/java
systemctl stop firewalld.service
[root@localhost jenkins 23:10:00]# cat /var/lib/jenkins/secrets/initialAdminPassword 
04076c99df124b199fd2522021e7916b
admin 12345678

03.插件安装


04.创建自由风格项目

08.Jenkins实现参数化构建

01.参数化构建介绍

02.插件安装

03.参数设置

04.脚本传参

09.创建Maven项目

01.Maven介绍

02.Mavne安装

10.创建Maven私服nexus

01.安装配置私服

02.创建Maven项目

11.代码质量检测SonarQube

01.sonarqube介绍

02.安装软件

03.配置sonar连接本地数据库

04.配置管理sonar 安装插件

05.集成jenkins测试代码

12.Jenkins集成微信

01.实现目标

\02. 注册微信企业公众号

03.配置jenkins

13 Jenkins Pipeline项目

01.CI/CD持续集成/持续部署

02.pipeline介绍

03.jenkins file

04.创建pipeline项目01.分布式构建介绍

05.部署分布式服务器

14.代码发布流程

01.直接发布

02.金丝雀发布

03.滚动式发布

04.双服务器组发布