tomcat上部署jenkins

tomcat上部署jenkins

主机名称 IP地址 需要的应用服务 工具包
node1 192.168.111.141 tomcat、jenkins git、maven
node2 192.168.111.142 tomcat

部署tomcat

//安装jdk
[root@node1 ~]# yum -y install java-17-openjdk

//下载9.0版本,10的版本部署有bug,无法实现
[root@node1 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.67/bin/apache-tomcat-9.0.67.tar.gz


//解压tomcat,并部署安装
[root@node1 ~]# tar -xzf apache-tomcat-9.0.67.tar.gz -C /usr/local/
[root@node1 ~]# cd /usr/local/
[root@node1 local]# ln -s apache-tomcat-9.0.67/ tomcat
[root@node1 local]# ls
apache-tomcat-9.0.67  bin  etc  games  include  lib  lib64  libexec  sbin  share  src  tomcat


//启动tomcat,测试能否访问tomcat首页
[root@node1 ~]# /usr/local/tomcat/bin/startup.sh 
[root@node1 ~]# ss -anlt | grep 8080
LISTEN 0      100                     *:8080            *:*         

访问首页

image

部署jenkins

//下载jenkins到/usr/local/tomcat/webapps/目录下
[root@node1 ~]# cd /usr/local/tomcat/webapps/


//下载下来后会自动解压
[root@node1 webapps]# wget http://mirrors.jenkins.io/war-stable/2.346.3/jenkins.war
[root@node1 webapps]# ls
ROOT  docs  examples  host-manager  jenkins  jenkins.war  manager

//启动服务
[root@node1 webapps]# cd ..
[root@node1 tomcat]# bin/catalina.sh stop
[root@node1 tomcat]# bin/catalina.sh start

浏览器访问jenkins

image

//查看/root/.jenkins/secrets/initialAdminPassword
[root@node1 tomcat]# cat /root/.jenkins/secrets/initialAdminPassword
1af83134887b469fa049de1c9cbb84a7

将查看到的密码复制
image

Jenkins创建流水线任务

在第一台上部署服务tomcat和jenkins(上面已经部署),登录web界面时选择安装推荐的插件,部署好后,设置国内源
image
等待下载
image
image
更改国内源
image
image

在node1中配置jenkins
image
创建流水线任务
image
首选都需要在jinkins所在服务器,也就是node1中安装git和maven

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

image
添加git下载信息 http://github.com/lizhenliang/tomcat-java-demo.git
image
添加文本参数客户端IP
image
添加字符参数定义tomcat路径,ssh密码
image
添加字符参数,打包
image
添加字符参数,定义tomcat路径
image
写流水脚本
image

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                git '${git_url}'
            }
        }
        stage('package') { 
            steps { 
                sh """ 
	            mvn clean
	            mvn package
	            """
	        }
        }
        stage('deploy') { 
	        steps { 
	            sh """
	            sshpass -p "${ssh_pass}" ssh root@${client_ip} 'yum -y install java-11-openjdk > /dev/null'
	            sshpass -p "${ssh_pass}" ssh root@${client_ip} 'wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.67/bin/apache-tomcat-9.0.67.tar.gz -P /tmp'
	            sshpass -p "${ssh_pass}" ssh root@${client_ip} 'tar xf /tmp/apache-tomcat-9.0.67.tar.gz -C /usr/local'
	            sshpass -p "${ssh_pass}" ssh root@${client_ip} 'ln -s /usr/local/apache-tomcat-9.0.67 ${tomcat_path}'
	            sshpass -p "${ssh_pass}" ssh root@${client_ip} '${tomcat_path}/bin/catalina.sh start'
	            sshpass -p "${ssh_pass}" scp target/${maven_package}.war root@${client_ip}:${tomcat_path}/webapps/
                    sshpass -p "${ssh_pass}" ssh root@${client_ip} '${tomcat_path}/bin/catalina.sh stop && ${tomcat_path}/bin/catalina.sh start'
                
                """
            }
        }
    }
}

image

posted @ 2022-09-29 21:47  事愿人为  阅读(722)  评论(0编辑  收藏  举报