环境
主机名 | ip地址 | 服务 |
---|---|---|
jenkins | 192.168.216.200 | tomcat、jenkins |
server | 192.168.216.215 | tomcat |
//关闭防火墙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 key's 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.215's 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端
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'" # 重启生成项目 } } } }
去目标主机验证
手动部署
//关闭防火墙,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会自动解压,过一会进入网站对应目录就能看到新部署的项目
到此这篇关于使用jenkins将项目部署到另一台主机的文章就介绍到这了,更多相关jenkins项目部署另一台主机内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_46115601/article/details/120862062