![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205031804277.png#crop=0&crop=0&crop=1&crop=1&id=wjgzf&originHeight=123&originWidth=387&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

### 介绍

C/S :  Client -  Server    客户端   服务器B/S： Browser - Server

Server AppCS ： 安装一个专门的客户端工具，才能访问APPBS ： 只需要有 标准的 浏览器软件 就能访问

Jenkins是一个开源的、可扩展的持续集成、交付、部署（软件/代码的编译、打包、部署）的基于web界面的平台。允许持续集成和持续交付项目，无论用的是什么平台，可以处理任何类型的构建或持续集成。

jenkins的作用和它的图标表现出来的一样，就是为了做工作的时候，能够比较轻松，像一个绅士一样游刃有余。

### Jenkins特性

开源的java语言开发的持续集成工具，支持CI，CD；易于安装部署配置：可通过yum安装，或下载war包以及通过docker容器等快速实现安装部署，可方便web界面配置管理；

消息通知及测试报告：集成RSS/E-mail通过RSS发布构建结果或当构建完成时通过e-mail通知，生成JUnit/TestNG测试报告；分布式构建：支持Jenkins能够让多台计算机一起构建/测试；文件识别:：Jenkins能够跟踪哪次构建生成哪些jar，哪次构建使用哪个版本的jar等；丰富的插件支持：支持扩展插件，你可以开发适合自己团队使用的工具，如git，svn，maven，docker等。

### Jenkins 和 Hudson

都是目前最流行的一款持续集成及自动化部署工具。Jenkins 和 Hundson 之间的关系：2009 年，甲骨文收购了 Sun 并继承了 Hudson 代 码库。在 2011 年年初，甲骨文和开源社区之间的关系破裂，该项目被分成两个独立的 项目：**Jenkins**：由大部分原始开发人员组成   **Hudson**：由甲骨文公司继续管理

所以 Jenkins 和 Hudson 是两款非常相似的产品。Jenkins 可以整合 GitHub 或 Subversion   Husband 也可以整合 GitHub 或 Subversion 二者既然是同源的工具软件，操作和指导思想就是接近的



![image-20221026232611714](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210262326831.png)





### JavaEE项目部署方式对比

#### 手动部署：

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205031808975.png#crop=0&crop=0&crop=1&crop=1&id=fIVz5&originHeight=323&originWidth=605&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 自动化部署：

“自动化”的具体体现：向版本库提交新的代码后，应用服务器上自动部署，用户 或测试人员使用的马上就是最新的应用程序。

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205031808423.png#crop=0&crop=0&crop=1&crop=1&id=VxfFp&originHeight=422&originWidth=785&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

搭建上述持续集成环境可以把整个构建、部署过程自动化，很大程度上减轻工作量。对于程序员的日常开发来说不会造成任何额外负担——自己把代码提交上去之后，服务器上运行的马上就是最新版本——一切都发生在无形中

### 网站

官网：[https://www.jenkins.io/](https://www.jenkins.io/)

插件下载地址：[https://plugins.jenkins.io/](https://plugins.jenkins.io/)中文社区文档： [https://www.jenkins.io/zh/doc/](https://www.jenkins.io/zh/doc/)中文下载站： [https://www.jenkins.io/zh/download/](https://www.jenkins.io/zh/download/)

### 实验环境
| 主机名 | IP | 备注 |
| --- | --- | --- |
| Jenkins | 192.168.245.187 |  |
| gitlab | 192.168.245.186 | gitlab |
| test | 192.168.245.185 | git、nginx |


### 部署

#### 下载安装

需要jdk的依赖（下载rpm包安装），可以选择去阿里源下载Jenkins的rpm包，官网选择lts版本（稳定版），2.289版本

rpm包内部会有一个tomcat的服务器不用另外安装，但是官网的war包需要

[https://mirrors.aliyun.com/jenkins/redhat/](https://mirrors.aliyun.com/jenkins/redhat/)

```shell
[root@Jenkins ~]# cd /opt/
[root@Jenkins opt]# yum -y install wget
[root@Jenkins opt]# wget https://mirrors.aliyun.com/jenkins/redhat-stable/jenkins-2.289.3-1.1.noarch.rpm
[root@jenkins opt]# wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.346.3-1.1.noarch.rpm
[root@jenkins opt]# yum install -y jenkins-2.332.3-1.1.noarch.rpm
[root@Jenkins opt]# yum -y install jenkins-2.289.3-1.1.noarch.rpm

注：安装的版本不同，需要的jdk也不同
yum -y install jenkins-2.332-1.1.noarch.rpm，要求jdk11
yum -y install jenkins-2.332.3-1.1.noarch.rpm，jdk8即可


```

##### 安装 jdk

###### 方法1：

```
[root@jenkins ~]# tar -zxvf jdk-8u191-linux-x64.tar.gz -C /usr/java
[root@Jenkins opt]# vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_191
export CLASSPATH=:.$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@Jenkins opt]# source !$
source /etc/profile
[root@Jenkins opt]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

[root@jenkins opt]# systemctl start jenkins
Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.
```

![image-20221027100918620](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271009720.png)

```
出现此错，原因是找不到jdk启动

[root@jenkins jenkins]# vim /etc/init.d/jenkins
99 candidates="
100 /etc/alternatives/java
101 /usr/lib/jvm/java-1.8.0/bin/java
102 /usr/lib/jvm/jre-1.8.0/bin/java
103 /usr/lib/jvm/java-11.0/bin/java
104 /usr/lib/jvm/jre-11.0/bin/java
105 /usr/lib/jvm/java-11-openjdk-amd64
106 /usr/bin/java

解决方案：
方法1：
[root@jenkins java]# ln -s  /usr/java/jdk1.8.0_191/bin/java /usr/bin/java
方法2：
或修改启动脚本的查找jdk的目录

[root@jenkins jenkins]# systemctl start jenkins
[root@jenkins jenkins]# systemctl status jenkins
```

![image-20221027103049509](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271030596.png)



###### 方法2：

```
安装openjdk
安装epel源
[root@jenkins opt]# yum install  java
[root@jenkins ~]# yum list |grep openjdk   
java-1.8.0-openjdk.i686                  1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk.x86_64                1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-accessibility.i686    1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-accessibility.x86_64  1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-demo.i686             1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-demo.x86_64           1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-devel.i686            1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-devel.x86_64          1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-headless.i686         1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-headless.x86_64       1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-javadoc.noarch        1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-javadoc-zip.noarch    1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-src.i686              1:1.8.0.345.b01-1.el7_9       updates  
java-1.8.0-openjdk-src.x86_64            1:1.8.0.345.b01-1.el7_9       updates 
。。。。

```



#### 启动

```shell
[root@Jenkins opt]# systemctl start jenkins.service
[root@Jenkins opt]# systemctl status jenkins.service
[root@Jenkins opt]# systemctl enable jenkins.service

```

### 配置

```shell
目录结构
[root@jenkins init.d]# rpm -ql jenkins
/etc/init.d/jenkins						#jenkins启动与停止脚本
/etc/logrotate.d/jenkins				#jenkins日志切割配置文件
/etc/sysconfig/jenkins					#jenkins配置文件
/usr/bin/jenkins
/usr/lib/systemd/system/jenkins.service
/usr/sbin/rcjenkins						#jenkins启动与停止脚本（软链接）
/usr/share/java/jenkins.war				#jenkins的程序文件（升级时替换这个文件即可）
/usr/share/jenkins
/usr/share/jenkins/migrate
/var/cache/jenkins						#jenkins缓存目录
/var/lib/jenkins						#jenkins主要目录
/var/log/jenkins						#jenkins日志相关	


# 存放插件的目录
[root@Jenkins opt]# cd /var/lib/jenkins/plugins/   # 如果是直接把以前的插件解压进来需要注意权限
[root@Jenkins plugins]# ls

[root@Jenkins updates]# ll /var/lib/jenkins/

# 浏览器输入IP地址:8080，如下图，按提示操作
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205031904618.png#crop=0&crop=0&crop=1&crop=1&id=q2EEw&originHeight=721&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

```shell
[root@Jenkins plugins]# cat /var/lib/jenkins/secrets/initialAdminPassword   # 生产上不要给其他人知道
ed91869da4284d079286c50b48233743
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032220190.png#crop=0&crop=0&crop=1&crop=1&id=FZB9b&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 1.选择安装推荐插件安装

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032221356.png#crop=0&crop=0&crop=1&crop=1&id=cItKt&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032241602.png#crop=0&crop=0&crop=1&crop=1&id=Dl8sK&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032242315.png#crop=0&crop=0&crop=1&crop=1&id=nilUQ&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032243796.png#crop=0&crop=0&crop=1&crop=1&id=RQR4s&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032245798.png#crop=0&crop=0&crop=1&crop=1&id=OMq2Q&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)



#### 2.自定义插件安装

可以选择“无”，不安装插件，跳过安装插件过程

![image-20221027112455061](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271124172.png)



跳过创建新用户，如下图所示

![image-20221027112716591](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271127679.png)



对比初始化时两种插件安装方法的不同

第一方法：

![image-20221027113607474](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271136584.png)

第二种方法：

![image-20221027113205719](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271132810.png)





#### 3.替换源

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032246655.png#crop=0&crop=0&crop=1&crop=1&id=cJj1l&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032248918.png#crop=0&crop=0&crop=1&crop=1&id=kWcqR&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

```shell
[root@Jenkins plugins]# cd /var/lib/jenkins/updates
[root@Jenkins updates]# ls
default.json  hudson.tasks.Maven.MavenInstaller
[root@Jenkins updates]# cp default.json default.json.bak
# 修改成清华源和百度源
[root@Jenkins updates]# sed -i 's#https://updates.jenkins.io/download#https://mirrors.tuna.tsinghua.edu.cn/jenkins#g' default.json && sed -i 's#http://www.google.com#https://www.baidu.com#g' default.json

# 验证是否修改成功
[root@Jenkins updates]# cat default.json | grep "tuna"

# plugin manager --> 高级 --> 升级站点
# 填写清华源的链接https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032300685.png#crop=0&crop=0&crop=1&crop=1&id=IYTW2&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)



![image-20221027114204210](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271142303.png)







![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032302539.png#crop=0&crop=0&crop=1&crop=1&id=GozqF&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)



#### 4.重启

方法1：

```shell
[root@Jenkins updates]# systemctl restart jenkins.service
[root@Jenkins updates]# systemctl status jenkins.service
# 重新登陆Jenkins即可，使用前面创建的用户进去
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032306075.png#crop=0&crop=0&crop=1&crop=1&id=Fp8ck&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

方法2：

在地址上加上/restart

![image-20221027114631247](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271146314.png)





#### 5.后期安装插件

| Jenkins插件安装方式                      |                                                              |
| ---------------------------------------- | ------------------------------------------------------------ |
| Web页面，选择与安装（联网）              | 一个一个找选择，安装重启jenkins                              |
| 自己下载插件，重启jenkins替你安装（hpi） | 一个一个安装，未来批量安装需要脚本，**修改所有者**           |
| 导入插件（手动批量)                      | 去一个机器上面把已经下载安装的插件目录打包，新机器解压，**修改所有者** |

安装中文汉化包为例

```
[root@jenkins plugins]# mv  localization-zh-cn* /opt
[root@jenkins plugins]# file workflow-step-api.jpi
workflow-step-api.jpi: Zip archive data, at least v1.0 to extract

#清华源下载插件安装包
[root@jenkins plugins]# wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/localization-zh-cn/1.0.24/localization-zh-cn.hpi --no-check-certificate

[root@jenkins plugins]# ll |grep localization-zh-cn
-rw-r--r-- 1 root    root      551108 11月 16 2020 localization-zh-cn.hpi

#重启jenkins
[root@jenkins plugins]# systemctl restart jenkins
[root@jenkins plugins]# ll |grep localization-zh-cn
drwxr-xr-x 5 jenkins jenkins       70 10月 27 14:44 localization-zh-cn
-rw-r--r-- 1 root    root      551108 11月 16 2020 localization-zh-cn.hpi

#对比jenkins下载与自已手动下载的插件安装包，md5码是一样的
[root@jenkins plugins]# md5sum localization-zh-cn.hpi
0f3e5cf389a983b5c424095a7c249336  localization-zh-cn.hpi
[root@jenkins plugins]# md5sum /opt/localization-zh-cn.jpi
0f3e5cf389a983b5c424095a7c249336  /opt/localization-zh-cn.jpi

注：jpi格式是jenkins web页面下载的
   hpi格式是官网或各种源里下载的
```



![image-20221027150639805](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271506893.png)

出现这种错：是因为插件解压包的属组不是jenkins

```
[root@jenkins plugins]# ll |grep localization-zh-cn
drwxr-xr-x 5 root    root          51 10月 27 14:56 localization-zh-cn
-rw-r--r-- 1 root    root      551108 11月 16 2020 localization-zh-cn.hpi
```





#### 6.插件包找不到时安装情况

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032319188.png#crop=0&crop=0&crop=1&crop=1&id=C00wA&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032321940.png#crop=0&crop=0&crop=1&crop=1&id=C0qM1&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205032322130.png#crop=0&crop=0&crop=1&crop=1&id=mTAiA&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**把gitlabhook.tar.gz上传到/var/lib/jenkins/plugins并解压**

```shell
[root@Jenkins plugins]# pwd
/var/lib/jenkins/plugins
[root@Jenkins plugins]# tar xf gitlabhook.tar.gz
[root@Jenkins plugins]# systemctl restart jenkins.service   # 重启
# 去web页面会找到Gitlab Hook Plugin插件已安装
```







### 忘记密码

#### 方法一:取消密码

```shell
# 进入Jenkins主目录，备份config.xml
cp config.xml config.xml.bak

# 修改config.xml文件 
# 找到关键词 useSecurity ，把后面的值 true 改为false


# 重启 Jenkins 
systemctl restart jenkins

# 可以无账号和密码登录Jenkins


```

#### 方法二:重置密码

```shell
# 修改 JENKINS_HOME/users/usernamedir 目录下的config.xml  文件

# 注：usernamedir 就是你忘记密码对应的用户文件夹
# 如果是java -jar方式启动的，HOME目录位于/root/.jenkins


# 修改config.xml，找到 passwordHash部分，替换原有的hash值



把
#jbcrypt:$2a$10$DdaWzN64JgUtLdvxWIflcuQu2fgrrMSAMabF5TSrGK5nXitqK9ZMS
修改为
#jbcrypt:$2a$10$4NW.9hNVyltZlHzrNOOjlOgfGrGUkZEpBfhkaUrb7ODQKBVmKRcmK
后者是123456的hash值

# 重启Jenkins 
systemctl restart jenkins

# 用最新的密码 123456 登录 

# 搞定
# 进去Jenkins修改复杂密码，要立马改，防止出现意外
```

```shell
[root@Jenkins workspace]# cd /var/lib/jenkins/users/
[root@Jenkins users]# ls
shy514_6070644599902000063  users.xml
[root@Jenkins users]# cd shy514_6070644599902000063/
[root@Jenkins shy514_6070644599902000063]# ls
config.xml
[root@Jenkins shy514_6070644599902000063]# cp config.xml config.xml.bak
[root@Jenkins shy514_6070644599902000063]# vim config.xml
[root@Jenkins shy514_6070644599902000063]# systemctl restart jenkins.service
```

### 





### 新建item

#### 测试item

##### 第一个项目：输出shell脚本

1.日志保留策略

![image-20221027152736183](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271527248.png)





参数化构建

**<font color='red'>如果没有This project is parameterized ，需要安装插件Git Parameter</font>**

![image-20221027152108274](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271521358.png)

选择“选项参数（choice parameter）”

![image-20221027162150961](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271621032.png)

![image-20221027162225190](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271622247.png)

![image-20221027162252108](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271622176.png)



![image-20221027162307762](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271623826.png)



![image-20221027162351000](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202210271623066.png)





##### 第二个项目：jenkins实现gitlab集成

1.代码准备



2.安装插件

| 插件名称                     | 插件作用                                   |
| ---------------------------- | ------------------------------------------ |
| Credentials Plugin           | 允许在jenkins中存储认证的凭据              |
| Git Client Plugin            | 允许jenkins使用Git                         |
| Git Plugin                   | 允许jenkins集成Git                         |
| Gitlab Plugin                | 允许Gitlabi触发jenkins构建并在Gitlab中显示 |
| GitLab Hook                  | 允许gitlab自动触发jenkins构建项目          |
| Gitlab Authentication plugin | gitlab身份验证插件                         |



![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040709322.png#crop=0&crop=0&crop=1&crop=1&id=BBnlx&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040711996.png#crop=0&crop=0&crop=1&crop=1&id=gS6f2&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040712074.png#crop=0&crop=0&crop=1&crop=1&id=cIOKu&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040713500.png#crop=0&crop=0&crop=1&crop=1&id=RHxXW&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 配置item

```shell
[root@Jenkins plugins]# yum -y install git
```

**凭证管理**

Manage Jenkins --> Manage Credentials

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040727020.png#crop=0&crop=0&crop=1&crop=1&id=H6spS&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040730424.png#crop=0&crop=0&crop=1&crop=1&id=YfyIw&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040731560.png#crop=0&crop=0&crop=1&crop=1&id=CBTaX&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040732648.png#crop=0&crop=0&crop=1&crop=1&id=yeQPV&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040734436.png#crop=0&crop=0&crop=1&crop=1&id=rcQMa&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040735782.png#crop=0&crop=0&crop=1&crop=1&id=AAnpX&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**完善item**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040715569.png#crop=0&crop=0&crop=1&crop=1&id=PnWBk&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040716347.png#crop=0&crop=0&crop=1&crop=1&id=aS2lB&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040725945.png#crop=0&crop=0&crop=1&crop=1&id=UQDwL&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040740406.png#crop=0&crop=0&crop=1&crop=1&id=b5XXX&originHeight=2618&originWidth=1329&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 测试构建

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040741411.png#crop=0&crop=0&crop=1&crop=1&id=HT7Hi&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040743885.png#crop=0&crop=0&crop=1&crop=1&id=vQ7UZ&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040745971.png#crop=0&crop=0&crop=1&crop=1&id=AxhxK&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

```shell
[root@Jenkins plugins]# cd /var/lib/jenkins/workspace/test
[root@Jenkins test]# ls
404.html         content-widgets.html     fonts                 index.html      media                    QHME.iml
alerts.html      css                      form-components.html  js              media.html               readme.md
assets           deviceManager.html       form-elements.html    keyInfo.html    messages.html            real-time.html
buttons.html     dianfei.html             form-examples.html    labels.html     mstp_105_SuperAdmin.iml  sa.html
calendar.html    efficiencyAnalysis.html  form-validation.html  LICENSE         mstp_map.html            tables.html
charts.html      energy_consumption.html  images-icons.html     list-view.html  other-components.html    typography.html
components.html  file-manager.html        img                   login.html      profile-page.html        userMng.html
```

```shell
# 修改默认所有者和所属组
# 不建议，所以在后面的shell命令脚本可以使用sudo提权
[root@Jenkins test]# vim /etc/sysconfig/jenkins
改: 29 JENKINS_USER="jenkins"
为: 29 JENKINS_USER="root"

[root@jenkins jenkins]# vim /usr/lib/systemd/system/jenkins.service
改：User=jenkins
改：Group=jenkins
为：User=root
为：Group=root

[root@Jenkins test]# systemctl restart jenkins.service
```

![image-20220610102827878](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202206101028984.png)





##### 第三个项目：免密拉取



jenkins与gitlab秘钥验证失败；

1、cmd：jenkins生成公钥对；公钥与私钥

2、web：gitlab上操作	 jenkins:将公钥推送到git1ab服务器对应的用户；（用户必须得对项目有拉取权限；）

3、web：jenkins上操作    告诉jenkins私钥是什么，这样才可以解密；Credentials



**在gitlab上进行对Jenkins的ssh免密**

```shell
[root@Jenkins test]# ssh-keygen -t RSA -N '' -f ~/.ssh/id_rsa
[root@Jenkins test]# ssh-copy-id 192.168.245.185

[root@Jenkins workspace]# cat ~/.ssh/id_rsa.pub
# 把公钥复制粘贴到gitlab上
# 再复制项目的ssh链接
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040848732.png#crop=0&crop=0&crop=1&crop=1&id=vNErb&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040850411.png#crop=0&crop=0&crop=1&crop=1&id=mgQMY&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**修改item**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040853122.png#crop=0&crop=0&crop=1&crop=1&id=d1Ano&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

```shell
[root@Jenkins workspace]# cat ~/.ssh/id_rsa
# 复制私钥进去
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040855817.png#crop=0&crop=0&crop=1&crop=1&id=FpzVg&originHeight=976&originWidth=1329&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040856986.png#crop=0&crop=0&crop=1&crop=1&id=kzCXi&originHeight=351&originWidth=1175&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**保存好重新构建验证**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040858822.png#crop=0&crop=0&crop=1&crop=1&id=aqyvP&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)



#### 部署item



```shell
# 在test机子上操作
[root@test ~]# cd /opt/
[root@test opt]# mkdir /opt/jenkins
[root@test opt]# grep -Ev "#|^$" /usr/local/nginx/conf/nginx.conf
worker_processes  1;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /opt/jenkins;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@test opt]# nginx -s reload
```

```shell
# 在Jenkins上操作
# 提权
[root@Jenkins test]# visudo
加: jenkins ALL=(ALL)       ALL

[root@Jenkins test]# visudo -c
/etc/sudoers：解析正确
```

**修改构建中shell命令**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040841355.png#crop=0&crop=0&crop=1&crop=1&id=xQ1VD&originHeight=434&originWidth=1181&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**build now --> 浏览器查看nginx页面，成功如下图**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205040843338.png#crop=0&crop=0&crop=1&crop=1&id=GnWPu&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**至此自动化部署的雏形已经形成**











### 参数化构建

#### 在gitlab上找到项目创建分支

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041019576.png#crop=0&crop=0&crop=1&crop=1&id=kZNjy&originHeight=553&originWidth=1107&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 修改test item的配置

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041023744.png#crop=0&crop=0&crop=1&crop=1&id=OSvRj&originHeight=1029&originWidth=1645&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**<font color='red'>如果没有Git Parameter，需要安装插件</font>**

![image-20220610111558100](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202206101115207.png)



![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041023920.png#crop=0&crop=0&crop=1&crop=1&id=U2oOz&originHeight=866&originWidth=1756&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041042983.png#crop=0&crop=0&crop=1&crop=1&id=vzPqe&originHeight=812&originWidth=1563&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 切换分支测试

在gitlab上切换到test分支上，创建一个测试文件

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041029626.png#crop=0&crop=0&crop=1&crop=1&id=tA0XH&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 开始构建

修改一下shell命令

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041035796.png#crop=0&crop=0&crop=1&crop=1&id=EvXa4&originHeight=418&originWidth=1186&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041037769.png#crop=0&crop=0&crop=1&crop=1&id=fpVNg&originHeight=631&originWidth=1344&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

```shell
# 验证
# 在test机子上
[root@test jenkins]# ls   # 发现1.html已经拉取下来
1.html           content-widgets.html     form-components.html  keyInfo.html    mstp_105_SuperAdmin.iml  tables.html
404.html         css                      form-elements.html    labels.html     mstp_map.html            typography.html
alerts.html      deviceManager.html       form-examples.html    LICENSE         other-components.html    userMng.html
assets           dianfei.html             form-validation.html  list-view.html  profile-page.html
buttons.html     efficiencyAnalysis.html  images-icons.html     login.html      QHME.iml
calendar.html    energy_consumption.html  img                   media           readme.md
charts.html      file-manager.html        index.html            media.html      real-time.html
components.html  fonts                    js                    messages.html   sa.html
```

### 版本回滚

并不是覆盖，而是回滚到指定版本号，选择好后直接构建即可(按照当时回滚版本的分支)，恢复的话就把分支改回参数引用；第二种方法是打tag，这里使用第一种比打tag方便

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041047949.png#crop=0&crop=0&crop=1&crop=1&id=YHQVW&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041047214.png#crop=0&crop=0&crop=1&crop=1&id=nb74e&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041048430.png#crop=0&crop=0&crop=1&crop=1&id=mel9T&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041048172.png#crop=0&crop=0&crop=1&crop=1&id=MVXan&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

### 视图

方便用去区分项目以及调整

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041059054.png#crop=0&crop=0&crop=1&crop=1&id=kG3Cn&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041059786.png#crop=0&crop=0&crop=1&crop=1&id=ykUqL&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041100969.png#crop=0&crop=0&crop=1&crop=1&id=DCGGY&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041101855.png#crop=0&crop=0&crop=1&crop=1&id=HN1fx&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041102773.png#crop=0&crop=0&crop=1&crop=1&id=JFZgG&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041103904.png#crop=0&crop=0&crop=1&crop=1&id=IpIDu&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

### 用户权限管理

#### 安装插件

安装这个插件[Role-based Authorization Strategy](https://plugins.jenkins.io/role-strategy)

在没有安装这个插件的时候，新创建用户是和管理员一样的权限，这是不允许的

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041107393.png#crop=0&crop=0&crop=1&crop=1&id=mxKJi&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 启用插件

在管理中启用插件

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041111482.png#crop=0&crop=0&crop=1&crop=1&id=MrpHZ&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041111407.png#crop=0&crop=0&crop=1&crop=1&id=u3hBk&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041111882.png#crop=0&crop=0&crop=1&crop=1&id=L2yuB&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 全局权限

新建一个base权限，只读；首先创建基础的全局权限，普通用户只需要读就可以

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041137295.png#crop=0&crop=0&crop=1&crop=1&id=tVd8E&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 项目权限

下图只是模拟增加，普通用户是不允许删除修改的

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041142942.png#crop=0&crop=0&crop=1&crop=1&id=MSApZ&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**分别为test和dev创建项目权限**

**创建项目权限，注意可以去掉普通用户的创建配置以及删除权限**

**项目权限需要匹配相关项目，可以写项目名字，也可以直接用正则，比如-test结尾，可以写成._-test；写完可以点击看能否匹配_

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041147449.png#crop=0&crop=0&crop=1&crop=1&id=YaO1i&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041147228.png#crop=0&crop=0&crop=1&crop=1&id=ggoZr&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

**最后点击save**

#### 创建用户

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041149103.png#crop=0&crop=0&crop=1&crop=1&id=oHEVA&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041150282.png#crop=0&crop=0&crop=1&crop=1&id=ySAFu&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041191375.png#crop=0&crop=0&crop=1&crop=1&id=zAkfu&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 分配权限

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041200153.png#crop=0&crop=0&crop=1&crop=1&id=Na3Yk&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041200394.png#crop=0&crop=0&crop=1&crop=1&id=JcwNd&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![image-20220610155916383](https://note-1308251438.cos.ap-guangzhou.myqcloud.com/typora/202206101559448.png)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041201084.png#crop=0&crop=0&crop=1&crop=1&id=Dtilv&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 验证

```shell
# 登陆到不同的账户只能看到权限所给予的和所能操作的
# 选择不同的分支进行构建
# 再回去服务器上查看会发现构建的项目或者分支不同，文件内容也不同
```

### 构建触发器

#### gitlab网络设置

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041218084.png#crop=0&crop=0&crop=1&crop=1&id=X0xub&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041218558.png#crop=0&crop=0&crop=1&crop=1&id=uElwh&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041218481.png#crop=0&crop=0&crop=1&crop=1&id=JuEip&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041219592.png#crop=0&crop=0&crop=1&crop=1&id=nFY09&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### Jenkins设置

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041222077.png#crop=0&crop=0&crop=1&crop=1&id=woch0&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041222945.png#crop=0&crop=0&crop=1&crop=1&id=rc4tG&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### gitlab的webhooks设置

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041225090.png#crop=0&crop=0&crop=1&crop=1&id=UAXct&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041225957.png#crop=0&crop=0&crop=1&crop=1&id=qsjjD&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

#### 测试推送

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041226995.png#crop=0&crop=0&crop=1&crop=1&id=m8cn4&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202205041228644.png#crop=0&crop=0&crop=1&crop=1&id=N2kBO&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)

```shell
# 此时只要gitlab上对应的项目进行了更改并且提交，Jenkins就会自动触发构建
```
