java程序发布步骤
1.拉取代码并打包
2.备份原程序
3.拷贝新程序到服务器的webapps目录中,停一会修改配置
4.重启tomcat
5.验证
Jenkins发布java项目到另一台主机的tomcat中
环境介绍:
名称 | IP | 服务 |
---|---|---|
jenkins | 192.168.159.110 | jenkins |
server | 192.168.159.120 | tomcat |
手动版
//server上部署一个tomcat
//拉取代码
[root@jenkins ~]# yum -y install git
[root@jenkins ~]# cd /opt/
[root@jenkins 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 | 8.00 MiB/s, done.
处理 delta 中: 100% (217/217), done.
[root@jenkins opt]# ls
tomcat-java-demo
//下载打包工具maven
[root@jenkins tomcat-java-demo]# yum -y install maven
[root@jenkins tomcat-java-demo]# mvn clean package
//target里面是打包好的
[root@jenkins tomcat-java-demo]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src target
[root@jenkins tomcat-java-demo]# ls target/
classes generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-archiver 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
//因为要把包传到server端,所以要做免密登录,在Jenkins生成公钥传给server
[root@jenkins 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:lBjSVqDTgQtSkiDQig3xd00/WGmQM9a2ZD/ERAkN30I root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
|O=. .o+o+++OE. |
|=oo .+o*==Bo=. |
|.=..+o+.=Bo+o . |
|o ...o . ..o. |
| S . |
| |
| |
| |
| |
+----[SHA256]-----+
[root@jenkins tomcat-java-demo]# ssh-copy-id root@192.168.159.120
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.159.120 (192.168.159.120)' can't be established.
ECDSA key fingerprint is SHA256:YvjlcWC1AXqLdQB7WpD6jYcN/HzeRzdaOqE0J3sVfTk.
ECDSA key fingerprint is MD5:35:e2:cf:b9:3b:6e:37:de:04:4c:aa:c1:2f:6c:2d:bf.
Are you sure you want to continue connecting (yes/no)? 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.159.120's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.159.120'"
and check to make sure that only the key(s) you wanted were added.
[root@jenkins tomcat-java-demo]# scp target/myapp.war root@192.168.159.120:/usr/local/tomcat/webapps
myapp.war 100% 17MB 75.2MB/s 00:00
//查看
[root@server ~]# cd /usr/local/tomcat/
[root@server tomcat]# pwd
/usr/local/tomcat
[root@server tomcat]# ls webapps/
docs examples host-manager manager myapp myapp.war ROOT test
手动部署完成
自动版
server上创建一个项目
评论区