Jenkins流水线部署java项目

Jenkins发布java项目

  • java程序发布步骤
  • 手动部署
  • 自动部署

java程序发布步骤

  • 拉取代码并打包
  • 备份源程序
  • 拷贝新程序到服务器的webapps目录中,停一会修改配置
  • 重启tomcat
  • 验证

手动部署

部署环境

主机名 ip地址 服务
jenkins 192.168.129.133 tomcat
jenkins
server 192.168.129.134 tomcat

jenkins主机部署Tomcat、Jenkins

server主机部署tomcat

关闭防火墙与selinux

[root@jenkins ~]# systemctl disable --now firewalld.service 
[root@jenkins ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 
[root@jenkins ~]# setenforce 0

[root@server ~]# systemctl disable --now firewalld.service 
[root@server ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 
[root@server ~]# setenforce 0

拉取代码

[root@jenkins ~]# cd /opt/
[root@jenkins opt]# yum -y install git		#安装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 | 3.49 MiB/s, 完成.
处理 delta 中: 100% (217/217), 完成.
[root@jenkins opt]# ls
tomcat-java-demo

打包文件

[root@jenkins opt]# cd tomcat-java-demo/
[root@jenkins tomcat-java-demo]# yum -y install maven		#安装maven 工具
[root@jenkins tomcat-java-demo]# mvn clean package 
......
[INFO] Packaging webapp
[INFO] Assembling webapp [ly-simple-tomcat] in [/opt/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [185 msecs]
[INFO] Building war: /opt/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:56 min
[INFO] Finished at: 2021-10-19T18:38:05+08:00
[INFO] ------------------------------------------------------------------------

[root@jenkins tomcat-java-demo]# ls target/
classes  generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT  ly-simple-tomcat-0.0.1-SNAPSHOT.war  maven-archiver  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  generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT  maven-archiver  maven-status  myapp.war

设置免密登录

[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:xtKS6e4tqVK8B849+eoUbbTJAJp38COb253ccwIw+FY root@jenkins
The key's randomart image is:
+---[RSA 3072]----+
|    o            |
|   o =           |
|  o + B E        |
|   . * # o       |
|   .o O S        |
|    += O +       |
|   +.++o+ + .    |
|  . +oB.   +     |
|   ..===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"
The authenticity of host '192.168.129.134 (192.168.129.134)' can't be established.
ECDSA key fingerprint is SHA256:uHo8Vg0cYwJ7KOQnCtvlu7g43XP3sjNQjDZoeCO6lBY.
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.129.134'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.

将包输送到server主机

[root@jenkins tomcat-java-demo]# pwd
/opt/tomcat-java-demo
[root@jenkins tomcat-java-demo]# scp target/myapp.war [email protected]:/usr/local/tomcat/webapps/
myapp.war                                                                            100%   17MB 125.8MB/s   00:00

测试
Jenkins流水线部署java项目_第1张图片

自动部署

Jenkins流水线部署java项目_第2张图片
Jenkins流水线部署java项目_第3张图片
Jenkins流水线部署java项目_第4张图片
Jenkins流水线部署java项目_第5张图片
Jenkins流水线部署java项目_第6张图片
Jenkins流水线部署java项目_第7张图片
查看打的包

[root@jenkins tomcat-java-demo]#  cd /root/.jenkins/workspace/
[root@jenkins workspace]# ls
demo  demo@tmp

在server主机上创建备份

[root@server webapps]# mkdir /opt/backup
[root@server ~]# tar -Jcf /opt/backup/myapp-$(date +%Y%m%d%H%M).tar.xz /usr/local/tomcat/webapps/myapp
tar: 从成员名中删除开头的“/[root@server ~]# ls /opt/backup/
myapp-202110191923.tar.xz

配置流线脚本

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 [email protected] 'tar -Jcf /opt/backup/myapp-\$(date +%Y%m%d%H%M).tar.xz /usr/local/tomcat/webapps/myapp'
                    scp target/myapp.war [email protected]:/usr/local/tomcat/webapps/
                    ssh [email protected] '/usr/local/tomcat/bin/catalina.sh stop'
                    sleep 3
                    ssh [email protected] '/usr/local/tomcat/bin/catalina.sh start'
                '''
            }
        }
    }
}

Jenkins流水线部署java项目_第8张图片
Jenkins流水线部署java项目_第9张图片
Jenkins流水线部署java项目_第10张图片
Jenkins流水线部署java项目_第11张图片

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