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'"
}
}
}
}