- tomcat使用jenkins部署项目到另一台主机
- 1.部署环境
- 2.安装打包的命令
- 3.配置免密
- 4.在jenkins上创建项目
- 5.修改shell脚本
| 主机 | IP地址 | 服务 |
|---|---|---|
| jenkins | 192.168.100.146 | tomcat、jenkins |
| test | 192.168.100.147 | tomcat |
[root@jenkins ~]# yum -y install maven git 上次元数据过期检查:1 day, 22:33:38 前,执行于 2021年10月17日 星期日 06时20分17秒。 依赖关系解决。3.配置免密
[root@jenkins ~]# 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:K2YbaWd+PXts2rSfaoNYwQTIaHQl2LLiEHoWVEhoBj8 root@jenkins The key's randomart image is: +---[RSA 3072]----+ |o+ooo.=ooo. | |.=o =.+. . | |+ E.. o o | |..oo . o | | oo . S . | | . . . . | | B + o.... | | + B ...o=+..| | . .. +B=o.| +----[SHA256]-----+ [root@jenkins ~]# ssh-copy-id root@192.168.100.147 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.100.147 (192.168.100.147)' can't be established. ECDSA key fingerprint is SHA256:r5uhMkBRTNxvYYvWxormTvBxafc0DHlna23cHgnhfl4. 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.100.147's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.100.147'" and check to make sure that only the key(s) you wanted were added. [root@jenkins ~]#4.在jenkins上创建项目
pipeline {
agent any
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git 'https://gitee.com/forgotten/tomcat-java-demo.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.100.147 'tar -Jcf /opt/backup/myapp-`date +%F`.tar.xz /usr/local/tomcat/webapps/myapp'" //备份原有的项目
sh "scp target/myapp.war root@192.168.153.156:/usr/local/tomcat/webapps/" //传包
sh "ssh root@192.168.100.147 '/usr/local/tomcat/bin/catalina.sh stop;sleep 3;/usr/local/tomcat/bin/catalina.sh start'" // 重启生成项目
}
}
}
}



