jenkins发布java项目

jenkins发布java项目

java 程序发布步骤:
1、拉取代码并打包
2、备份源程序
3、拷贝新程序到服务器的webapps目录中,停顿会,修改配置
4、重启tomcat
5、验证

首先,手动发布验证,再web界面部署

手动部署

[root@jenkins ~]# yum -y install git maven

拉取代码
[root@jenkins ~]# cd /opt/
[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 | 1.32 MiB/s, 完成.
处理 delta 中: 100% (217/217), 完成.
[root@jenkins opt]# ls
tomcat-java-demo

打包
[root@jenkins tomcat-java-demo]# ls
db           Dockerfile   LICENSE  README.md  target
deploy.yaml  jenkinsfile  pom.xml  src
[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/myyzy.war
[root@jenkins tomcat-java-demo]# ll target/
总用量 17840
drwxr-xr-x 5 root root       95 10月 20 12:17 classes
drwxr-xr-x 3 root root       25 10月 20 12:17 generated-sources
drwxr-xr-x 4 root root       37 10月 20 12:17 ly-simple-tomcat-0.0.1-SNAPSHOT
drwxr-xr-x 2 root root       28 10月 20 12:17 maven-archiver
drwxr-xr-x 3 root root       35 10月 20 12:17 maven-status
-rw-r--r-- 1 root root 18265402 10月 20 12:17 myyzy.war
在jenkins和server端设置ssh免密登录
[root@jenkins ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:n5Xg/Ju8atXC+9qdZtdRirfKTbYgrMNtr1tgjyOfAVk root@jenkins
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|                 |
|          E      |
|         = . .  .|
|        S =.oo ..|
|         = B= +. |
|       ...O.=+o.o|
|        o++O.O.=+|
|        .+*=X=B.o|
+----[SHA256]-----+
[root@jenkins ~]# 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@jenkins target]# scp myyzy.war [email protected]:/usr/local/tomcat/webapps/
myyzy.war                              100%   17MB 101.4MB/s   00:00    
[root@server webapps]# ls
myyzy  myyzy.war  ROOT

jenkins发布java项目_第1张图片

通过jenkins自动发布

在接受端创建一个文件来存放备份文件

[root@server ~]# mkdir /opt/backup

]

通过jenkins自动发布

在接受端创建一个文件来存放备份文件

[root@server ~]# mkdir /opt/backup

jenkins发布java项目_第2张图片
jenkins发布java项目_第3张图片
箭头指向是包的下载地址 然后保存
jenkins发布java项目_第4张图片
jenkins发布java项目_第5张图片
jenkins发布java项目_第6张图片
jenkins发布java项目_第7张图片
jenkins发布java项目_第8张图片
jenkins发布java项目_第9张图片
通过jenkins部署成功

我们可以通过片段生成器来生成我们需要的流水线语法
jenkins发布java项目_第10张图片

脚本

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"
                sh "mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/myyzy.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/myyzy-\$(date +%Y%m%d).tar.xz /usr/local/tomcat/webapps/myyzy"
                sh "scp target/myyzy.war [email protected]:/usr/local/tomcat/webapps/"
                sh "ssh [email protected] '/usr/localtomcat/bin/catalina.sh stop;sleep 3;/usr/local/tomcat/bin/catalina.sh start'"
            }
        }
        
        
    }
}

你可能感兴趣的:(java,jenkins,tomcat)