微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

tomcat任务部署

tomcat项目部署

主机 IP 需求
web-1 192.168.78.144 tomcat+jenkins
web-2 192.168.78.145 tomcat

在RHEL-1主机上部署的jenkins将项目打包传输到RHEL-2主机上并可以实现网页效果

新建任务

输入任务名称选择任务类型

查看流水线语法

生成流水线脚本页面

选择并复制项目的分支和链接

点击生成流水线脚本

拉取代码

pipeline {
    agent any 
    stages {
        stage("pull code"){
            steps {
                git 'https://github.com/chendao2015/tomcat-java-demo.git'
            }
            
        }  
    }
}

应用并保存

查看是否完成

查看状态日志

完成后的任务保存路径

RHEL-1主机上查看

[root@localhost ~]# cd .jenkins/workspace/test/
[root@localhost test]# ls
db  Dockerfile  LICENSE  pom.xml  README.md  src
[root@localhost test]# 

安装maven打包工具

[root@localhost ~]# yum install maven -y

编写打包脚本

pipeline {
    agent any 
    stages {
        stage("pull code"){
            steps {
                git 'https://github.com/chendao2015/tomcat-java-demo.git'
            }
        }    
        stage("make pkg"){
            steps {
                sh """
                    mvn clean package -Dmaven.test.skip=true
                """
            }
        }  
    }
}

完成打包

//可以看到打包后新生成的target目录
[root@localhost ~]# cd .jenkins/workspace/test/
[root@localhost test]# cd target/
[root@localhost target]# ls
classes            ly-simple-tomcat-0.0.1-SNAPSHOT      maven-archiver
generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT.war  maven-status

配置免密登录

[root@web-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNowN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <broADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:06:f1:f5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.78.144/24 brd 192.168.78.255 scope global dynamic noprefixroute ens160
       valid_lft 1229sec preferred_lft 1229sec
    inet6 fe80::20c:29ff:fe06:f1f5/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@web-1 ~]# ssh root@192.168.78.145
Last login: Thu Aug 18 08:55:30 2022 from 192.168.78.144
[root@web-2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNowN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <broADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:93:62:36 brd ff:ff:ff:ff:ff:ff
    inet 192.168.78.145/24 brd 192.168.78.255 scope global dynamic noprefixroute ens160
       valid_lft 1218sec preferred_lft 1218sec
    inet6 fe80::20c:29ff:fe93:6236/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

完善脚本

pipeline {
    agent any 
    stages {
        stage("pull code"){
            steps {
                git 'https://github.com/chendao2015/tomcat-java-demo.git'
            }
        }    
        stage("make pkg"){
            steps {
                sh """
                    mvn clean package -Dmaven.test.skip=true
                """
            }
        } 
        stage("publish"){
            steps {
                sh """
                    cd /root/.jenkins/workspace/test/target/
                   scp ly-simple-tomcat-0.0.1-SNAPSHOT.war   root@192.168.220.30:/usr/local/tomcat/webapps/
                   ssh root@192.168.78.145 '/usr/local/tomcat/bin/catalina.sh stop && /usr//local/tomcat/bin/catalina.sh start'
                   """
            }
        }
    }
}

构建成功

在web-2主机上查看

[root@web-2 ~]# cd /usr/local/tomcat/webapps/
[root@web-2 webapps]# ls
docs      host-manager                     ly-simple-tomcat-0.0.1-SNAPSHOT.war  ROOT
examples  ly-simple-tomcat-0.0.1-SNAPSHOT  manager                              test

web-2主机IP网页测试

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