Jenkins发布java项目

Jenkins发布java项目,第1张

Jenkins发布java项目 Jenkins发布java项目 手动部署 拉取代码
[root@jenkins ~]# cd /opt/
[root@jenkins opt]# git clone https://gitee.com/forgotten/tomcat-java-demo.git
Cloning into '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
Receiving objects: 100% (558/558), 5.08 MiB | 715.00 KiB/s, done.
Resolving deltas: 100% (217/217), done.
[root@jenkins opt]# ls
tomcat-java-demo
打包文件
[root@jenkins opt]# yum -y install maven    ##安装打包软件

[root@jenkins opt]# cd tomcat-java-demo/
[root@jenkins tomcat-java-demo]# mvn clean package 
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.ly.simple:ly-simple-tomcat >-------------------
[INFO] Building ly-simple-tomcat 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ ly-simple-tomcat ---
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ ly-simple-tomcat ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 136 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ ly-simple-tomcat ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to /opt/tomcat-java-demo/target/classes
..........................

[root@jenkins tomcat-java-demo]# ls target/
classes            ly-simple-tomcat-0.0.1-SNAPSHOT      maven-archiver
generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT.war  maven-status

##原名称太长,更改名称
[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
将包文件传送过去
[root@jenkins tomcat-java-demo]# scp target/myapp.war root@192.168.10.203:/usr/local/tomcat/webapps
The authenticity of host '192.168.10.203 (192.168.10.203)' can't be established.
ECDSA key fingerprint is SHA256:hdIBu8OYrcwUpqYdddILiPC9GEG8hj6dNhQncxKB1C4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.10.203' (ECDSA) to the list of known hosts.
root@192.168.10.203's password: 
myapp.war                                                         100%   17MB  92.7MB/s   00:00    
[root@jenkins tomcat-java-demo]#
重启tomcat并查看效果
[root@server ~]# cd /usr/local/tomcat/webapps/
[root@server webapps]# ls
docs  examples  host-manager  manager  myapp.war  ROOT
[root@server webapps]# ../bin/catalina.sh start
Using CATALINA_base:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

自动部署 做免密登陆
[root@jenkins ~]# ssh-keygen -t rsa
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:FKbE73Qy84p81/cyGzd+shgQOVrTgVQ5mSXpk33f4Fs root@jenkins
The key's randomart image is:
+---[RSA 3072]----+
|     .. o..o+*.  |
|     ..o ..o*o   |
|      ... *..+   |
|       .*o.++ o .|
|       oS*.  o oo|
|        . ..  . E|
|     . . . .. .+.|
|      o o . .oBoo|
|       . .  ..oOo|
+----[SHA256]-----+
[root@jenkins ~]# ssh-copy-id root@192.168.10.203
/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.10.203's password: 

Number of key(s) added: 1

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

[root@jenkins ~]#
进入Jenkins界面

编写脚本内容

应用并保存后开始构建

访问IP:8080/myapp

流水线脚本
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  
                    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.10.203  'tar -Jcf /opt/backup/myapp-`date +%F`.tar.xz /usr/local/tomcat/webapps/myapp'"  
                sh "scp target/myapp.war root@192.168.10.203:/usr/local/tomcat/webapps/" 
                sh '''
                    ssh root@192.168.10.203 '/usr/local/tomcat/bin/catalina.sh stop'
                    sleep 3
                    ssh root@192.168.10.203 '/usr/local/tomcat/bin/catalina.sh start'
                '''
            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4667730.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存