Tomcat

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Tomcat > tomcat部署jenkins

tomcat部署jenkins项目的实现示例

作者:运维工程师001号

Jenkins自动化部署可以解决集成、测试、部署等重复性的工作,本文主要介绍了tomcat部署jenkins项目,具有一定的参考价值,感兴趣的可以了解一下

jenkins简介

​ Jenkins自动化部署可以解决集成、测试、部署等重复性的工作,工具集成的效率明显高于人工操作;并且持续集成可以更早的获取代码变更的信息,从而更早的进入测试阶段,更早的发现问题,这样解决问题的成本就会显著下降:持续集成缩短了从开发、集成、测试、部署各个环节的时间,从而也就缩短了中间出现的等待时间;持续集成也意味着开发、集成、测试、部署得以持续。

Jenkins的主要特点和功能:

环境准备:

部署jenkins前先部署tomcat,tomcat部署方法请参考:tomcat

jenkins部署

下载jenkins软件包

下载地址:Jenkins download and deployment

[root@localhost ~]# wget https://get.jenkins.io/war-stable/2.414.2/jenkins.war
...
Length: 89532729 (85M) [application/java-archive]
Saving to: ‘jenkins.war'
jenkins.war                       100%[===========================================================>]  85.38M   299KB/s    in 3m 15s  
2023-10-11 14:07:07 (448 KB/s) - ‘jenkins.war' saved [89532729/89532729]
[root@localhost ~]# 
[root@localhost ~]# ls
anaconda-ks.cfg  apache-tomcat-9.0.65.tar.gz  jenkins.war
[root@localhost ~]# 

将jenkins.war文件拷贝至webapps目录下

[root@localhost ~]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# 
[root@localhost webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@localhost webapps]# 
[root@localhost webapps]# cp /root/jenkins.war .
[root@localhost webapps]# ls
docs  examples  host-manager  jenkins.war  manager  ROOT
[root@localhost webapps]# 

编辑server.xml配置文件

开启AJP协议,用于tomcat跟apache的效率,注意如果有多个tomcat,则改一下端口号,比如8009变8010

[root@localhost ~]# cd /usr/local/tomcat/conf/
[root@localhost conf]# ls
Catalina         catalina.properties  jaspic-providers.xml  logging.properties  tomcat-users.xml  web.xml
catalina.policy  context.xml          jaspic-providers.xsd  server.xml          tomcat-users.xsd
[root@localhost conf]# 
[root@localhost conf]# vim server.xml 
...
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443" />
...
"server.xml" 168L, 7563C written                                                                          
[root@localhost conf]#

重启服务,jenkins.war包会自动解压缩

[root@localhost webapps]# cd ..
[root@localhost tomcat]# ./bin/catalina.sh stop
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:   
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@localhost tomcat]# 
[root@localhost tomcat]# 
[root@localhost tomcat]# ./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@localhost tomcat]# 
[root@localhost tomcat]# ss -anlt
State          Recv-Q         Send-Q                      Local Address:Port                 Peer Address:Port        Process         
LISTEN         0              128                               0.0.0.0:22                        0.0.0.0:*                           
LISTEN         0              1                      [::ffff:127.0.0.1]:8005                            *:*                           
LISTEN         0              100                                 [::1]:8009                         [::]:*                           
LISTEN         0              100                                     *:8080                            *:*                           
LISTEN         0              128                                  [::]:22                           [::]:*                           
[root@localhost tomcat]#
[root@localhost tomcat]# cd webapps/
[root@localhost webapps]# ls
docs      host-manager     jenkins.war    ROOT    examples  jenkins      manager
[root@localhost webapps]#  

访问网页解锁 Jenkins

[root@localhost webapps]# cat /root/.jenkins/secrets/initialAdminPassword
d4871675ae814d8ea501b87f853b94f6
[root@localhost webapps]#

image-20231011144848802

选择安装推荐的插件

image-20231011145208200

等待安装完成

image-20231011145808906

创建管理员用户

用户名,密码和邮箱自定义

登录成功

image-20231011150855659

使用jenkins部署项目

安装拉取项目和打包项目的工具

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

