Jenkins发布java项目
Jenkins发布java项目到另一台主机的tomcat
主机 | 安装 | |
---|---|---|
CentOS8 192.168.220.9(client) | tomcat jenkins | 发送端 |
CentOS8 192.168.220.10(server) | tomcat | 接受端 |
安装tomcat和Jenkins的步骤略,前面的文章有写到,现在直接开始操作
关闭两台防火墙,查看端口,访问页面是否正常
[root@client ~]# systemctl disable --now firewalld
[root@client ~]# setenforce 0
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@server ~]# systemctl disable --now firewalld
[root@server ~]# setenforce 0
[root@server ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
发送端的tomcat访问正常
发送端的Jenkins访问正常
接受端的tomcat访问正常
/
首先,手动发布项目
发送端将代码拉下来并打包成.war 包
yum -y install git maven
[root@client ~]# git clone https://gitee.com/forgotten/tomcat-java-demo.git(拉取代码)
Cloning into '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
Receiving objects: 100% (558/558), 5.08 MiB | 600.00 KiB/s, done.
Resolving deltas: 100% (217/217), done.
[root@client ~]# ls
anaconda-ks.cfg tomcat-java-demo(拉下的来项目)
[root@client ~]# ls tomcat-java-demo/
Dockerfile LICENSE README.md db deploy.yaml jenkinsfile pom.xml src
# 打包
[root@client tomcat-java-demo]# mvn clean package
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.0.1.RELEASE/spring-boot-starter-parent-2.0.1.RELEASE.pom
..............
[INFO] Building war: /root/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:48 min
[INFO] Finished at: 2021-10-19T02:53:11-04:00
[INFO] ------------------------------------------------------------------------
[root@client tomcat-java-demo]# ls
Dockerfile LICENSE README.md db deploy.yaml jenkinsfile pom.xml src target(打包成功后的目录)
[root@client 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
配置免密登录,将打包好的.war包传到接收端发布
# 先给war包改名,太长
[root@client 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@client tomcat-java-demo]# mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/myapp.war
[root@client tomcat-java-demo]# ls target/
classes generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT maven-archiver maven-status myapp.war(改名成功)
# 配置免密登录并传输到接受端
[root@client tomcat-java-demo]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Zzx0MlNtUum3UPWFy/m0jzUATkPEccqCMpAf5InPC/w root@client
The key's randomart image is:
+---[RSA 3072]----+
| .o. ++o+.o+|
| .+.. . .*+ =.o|
| ..=.. .Bo+=.o.|
| . o.o o.* o= o|
| o o S = o+o|
| o . o . +o|
| E oo|
| . .|
| |
+----[SHA256]-----+
[root@client tomcat-java-demo]# ssh-copy-id root@192.168.220.10(接受端IP)
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.220.10 (192.168.220.10)' can't be established.
ECDSA key fingerprint is SHA256:ZeQy7MqiTiEdnowRrum5zEWROx4LRrsYwdL8dvgx6N0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.220.10's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.220.10'"
and check to make sure that only the key(s) you wanted were added.
# 传输到接收端
[root@client tomcat-java-demo]# scp target/myapp.war 192.168.220.10(接收端IP):/usr/local/tomcat/webapps/
myapp.war 100% 17MB 183.7MB/s 00:00
接收端查看
# 传输成功并已经自动解压
[root@server webapps]# ls
ROOT docs examples host-manager jenkins jenkins.war manager myapp(自动解压的) myapp.war(war包)
接受端访问查看
/
通过Jenkins自动发布
接收端创建一个文件夹来存放备份文件(如果有文件)
[root@server ~]# mkdir /opt/backup
[root@server ~]# ls /opt/backup/
[root@server ~]#
发送端进入Jenkins web界面进行操作(创建项目)
写入脚本自动执行
脚本内容
pipeline {
agent any
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git 'https://gitee.com/jinchenghe92/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.220.10 tar -Jcf /opt/backup/myapp-\$(date +%Y%m%d).tar.xz /usr/local/tomcat/webapps/myapp"
sh "scp target/myapp.war root@192.168.220.10:/usr/local/tomcat/webapps/"
sh "ssh root@192.168.220.10 '/usr/localtomcat/bin/catalina.sh stop;sleep 3;/usr/local/tomcat/bin/catalina.sh start'"
}
}
}
}
查看接收端是否收到 myapp.war 包
[root@server ~]# cd /usr/local/tomcat/webapps/
[root@server webapps]# ls
ROOT docs examples host-manager jenkins jenkins.war manager myapp(已自动解压) myapp.war(收到)
接收端 web界面访问