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

环境

 

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

使用jenkins将项目部署到另一台主机的过程_第1张图片
使用jenkins将项目部署到另一台主机的过程_第2张图片

//关闭防火墙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 [email protected]
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 [email protected]
/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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost ~]# ssh [email protected]
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将项目部署到另一台主机的过程_第3张图片
使用jenkins将项目部署到另一台主机的过程_第4张图片
使用jenkins将项目部署到另一台主机的过程_第5张图片

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 [email protected]  'tar -Jcf /opt/backup/webapps-\$(date +%Y-%m%d).tar.xz   /usr/local/tomcat/webapps/*'"   #备份原有的项目
                sh "scp target/myapp.war [email protected]:/usr/local/tomcat/webapps/"  #传包到目标主机
                sh "ssh [email protected] '/usr/local/tomcat/bin/catalina.sh stop;sleep 3;/usr/local/tomcat/bin/catalina.sh start'"   #  重启生成项目
            }
        }
    }
}

使用jenkins将项目部署到另一台主机的过程_第6张图片
使用jenkins将项目部署到另一台主机的过程_第7张图片

去目标主机验证

使用jenkins将项目部署到另一台主机的过程_第8张图片

手动部署

//关闭防火墙,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 [email protected]:/usr/local/tomcat/webapps/
//tomcat会自动解压,过一会进入网站对应目录就能看到新部署的项目

到此这篇关于使用jenkins将项目部署到另一台主机的文章就介绍到这了,更多相关jenkins项目部署另一台主机内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(使用jenkins将项目部署到另一台主机的过程)