实例----使用jenkins部署java应用到tomcat服务器
- 1.0 环境准备
- 1.1 Gitlab仓库中准备 Java 代码
- 1.1.1 开发机拉取公开的git项目并推送到自己的git仓库
- 1.2 安装 tomcat 服务器和配置
- 1.2.1 安装 tomcat
- 1.2.2 tomcat 服务器配置
- 1.3 Jenkins安装 Maven 和 Tomcat 插件
- 1.4 Jenkins 服务器上安装 maven 和配置镜像加速
- 1.5 Jenkins 全局工具配置 JDK 和 Maven
- 1.6 创建 Tomcat 的全局凭据
- 1.7 创建 Maven 风格的任务
- 1.8 构建验证
- 1.9 java应用版本更新
- 1.10 git参数构建
1.0 环境准备
节点名称 | 节点名称 | ip地址 |
开发机 | gc | 10.0.0.8 |
tomcat | tomcat | 10.0.0.50 |
jenkins | jenkins | 10.0.0.40 |
gitlab | gitlab | 10.0.0.30 |
1.1 Gitlab仓库中准备 Java 代码
1.1.1 开发机拉取公开的git项目并推送到自己的git仓库
[root@gc ~]# cd /data/
[root@gc data]# git clone https://gitee.com/lbtooth/hello-world-war.git
[root@gc data]# ls
aa.xtx hello-world-war meta2s www
---------------------------------------------------------
[root@gc hello-world-war]# git remote -v
origin https://gitee.com/lbtooth/hello-world-war.git (fetch)
origin https://gitee.com/lbtooth/hello-world-war.git (push)
[root@gc hello-world-war]# git remote rm origin
[root@gc hello-world-war]#
[root@gc hello-world-war]# git config --global user.name "blackjack"
[root@gc hello-world-war]# git config --global user.email "blackjack@11.com"
[root@gc hello-world-war]#
[root@gc hello-world-war]# git remote add origin git@gitlab.lec.org:dev1/hello-world-war.git
[root@gc hello-world-war]# git branch
* main
[root@gc hello-world-war]# git push -u origin --all
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (18/18), 3.49 KiB | 3.49 MiB/s, done.
Total 18 (delta 1), reused 18 (delta 1), pack-reused 0
remote:
remote:
remote: The private project dev1/hello-world-war was successfully created.
remote:
remote: To configure the remote, run:
remote: git remote add origin git@gitlab.lec.org:dev1/hello-world-war.git
remote:
remote: To view the project, visit:
remote: http://gitlab.lec.org/dev1/hello-world-war
remote:
remote:
remote:
To gitlab.lec.org:dev1/hello-world-war.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
1.2 安装 tomcat 服务器和配置
1.2.1 安装 tomcat
root@tomcat:~# apt -y install tomcat9 tomcat9-admin
测试登录.浏览器输入10.0.0.50:8080
1.2.2 tomcat 服务器配置
部署java应用至tomcat服务器,并需要提前创建用户和授权
root@tomcat:~# cp -r /usr/share/tomcat9-admin/* /var/lib/tomcat9/webapps/
root@tomcat:~# ls /var/lib/tomcat9/webapps/
host-manager manager ROOT
安装tomcat并配置用户和权限
root@tomcat:~# vim /var/lib/tomcat9/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script"/>
如图
配置远程链接权限
root@tomcat:~# vim /var/lib/tomcat9/webapps/manager/META-INF/context.xml
#注释掉这两行
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
重启tomcat服务
root@tomcat:~# systemctl restart tomcat9
1.3 Jenkins安装 Maven 和 Tomcat 插件
- Maven插件 : Maven Integration
- Tomcat 插件:Deploy to container
1.4 Jenkins 服务器上安装 maven 和配置镜像加速
# 安装maven
apt -y install maven
#镜像加速
root@jenkins:~# vim /etc/maven/settings.xml
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
#查看相关变量值
root@jenkins:~# mvn --version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-112-generic", arch: "amd64", family: "unix"
1.5 Jenkins 全局工具配置 JDK 和 Maven
jenkins上系统管理----全局工具配置
- 新增jdk
- 新增maven
1.6 创建 Tomcat 的全局凭据
根据tomcat的用户权限配置,创建jenkins连接tomcat的用户和权限
tomcat 账号 tomcat 密码 tomcat
1.7 创建 Maven 风格的任务
创建hello-world-war任务
源码管理git配置
输入maven的编译选项
clean package -Dmaven.test.skip=true
选择成功后执行shell,以及war包改名
构建后操作,添加tomcat容器
保存,开始构建
1.8 构建验证
立即构建
构建完成后,在访问tomcat 地址 http://10.0.0.50:8080/hello/ 发现应用成功
1.9 java应用版本更新
开发机 10.0.0.8上修改
[root@gc hello-world-war]# pwd
/data/hello-world-war
[root@gc hello-world-war]# vi src/main/webapp/index.jsp
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World! version 2.0 </h1>
<p>
It is now
<%= new java.util.Date() %></p>
<p>
You are coming from
<%= request.getRemoteAddr() %></p>
</body>
推送到git上
[root@gc hello-world-war]# git commit -am 'v2.0' ;git push origin main
[main c0da410] v2.0
1 file changed, 2 insertions(+), 2 deletions(-)
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 452 bytes | 452.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.lec.org:dev1/hello-world-war.git
157ee76..c0da410 main -> main
jenkins上再次执行任务,构建完成后,再次访问tomcat,发现变为2.0版本
1.10 git参数构建
hello-world-war任务,配置----参数化构建过程
下面的git指定分支改为刚才的参数
保存,然后再次构建选择版本
再次访问tomcat,发现回到java应用版本1