Jenkins发布java项目

Jenkins发布java项目

手动部署

拉取代码
[root@localhost opt]# git clone https://gitee.com/forgotten/tomcat-java-demo.git
正克隆到 'tomcat-java-demo'...
remote: Enumerating objects: 558, done.
remote: Counting objects: 100% (558/558), done.
remote: Compressing objects: 100% (316/316), done.
remote: Total 558 (delta 217), reused 558 (delta 217), pack-reused 0
接收对象中: 100% (558/558), 5.08 MiB | 278.00 KiB/s, 完成.
处理 delta 中: 100% (217/217), 完成.
[root@localhost opt]# ls
tomcat-java-demo

打包文件
[root@localhost tomcat-java-demo]# yum -y install maven ##安装打包软件

[root@localhost tomcat-java-demo]# mvn clean package 
······
[INFO] Processing war project
[INFO] Webapp assembled in [369 msecs]
[INFO] Building war: /opt/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11:40 min
[INFO] Finished at: 2021-10-19T03:07:09-04:00
[INFO] ------------------------------------------------------------------------

[root@localhost 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@localhost target]# mv ly-simple-tomcat-0.0.1-SNAPSHOT.war myapp.war
[root@localhost target]# ls
classes            ly-simple-tomcat-0.0.1-SNAPSHOT  maven-status
generated-sources  maven-archiver                   myapp.war

将包文件传送过去
[root@localhost target]# scp myapp.war root@192.168.240.50:/usr/local/tomcat/webapps
The authenticity of host '192.168.240.50 (192.168.240.50)' can't be established.
ECDSA key fingerprint is SHA256:lJaDvQ9JrWLSAZbw6RRce5aPLnj7Mhatv99i5G9NbIw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.240.50' (ECDSA) to the list of known hosts.
root@192.168.240.50's password: 
myapp.war                                                100%   17MB  30.4MB/s   00:00 
浏览器访问测试

在这里插入图片描述

自动部署

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

查看打的包
[root@localhost target]# cd /root/.jenkins/workspace/
[root@localhost workspace]# ls
lqlq  lqlq@tmp
[root@localhost workspace]# ls lqlq
db  deploy.yaml  Dockerfile  jenkinsfile  LICENSE  pom.xml  README.md  src  target
[root@localhost workspace]# ls lqlq/target/
classes            ly-simple-tomcat-0.0.1-SNAPSHOT      maven-archiver
generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT.war  maven-status

在tomcat主机上创建备份
[root@localhost webapps]# mkdir /opt/backup
配置流水线脚本
pipeline {
    agent any
    
    stages {
        stage('Build') {
            steps {
                // Get some code from a GitHub repository
                git 'https://gitee.com/best2001/tomcat-java-demo.git'  

                // Run Maven on a Unix agent.
                sh "mvn -Dmaven.test.failure.ignore=true clean package"  
                sh "mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war  target/myapp.war"   
                // To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
                
            } 
        }        
        stage("publish"){
            steps{
                sh "ssh root@192.168.240.50  'tar -Jcf /opt/backup/myapp-`date +%F`.tar.xz    /usr/local/tomcat/webapps/myapp'"  
                sh "scp target/myapp.war root@192.168.240.50:/usr/local/tomcat/webapps/" 
                sh "ssh root@192.168.240.50 '/usr/local/tomcat/bin/catalina.sh stop;sleep 3;/usr/local/tomcat/bin/catalina.sh start'"  
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述

使用IP访问测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木逢秋࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值