Jenkins是一个可扩展的、基于Web的工具,用于自动化构建、测试和部署软件项目。它支持各种版本控制系统、构建工具和测试框架,并提供了丰富的插件生态系统来满足不同的需求。
优点:
缺点:
Jenkins的工作原理基于事件驱动和插件系统。它通过不断监测源代码管理系统中的代码变更或预定的时间触发事件,然后根据预定义的构建配置和操作来执行构建、测试和部署任务。
jenkins需部署在tomcat服务上
操作系统 | tomcat版本 | jenkins版本 |
---|---|---|
centos 8 | tomcat-9.0.80 | jenkins-2.414.2 |
//关闭防火墙和selinux
[root@tomcat ~]# systemctl disable --now firewalld
[root@tomcat ~]# setenforce 0
[root@tomcat ~]# vim /etc/selinux/config
[root@tomcat ~]# reboot //重启主机生效
[root@tomcat ~]# getenforce
Disabled //确保selinux状态为disabled
//配置yum源,建议使用阿里云的centos8源
[root@tomcat ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 2338 0 0:00:01 0:00:01 --:--:-- 2338
[root@tomcat ~]# yum clean all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
17 files removed
[root@tomcat ~]# yum makecache
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
CentOS-8.5.2111 - Base - mirrors.aliyun.com 410 kB/s | 4.6 MB 00:11
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 19 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 757 kB/s | 8.4 MB 00:11
Metadata cache created.
//安装java环境
[root@tomcat ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel wget vim
[root@tomcat ~]# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
//拉取tomcat软件包
[root@tomcat ~]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz
--2023-10-10 13:41:29-- https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz
Resolving archive.apache.org (archive.apache.org)... 65.108.204.189, 2a01:4f9:1a:a084::2
Connecting to archive.apache.org (archive.apache.org)|65.108.204.189|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11692853 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-9.0.80.tar.gz’
apache-tomcat-9.0.80.tar.gz 100%[===============================================>] 11.15M 25.2KB/s in 5m 55s
2023-10-10 13:47:25 (32.1 KB/s) - ‘apache-tomcat-9.0.80.tar.gz’ saved [11692853/11692853]
[root@tomcat ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.80.tar.gz
//解压tomcat软件包
[root@tomcat ~]# tar xf apache-tomcat-9.0.80.tar.gz
[root@tomcat ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.80 apache-tomcat-9.0.80.tar.gz jenkins.war
//将解压后的目录复制两份放到/usr/local/目录下,一个用于运行jenkins,另一个用来运行tomcat中的项目
[root@tomcat ~]# cp -r apache-tomcat-9.0.80 /usr/local/jenkins
[root@tomcat ~]# cp -r apache-tomcat-9.0.80 /usr/local/tomcat
[root@tomcat ~]# ls /usr/local/ && cd /usr/local/
bin etc games include jenkins lib lib64 libexec sbin share src tomcat
//更改tomcat中的端口号,与jenkins以示区分
[root@tomcat ~]# cd /usr/local/tomcat/conf/
[root@tomcat conf]# ls
catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd
catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml
[root@tomcat conf]# vim server.xml
[root@tomcat conf]# grep -A1 '
"8006" shutdown="SHUTDOWN">
"org.apache.catalina.startup.VersionLoggerListener" />
[root@tomcat conf]# grep -A3 '
"8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8444"
maxParameterCount="1000"
//开启服务,查看端口
[root@tomcat conf]# ../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@tomcat conf]# cd
[root@tomcat ~]# cd /usr/local/jenkins/
[root@tomcat jenkins]# bin/catalina.sh start
Using CATALINA_BASE: /usr/local/jenkins
Using CATALINA_HOME: /usr/local/jenkins
Using CATALINA_TMPDIR: /usr/local/jenkins/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/jenkins/bin/bootstrap.jar:/usr/local/jenkins/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@tomcat jenkins]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 1 [::ffff:127.0.0.1]:8006 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 100 *:8081 *:*
Jenkins官网https://www.jenkins.io/download/
///安装Jenkins所需要的Java-17-openjdk
[root@tomcat local]# cd jenkins/
[root@tomcat jenkins]# yum -y install java-17-openjdk java-17-openjdk-devel
//由于安装tomcat时安装的Java1.8,但是Jenkins需要Java17,为了防止版本冲突,需要配置jdk的优先级,从而能够同时使用这两个版本
[root@tomcat jenkins]# alternatives --config java //优先使用java-17-openjdk
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@tomcat ~]# bash //bash使其生效
//下载jenkins.war的包
[root@tomcat ~]# wget wget https://get.jenkins.io/war-stable/2.414.2/jenkins.war
[root@tomcat ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.80 apache-tomcat-9.0.80.tar.gz jenkins.war
////将Jenkins包复制到我们为其准备的其中一个tomcat的/usr/local/jenkins/webapps/目录中去,也就是我们前面复制的jenkins中去
[root@tomcat ~]# cp jenkins.war /usr/local/jenkins/webapps/
[root@tomcat ~]# cd /usr/local/jenkins/webapps/
[root@tomcat webapps]# ls
docs examples host-manager jenkins.war manager ROOT
//重启服务后,则会自动解压jenkins.war
[root@tomcat webapps]# ../bin/catalina.sh stop
Using CATALINA_BASE: /usr/local/jenkins
Using CATALINA_HOME: /usr/local/jenkins
Using CATALINA_TMPDIR: /usr/local/jenkins/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/jenkins/bin/bootstrap.jar:/usr/local/jenkins/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@tomcat webapps]# ../bin/catalina.sh start
Using CATALINA_BASE: /usr/local/jenkins
Using CATALINA_HOME: /usr/local/jenkins
Using CATALINA_TMPDIR: /usr/local/jenkins/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/jenkins/bin/bootstrap.jar:/usr/local/jenkins/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@tomcat webapps]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 1 [::ffff:127.0.0.1]:8006 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 100 *:8081 *:*
[root@tomcat webapps]# ls //jenkins自动解压
docs examples host-manager jenkins jenkins.war manager ROOT
[root@tomcat webapps]# cat /root/.jenkins/secrets/initialAdminPassword
3a5952875fdc4a4c91fc87d480f32338
安装所需的软件包
[root@tomcat ~]# yum -y install git maven
[root@tomcat ~]# which git
/usr/bin/git
[root@tomcat ~]# which mvn
/usr/bin/mvn
脚本内容:
//在gitee网站上拉取一个项目目录,然后领用mvn命令压缩至我们想要的.war格式,再将压缩包放置到tomcat的项目目录中,重启tomcat服务后自动解压,从而能够通过tomcat服务端口号访问到我们拉取的项目网页
pipeline {
agent any
stages {
stage('pull code and package') {
steps {
git 'https://gitee.com/forgotten/tomcat-java-demo.git'
sh 'mvn clean package'
}
}
stage('publish') {
steps {
sh 'cp /root/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/ftx.war'
sh '/usr/local/tomcat/bin/catalina.sh stop && /usr/local/tomcat/bin/catalina.sh start'
}
}
}
}
通过jenkins压缩的包存放位置
[root@tomcat target]# ls
classes ly-simple-tomcat-0.0.1-SNAPSHOT maven-archiver
generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-status
[root@tomcat target]# pwd
/root/.jenkins/workspace/test/target
进入tomcat项目进行查看
[root@tomcat webapps]# pwd
/usr/local/tomcat/webapps
[root@tomcat webapps]# ls
docs examples ftx ftx.war host-manager manager ROOT
通过8081端口访问我们拉取的项目
generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-status
[root@tomcat target]# pwd
/root/.jenkins/workspace/test/target
**进入tomcat项目进行查看**
~~~powershell
[root@tomcat webapps]# pwd
/usr/local/tomcat/webapps
[root@tomcat webapps]# ls
docs examples ftx ftx.war host-manager manager ROOT
通过8081端口访问我们拉取的项目