环境
主机名 | 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项目部署另一台主机内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!