tomcat部署jenkins项目
一、jenkins介绍
Jenkins是一个开源的持续集成/持续交付(CI/CD)工具,旨在帮助开发团队通过自动化构建、测试和部署软件项目来提高产品质量和开发效率。
Jenkins支持各种编程语言和开发环境,可以与许多其他工具和技术集成,如Git、SVN、Maven、Docker等。通过插件机制,Jenkins可以扩展其功能,并支持自定义构建和部署流程。
Jenkins的主要功能包括:
- 自动化构建和测试:Jenkins可以自动从代码库中拉取源代码,并进行构建和测试,以确保代码的质量和稳定性。
- 持续集成和持续交付:Jenkins可以实现持续集成和持续交付,使得软件开发和部署更加高效和快速。
- 可视化构建流程:Jenkins提供了一个易于使用的Web界面,可以帮助用户更加清晰地了解构建流程和构建结果。
- 插件扩展:Jenkins支持插件扩展机制,用户可以根据自己的需求选择和安装不同的插件,扩展Jenkins的功能。
总之,Jenkins是一个强大的自动化部署工具,可以大大提高软件开发和部署的效率和质量。
二、jenkins部署
说明:
Jenkins需要部署在Tomcat上,Tomcat部署方法请看Tomcat部署-CSDN博客
在企业中,正确且合理的做法是Jenkins和项目要分开部署
环境:
操作系统 | Tomcat版本 | Jenkins版本 |
---|---|---|
centos-8 | tomcat-9.0.79 | Jenkins 2.414.2 |
准备工作:
关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
部署Jenkins
首先去官网下载Jenkins包
[root@localhost ~]# wget https://get.jenkins.io/war-stable/2.414.2/jenkins.war
[root@localhost ~]# ls
anaconda-ks.cfg jenkins.war #后缀为war的包是Java程序打包后的包
安装Jenkins所需要的Java17
[root@localhost ~]# yum -y install java-17-openjdk java-17-openjdk-devel
由于安装tomcat时安装的Java1.8,但是Jenkins需要Java17,所以切换一下Java版本
[root@localhost ~]# alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 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)
2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/bin/java)
Enter to keep the current selection[+], or type selection number: 2 #选择第二个版本
[root@localhost ~]#
java -jar命令用于执行Java程序的可执行JAR文件。
[root@localhost ~]# ls
anaconda-ks.cfg jenkins.war
[root@localhost ~]# java -jar jenkins.war
然后将这个war包移动到tomcat的webapps目录下
[root@localhost ~]# cp jenkins.war /usr/local/tomcat/webapps/
[root@localhost ~]# ls /usr/local/tomcat/webapps/
docs examples host-manager jenkins.war manager ROOT test
[root@localhost ~]#
重启tomcat服务,war将自动解压缩
[root@localhost ~]# cd /usr/local/tomcat/bin/
[root@localhost bin]# ./catalina.sh stop
[root@localhost bin]# ./catalina.sh start
[root@localhost bin]# ss -antl
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 100 *:8080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
[root@localhost bin]# cd ../webapps/
[root@localhost webapps]# ls
docs examples host-manager jenkins jenkins.war manager ROOT test #jenkins已经解压了
[root@localhost webapps]#
访问Jenkins页面
IP地址后接:8080/Jenkins
根据提示密码在/root/.jenkins/secrets/initialAdminPassword这个位置
[root@localhost webapps]# cat /root/.jenkins/secrets/initialAdminPassword
e76d60ca73a94f629e2911d2104476cf
[root@localhost webapps]#
输入密码登录后出现这个界面
选择安装推荐的插件
选择后会自动安装插件
安装完成后会要求你创建一个用户
**
**
创建Jenkins项目
输入任务名称,选择流水线
勾选不允许并发构建
往下翻找到高级项目选项
点击立即构建
构建成功
使用jenkins部署项目
在主机上安装git命令和maven打包工具
[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 | 1.60 MiB/s, done.
Resolving deltas: 100% (217/217), done.
[root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg jenkins.war tomcat-java-demo
[root@localhost ~]#
手动打包测试
[root@localhost tomcat-java-demo]# cd
[root@localhost ~]# cd tomcat-java-demo/
[root@localhost tomcat-java-demo]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src
[root@localhost tomcat-java-demo]#
[root@localhost tomcat-java-demo]# mvn clean package #打包
[INFO] Scanning for projects...
......
[INFO] Assembling webapp [ly-simple-tomcat] in [/root/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT]
[INFO] Processing war project
.......
[root@localhost tomcat-java-demo]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src target #多出来一个target目录
[root@localhost tomcat-java-demo]#
[root@localhost tomcat-java-demo]# cd target/
[root@localhost 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@localhost target]#
新建新的项目
生成拉取脚本,编写流水线脚本
仓库URL下面填写你要拉取的项目地址
生成拉取脚本后,编写流水线脚本
脚本内容
pipeline {
agent any
stages {
stage("pull code"){
steps {
git 'https://gitee.com/forgotten/tomcat-java-demo.git'
}
}
}
}
点击立即构建
拉取成功
查看流水线脚本拉取的项目
[root@localhost ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .groovy .jenkins .m2 .tcshrc .viminfo
.. .bash_history .bash_profile .cshrc .java jenkins.war .mysql_history tomcat-java-demo .wget-hsts
[root@localhost ~]# cd .jenkins/
[root@localhost .jenkins]# ls
caches nodes
config.xml org.jenkinsci.plugins.workflow.flow.FlowExecutionList.xml
hudson.model.UpdateCenter.xml plugins
hudson.plugins.git.GitTool.xml queue.xml
identity.key.enc secret.key
jenkins.install.InstallUtil.lastExecVersion secret.key.not-so-secret
jenkins.install.UpgradeWizard.state secrets
jenkins.model.JenkinsLocationConfiguration.xml updates
jenkins.telemetry.Correlator.xml userContent
jobs users
logs war
nodeMonitors.xml workspace
[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打包插件
选择需要安装的插件
点击安装
安装完成
手动打包成功后编写流水线脚本
流水线脚本代码
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'
}
}
}
}
构建成功
流水线构建完成后webapps目录下可以看到拷贝和解压的ly-simple-tomcat-0.0.1-SNAPSHOT.war文件,服务重启后会自动解压war类型的文件
[root@localhost test]# cd /usr/local/tomcat/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/访问项目