使用jenkins将项目部署到另一台主机的过程

本站所有内容来自互联网收集,仅供学习和交流,请勿用于商业用途。如有侵权、不妥之处,请第一时间联系我们删除!Q群:迪思分享

免费资源网 – https://freexyz.cn/

环境

主机名 ip地址 服务 jenkins 192.168.216.200 tomcat、jenkins server 192.168.216.215 tomcat

使用jenkins将项目部署到另一台主机的过程插图 使用jenkins将项目部署到另一台主机的过程插图1

//关闭防火墙selinux //安装打包命令,git [root@jenkins ~]# yum -y install maven [root@jenkins ~]# yum -y install git //配置免密登录 [root@localhost ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): 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:npcyZyHpOPmqGL1nbpZRj7UYULZxJhvNIcDg2G9Qvso root@localhost.localdomain The keys randomart image is: +—[RSA 3072]—-+ | .oooBo+. | | + oo. Xo | | . + ..o | | o .o.. | | +.S*.. | | o o.=oooo | | . E +o* = | | o .*o * | | . oBo.. | +—-[SHA256]—–+ [root@localhost ~]# ssh-copy-id root@192.168.216.215 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub” /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.216.215s password: Number of key(s) added: 1 Now try logging into the machine, with: “ssh root@192.168.216.215” and check to make sure that only the key(s) you wanted were added. [root@localhost ~]# ssh root@192.168.216.215 Last login: Wed Oct 20 11:03:43 2021 from 192.168.216.200 [root@localhost ~]# exit 登出 Connection to 192.168.216.215 closed.

进入jenkins端

使用jenkins将项目部署到另一台主机的过程插图2 使用jenkins将项目部署到另一台主机的过程插图3 使用jenkins将项目部署到另一台主机的过程插图4

pipeline { agent any stages { stage(Build) { steps { // Get some code from a GitHub repository git https://gitee.com/forgotten/tomcat-java-demo.git #下载包,确保主机上有git命令 // Run Maven on a Unix agent. sh “mvn -Dmaven.test.failure.ignore=true clean package” #打包成war包 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.216.215 tar -Jcf /opt/backup/webapps-$(date +%Y-%m%d).tar.xz /usr/local/tomcat/webapps/*” #备份原有的项目 sh “scp target/myapp.war root@192.168.216.215:/usr/local/tomcat/webapps/” #传包到目标主机 sh “ssh root@192.168.216.215 /usr/local/tomcat/bin/catalina.sh stop;sleep 3;/usr/local/tomcat/bin/catalina.sh start” # 重启生成项目 } } } }

使用jenkins将项目部署到另一台主机的过程插图5 使用jenkins将项目部署到另一台主机的过程插图6

去目标主机验证

使用jenkins将项目部署到另一台主机的过程插图7

手动部署

//关闭防火墙,selinux //确保git命令存在,拉取部署的项目包 [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 | 673.00 KiB/s, 完成. 处理 delta 中: 100% (217/217), 完成. [root@jenkins opt]# ls tomcat-java-demo // 打包,确保mvn命令存在 [root@jenkins opt]# cd tomcat-java-demo/ [root@jenkins tomcat-java-demo]# ls db Dockerfile LICENSE README.md deploy.yaml jenkinsfile pom.xml src // 只要是带pom.xml这样文件的都是用mvn打包 [root@jenkins tomcat-java-demo]# mvn clean package [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 ly-simple-tomcat-0.0.1-SNAPSHOT maven-status generated-sources maven-archiver myapp.war // 确保有免密登录,通过ssh启动server端tomcat,然后把要部署的包传过去 [root@jenkins tomcat-java-demo]# scp target/myapp.war root@192.168.47.163:/usr/local/tomcat/webapps/ //tomcat会自动解压,过一会进入网站对应目录就能看到新部署的项目
免费资源网 – https://freexyz.cn/


© 版权声明
THE END
★喜欢这篇文章吗?喜欢的话,麻烦动动手指支持一下!★
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容