jenkins发布java项目
1.jenkins发布java项目
1.1 准备工作
java程序发布步骤:
- 拉取代码并打包
- 备份原程序
- 拷贝新程序到webapps目录中,等tomcat自动解压后修改配置
- 重启tomcat
- 验证
环境要求:
主机名 | IP | 软件环境 |
---|---|---|
Jenkins | 192.168.237.130 | tomcat、jenkins |
server | 192.168.237.167 | tomcat |
两台主机之间需要做免密登录
//两台主机都关闭防火墙和selinux并启动tomcat
[root@jenkins ~]# systemctl status firewalld.service
● 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)
[root@jenkins ~]# getenforce
Disabled
[root@jenkins ~]# ss -anltu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 100 *:8080 *:*
tcp LISTEN 0 128 [::]:22 [::]:*
tcp LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
[root@server ~]# systemctl status firewalld.service
● 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)
[root@server ~]# getenforce
Disabled
[root@server ~]# ss -anltu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
tcp LISTEN 0 100 *:8080 *:*
tcp LISTEN 0 128 [::]:22 [::]:*
1.2 手动发布
//下载拉取代码的软件包和打包代码的软件包
[root@jenkins ~]# yum -y install git
[root@jenkins ~]# yum -y install maven-1:3.5.4-5.module_el8.0.0+39+6a9b6e22.noarch
//拉取代码
[root@jenkins ~]# git clone https://gitee.com/forgotten/tomcat-java-demo.git
[root@jenkins ~]# ls
anaconda-ks.cfg tomcat-java-demo
//打包代码
[root@jenkins tomcat-java-demo]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src
[root@jenkins tomcat-java-demo]# mvn clean package
[root@jenkins tomcat-java-demo]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src target
//拷贝war包到server主机上的/usr/local/tomcat/webapps目录下
[root@jenkins tomcat-java-demo]# ls target/
classes ly-simple-tomcat-0.0.1-SNAPSHOT maven-archiver
generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-status
[root@jenkins tomcat-java-demo]# mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/myapp.war
[root@jenkins tomcat-java-demo]# ls target/
classes generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT maven-archiver maven-status myapp.war
[root@jenkins tomcat-java-demo]# scp target/myapp.war root@192.168.237.167:/usr/local/tomcat/webapps/
myapp.war 100% 17MB 123.8MB/s 00:00
//tomcat自动解压war包
[root@server ~]# ls /usr/local/tomcat/webapps/
docs examples host-manager manager myapp myapp.war ROOT
发布完成
1.3 jenkins发布
新建项目
选择流水线
开始构建
项目构建完成
测试