手动拉取项目测试

[root@localhost ~]# git clone https://gitee.com/forgotten/tomcat-java-demo.git
Cloning into 'tomcat-java-demo'...
remote: Enumerating objects: 558, done.
remote: Total 558 (delta 0), reused 0 (delta 0), pack-reused 558
Receiving objects: 100% (558/558), 5.08 MiB | 536.00 KiB/s, done.
Resolving deltas: 100% (217/217), done.
[root@localhost ~]# ls
anaconda-ks.cfg  apache-tomcat-9.0.65.tar.gz  jenkins.war  tomcat-java-demo
[root@localhost ~]# 

将java版本更换为17

[root@localhost ~]# alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/bin/java)
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/jre/bin/java)

Enter to keep the current selection[+], or type selection number: 1
[root@localhost ~]# 

手动打包测试

[root@localhost tomcat-java-demo]# mvn clean package
...
[INFO] Packaging webapp
[INFO] Assembling webapp [ly-simple-tomcat] in [/root/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT]
...
[root@localhost tomcat-java-demo]# 
[root@localhost tomcat-java-demo]# cd target/
[root@localhost target]# ls
classes  generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT  ly-simple-tomcat-0.0.1-SNAPSHOT.war  maven-archiver  maven-status
[root@localhost target]# 

创建一个流水线任务

编写一个hello world 流水线脚本

点击test,然后点击立即构建

构建成功

image-20231011162214404

生成拉取脚本,编写流水线脚本

仓库URL下面填写你要拉取的项目地址

生成拉取脚本后,编写流水线脚本

点击立即构建

拉取成功

image-20231011164348598

查看流水线脚本拉取的项目

[root@localhost ~]# ls -a       
.   anaconda-ks.cfg              .bash_history  .bash_profile  .cshrc   .java     jenkins.war  .tcshrc   .wget-hsts
..  apache-tomcat-9.0.65.tar.gz  .bash_logout   .bashrc        .groovy  .jenkins  .m2          .viminfo
[root@localhost ~]# 
[root@localhost ~]# cd .jenkins/
[root@localhost .jenkins]# 
[root@localhost .jenkins]# ls
...
jenkins.install.UpgradeWizard.state             plugins                                                    users
jenkins.model.JenkinsLocationConfiguration.xml  queue.xml                                                  workspace
jenkins.telemetry.Correlator.xml                queue.xml.bak
[root@localhost .jenkins]# 
[root@localhost .jenkins]# cd workspace/test/
[root@localhost test]# ls
db  deploy.yaml  Dockerfile  jenkinsfile  LICENSE  pom.xml  README.md  src
[root@localhost test]# 

安装maven打包插件

选择需要安装的插件

点击安装

安装完成

image-20231011165658072

手动打包成功后编写流水线脚本

流水线脚本代码

pipeline {
    agent any
    stages {
        stage('pull code and package') {
            steps {
                git 'https://gitee.com/forgotten/tomcat-java-demo'
                sh 'mvn clean package -Dmaven.test.skip=true'
            }
        }
        stage("deploy"){
            steps {
                sh 'cp /root/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps'
                sh '/usr/local/tomcat/bin/catalina.sh stop && /usr/local/tomcat/bin/catalina.sh start'
            }
        }
    }
}

构建成功

image-20231011215242380

流水线构建完成后webapps目录下可以看到拷贝和解压的ly-simple-tomcat-0.0.1-SNAPSHOT.war文件,服务重启后会自动解压war类型的文件

[root@localhost ~]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# 
[root@localhost webapps]# ls
docs      host-manager  jenkins.war                      ly-simple-tomcat-0.0.1-SNAPSHOT.war  ROOT
examples  jenkins       ly-simple-tomcat-0.0.1-SNAPSHOT  manager                              test
[root@localhost webapps]# 

访问网页,在地址栏端口号后面加上 /ly-simple-tomcat-0.0.1-SNAPSHOT/

image-20231011215537974

到此这篇关于tomcat部署jenkins项目的实现示例的文章就介绍到这了,更多相关tomcat部署jenkins 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文