【Linux应用系列教程】基于Jenkins发布maven项目

一、环境准备

主机名IP地址服务器软件
jenkins.linux.com192.168.140.10Jenkins
tomcat.linux.com192.168.140.12Tomcat
gitlab.linux.com192.168.140.13Gitlab

二、服务器准备

部署过程省略 Jenkins部署教程:https://www.wsjj.top/archives/119 Gitlab部署教程:https://www.wsjj.top/archives/118 Tomcat部署教程:https://www.wsjj.top/archives/90

三、在Gitlab服务器上传项目

先创建一个空仓库

jenkins09

1.克隆项目

[root@gitlab ~]# git clone http://gitlab.linux.com/root/hello.git
正克隆到 'hello'...
Username for 'http://gitlab.linux.com': root
Password for 'http://root@gitlab.linux.com': 
warning: 您似乎克隆了一个空版本库。

2.上传项目

测试项目下载链接:https://pan.baidu.com/s/1-ct99Xaf045ITL-Nw_P2FQ?pwd=xr9z

jenkins07

[root@gitlab hello]# ls -a
.  ..  .classpath  .git  pom.xml  .project  .settings  src  target

3.上传项目文件到Gitlab

如果您是新配置的Git环境,请先配置用户名和email

[root@gitlab hello]# git add .

[root@gitlab hello]# git commit -m "hello world"

[master(根提交) 8a7a45b] hello world
 16 files changed, 187 insertions(+)
 create mode 100644 .classpath
 create mode 100644 .project
 create mode 100644 .settings/.jsdtscope
 create mode 100644 .settings/org.eclipse.jdt.core.prefs
 create mode 100644 .settings/org.eclipse.m2e.core.prefs
 create mode 100644 .settings/org.eclipse.wst.common.component
 create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.xml
 create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.container
 create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.name
 create mode 100644 .settings/org.eclipse.wst.validation.prefs
 create mode 100644 pom.xml
 create mode 100644 src/main/webapp/WEB-INF/web.xml
 create mode 100644 src/main/webapp/index.jsp
 create mode 100644 target/m2e-wtp/web-resources/META-INF/MANIFEST.MF
 create mode 100644 target/m2e-wtp/web-resources/META-INF/maven/hello/hello/pom.properties
 create mode 100644 target/m2e-wtp/web-resources/META-INF/maven/hello/hello/pom.xml
 
[root@gitlab hello]# git push -u origin master
Username for 'http://gitlab.linux.com': root
Password for 'http://root@gitlab.linux.com': 
Counting objects: 29, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (29/29), 3.83 KiB | 0 bytes/s, done.
Total 29 (delta 1), reused 0 (delta 0)
To http://gitlab.linux.com/root/hello.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

前往Gitlab端web界面查看

jenkins08

四、在Jenkins服务器上安装插件

  • git插件
  • gitlab插件
    • 作用:
      • 连接gitlab服务器拉取代码
  • maven intergration插件
    • 支持maven编译、发布
  • publish over ssh插件
    • 借助SSH协议连接tomcat服务器,发布项目

1.回到Jenkins端web界面添加插件

jenkins10

请自行搜索安装以下插件git、gitlab、Maven Integration plugin、Publish Over SSH 如果在软件商店搜索不到,就证明已经安装过了 下图中==仅展示==安装其中一个,剩下的自行搜索安装

jenkins11

如果不确定是否安装,可以搜索本地已安装的插件

jenkins12

五、配置Jenkins服务器与Tomcat服务器之间免密SSH

[root@jenkins ~]# ssh-keygen -t rsa
[root@jenkins ~]# ssh-copy-id root@192.168.140.12

测试免密SSH

[root@jenkins ~]# ssh root@192.168.140.12 hostname
tomcat.linux.com

六、在Jenkins服务器安装maven编译工具和Git工具

1.安装Git工具

[root@jenkins ~]# yum install -y git

2.安装maven编译工具

[root@jenkins ~]# wget --no-check-certificate https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz
[root@jenkins ~]# tar xf apache-maven-3.9.2-bin.tar.gz -C /usr/local/
[root@jenkins ~]# mv /usr/local/apache-maven-3.9.2/ /usr/local/maven39

配置环境变量

因为我们用yum装了jdk11,所以不用配置jdk环境

[root@jenkins ~]# vim /etc/profile
export PATH=$PATH:/usr/local/maven39/bin

[root@jenkins ~]# source /etc/profile

测试

[root@jenkins ~]# mvn -version
Apache Maven 3.9.2 (c9616018c7a021c1c39be70fb2843d6f5f9b8a1c)
Maven home: /usr/local/maven39
Java version: 11.0.19, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"

七、配置Jenkins连接Gitlab

  • 基于Gitlab令牌token验证

1.前往Gitlab端web界面生成token

jenkins13

jenkins14

jenkins15

2.在Jenkins端web界面添加Gitlab访问地址和令牌

jenkins16

jenkins17

jenkins18

jenkins19

报错解决方法

添加域名解析即可
[root@jenkins ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.140.13 gitlab.linux.com

再次回到Jenkins端web界面测试

jenkins20

八、配置Jenkins连接Tomcat服务器

1.准备Jenkins服务器私钥

[root@jenkins ~]# cat /root/.ssh/id_rsa
(这段原本贴的是一份真实的 id_rsa 私钥,已移除,请使用你自己的 ~/.ssh/id_rsa)

jenkins16

jenkins21

jenkins22

九、配置Jenkins全局工具

jenkins23

jenkins24

jenkins25

jenkins26

十、创建项目发布任务

1.创建任务

jenkins27

jenkins28

2.设置Gitlab连接

jenkins29

jenkins30

如果遇到报错请看下面

遇到这种问题解决方法也很简单 相信接触过Github或者Gitee的小伙伴都知道,在创建仓库的时候,会选择仓库是否为==公开仓库==或者是==私人仓库== 上面没有遇到报错,是因为我已经提前设置了==仓库为公开状态==

jenkins31

jenkins32

jenkins33

3.构建触发器

每30分钟拉取一次 这里的H代表串行执行 默认*/30代表并行执行,当服务增多的时候,代表所有服务都要同时拉取,同时编译,这样会造成服务器卡顿 而H/30代表串行执行,当服务增多的时候,会一个一个的编译安装,防止服务器卡顿

jenkins34

4.定义pom.xml文件路径

jenkins35

clean package -Dmaven.test.skip=true

5.设置构建后的操作

jenkins36

jenkins37

执行的操作(仅供参考)

export PATH=$PATH:/usr/local/jdk1.8.0_191/bin
/usr/local/tomcat85/bin/shutdown.sh
cp /app/project/*.war   /usr/local/tomcat85/webapps
/usr/local/tomcat85/bin/startup.sh

十一、执行任务

jenkins38

查看控制台输出信息

jenkins39

jenkins40

构建完成,浏览器访问测试

jenkins41

jenkins42