# spring-boot-jenkins
Example of the deployment of a Spring Boot App with Jenkins in a Unix System
One thing that I found very hard to do was to integrate a spring boot project into a CI environment using jenkins. As a default behavior, the Jenkins process tree killer always kills the process started by a job which stops the execution of a Spring Boot App after the jenkins job finishes. In addition of that, I wanted to see the server log on the jenkyns windows until it finishes loading. This article will try to help us solving this problems.
But first I would like to discuss what I consider a good practice to a Spring Boot App CI environment. I find very useful to first copy the artifacts to a specified area on the server to keep track of the artifacts deployed and deploy the artifact from that location. Also, I create a server log file there and start to listening on the jenkins window until the server started.
So the script below does that. With some minor improvements self explained on the comments, but in summary it does this:
- stop whatever process running on the deployed port.
- delete the files of the previous deploy
- copy the files to deploy location
- start application with nohup command, java - jar
- start listening to the server log until it reaches a specific instruction.
Finally you have to do some adjustments to your job on Jenkins to avoid the default tree killing process. Just add this instruction before calling the sh: BUILD_ID=dontKillMe /path/to/my/script.sh (FIGURE 3)
You can see the jenkins job configuration window on FIGURES 1, 2, and 3 and the log result window on FIGURES 4 and 5.
Go to my github repo to check the project, but it recommend to extract the shell script to another repo to keep it lifecycle independent of your app.
https://github.com/rcoli/spring-boot-jenkins
This is my deploy folder structure (FIGURE 6):
```
-- spring-boot
---- dev
------ resources
-------- application.yml
------ initServer.log
------ my-app-jar
---- sit
------ resources
-------- application.yml
------ initServer.log
------ my-app-jar
---- uat
------ resources
-------- application.yml
------ initServer.log
------ my-app-jar
```
* dev - develop, sit - system integration testing, uat - user acceptance testing, application.yml - external app configuration file
This my project folder structure (FIGURE 7 and 8):
```
-- my-project
---- resources
------ application.yml
---- api
------ src
------ (other project file)
------ build.gradle
```
The script example.
```bash
#!/bin/bash
# COMMAND LINE VARIABLES
#enviroment FIRST ARGUMENT
# Ex: dev | sit | uat
env=$1
# deploy port SECOND ARGUMENT
# Ex: 8090 | 8091 | 8092
serverPort=$2
# THIRD ARGUMENT project name, deploy folder name and jar name
projectName=$3 #spring-boot
# FOURTH ARGUMENT external config file name
# Ex: application-localhost.yml
configFile=$4
#### CONFIGURABLE VARIABLES ######
#destination absolute path. It must be pre created or you can
# improve this script to create if not exists
destAbsPath=/home/rcoli/Desktop/$projectName/$env
configFolder=resources
##############################################################
#####
##### DONT CHANGE HERE ##############
#jar file
# $WORKSPACE is a jenkins var
sourFile=$WORKSPACE/api/build/libs/$projectName*.jar
destFile=$destAbsPath/$projectName.jar
#config files folder
sourConfigFolder=$WORKSPACE/$configFolder*
destConfigFolder=$destAbsPath/$configFolder
properties=--spring.config.location=$destAbsPath/$configFolder/$configFile
#CONSTANTS
logFile=initServer.log
dstLogFile=$destAbsPath/$logFile
#whatToFind="Started Application in"
whatToFind="Started "
msgLogFileCreated="$logFile created"
msgBuffer="Buffering: "
msgAppStarted="Application Started... exiting buffer!"
### FUNCTIONS
##############
function stopServer(){
echo " "
echo "Stoping process on port: $serverPort"
fuser -n tcp -k $serverPort > redirection &
echo " "
}
function deleteFiles(){
echo "Deleting $destFile"
rm -rf $destFile
echo "Deleting $destConfigFolder"
rm -rf $destConfigFolder
echo "Deleting $dstLogFile"
rm -rf $dstLogFile
echo " "
}
function copyFiles(){
echo "Copying files from $sourFile"
cp $sourFile $destFile
echo "Copying files from $sourConfigFolder"
cp -r $sourConfigFolder $destConfigFolder
echo " "
}
function run(){
#echo "java -jar $destFile --server.port=$serverPort $properties" | at now + 1 minutes
nohup nice java -jar $destFile --server.port=$serverPort $properties $> $dstLogFile 2>&1 &
echo "COMMAND: nohup nice java -jar $destFile --server.port=$serverPort $properties $> $dstLogFile 2>&1 &"
echo " "
}
function changeFilePermission(){
echo "Changing File Permission: chmod 777 $destFile"
chmod 777 $destFile
echo " "
}
function watch(){
tail -f $dstLogFile |
while IFS= read line
do
echo "$msgBuffer" "$line"
if [[ "$line" == *"$whatToFind"* ]]; then
echo $msgAppStarted
pkill tail
fi
done
}
### FUNCTIONS CALLS
#####################
# Use Example of this file. Args: enviroment | port | project name | external resourcce
# BUILD_ID=dontKillMe /path/to/this/file/api-deploy.sh dev 8082 spring-boot application-localhost.yml
# 1 - stop server on port ...
stopServer
# 2 - delete destinations folder content
deleteFiles
# 3 - copy files to deploy dir
copyFiles
changeFilePermission
# 4 - start server
run
# 5 - watch loading messages until ($whatToFind) message is found
watch
```
--- Jenkins Job Configuration (Git) FIGURE 1
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10940518/ed6d4062-82ed-11e5-88e8-6529970d2831.png)
--- Jenkins Job Configuration (Gradle) FIGURE 2
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10940527/fc1a0078-82ed-11e5-9dd7-aa75924b1d3f.png)
--- Jenkins Job Configuration (Deploy) FIGURE 3
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10940534/0678e232-82ee-11e5-84dd-6ca751e66903.png)
--- Jenkins Summary Beginning FIGURE 4
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10939540/74ed1058-82e9-11e5-9ca8-fcdfa9138647.png)
--- Jenkins Summary Finnished (Job Complete) FIGURE 5
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10939547/7a37dc6e-82e9-11e5-9b1e-bda47592ed6d.png)
--- Deploy Structure Folder FIGURE 6
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10939616/0aefad86-82ea-11e5-8d6b-40ca67df04f2.png)
--- Project structure folder FIGURE 7
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10939537/708ed014-82e9-11e5-85e1-c53ac1d219eb.png)
--- External Resources Folder FIGURE 8
![alt tag](https://cloud.githubusercontent.com/assets/1146514/10939548/7e91ed90-82e9-11e5-8a61-31e6f6f9c42a.png)
没有合适的资源?快使用搜索试试~ 我知道了~
基于Jenkins的Spring Boot应用部署系统.zip
共28个文件
bin:4个
prefs:4个
java:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 164 浏览量
2024-09-13
22:58:11
上传
评论
收藏 36KB ZIP 举报
温馨提示
该项目是一个使用Jenkins在Unix系统中部署Spring Boot应用的示例。它主要解决了一些在集成Spring bo项目ot到持续集成环境中遇到的难题,如Jenkins进程树杀手会终止Spring Boot应用的执行,以及在Jenkins窗口查看服务器日志的问题。该项目提供了一个脚本,用于复制工件到服务器指定目录、启动应用程序并监听服务器日志,直到应用启动完成。此外,还介绍了一些配置CI环境的最佳实践,如创建服务器日志文件、调整jenkins作业配置以避免默认进程树杀手等。项目文件夹结构包括开发、系统集成测试和用户验收测试等不同环境的资源文件和应用程序文件。总之,这是一个实用的spring Boot应用CI/CD部署流程示例。 用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
资源推荐
资源详情
资源评论
收起资源包目录
基于Jenkins的Spring Boot应用部署系统.zip (28个子文件)
spring-boot-jenkins-master
resources
application-localhost.yml 48B
LICENSE 6KB
api
.gradle
2.7
taskArtifacts
cache.properties 31B
cache.properties.lock 17B
fileSnapshots.bin 76KB
taskArtifacts.bin 26KB
fileHashes.bin 23KB
outputFileStates.bin 19KB
gradle
wrapper
gradle-wrapper.properties 200B
.classpath 520B
.settings
gradle
org.springsource.ide.eclipse.gradle.core.import.prefs 289B
org.springsource.ide.eclipse.gradle.refresh.prefs 267B
org.springsource.ide.eclipse.gradle.core.prefs 290B
org.eclipse.jdt.core.prefs 620B
src
test
java
br
com
rcoli
ApplicationTests.java 484B
main
resources
application.yml 73B
java
br
com
rcoli
Application.java 304B
controller
MainController.java 713B
model
Response.java 1KB
gradlew.bat 2KB
build.gradle 1KB
bin
.gitignore 58B
gradlew 5KB
.project 429B
.gitignore 8B
api-deploy.sh 3KB
.gitignore 189B
README.md 7KB
共 28 条
- 1
资源评论
sec0nd_
- 粉丝: 5921
- 资源: 1598
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 整车质量估计算法,采用simulink模型搭建,基于模糊逻辑思想,通过设计合理的模糊控制规则确定质量估计的置信度,当置信度高于某
- 基于Java和SQL Server 2008实现的图书馆管理系统,具备基本的增、改、查、备份与恢复功能
- comsol拓扑光子晶体单向传输
- 元胞自动机模拟,定向凝固模拟,微观组织模拟,增材制造微观组织模拟,激光熔覆微观组织模拟,等轴晶,柱状晶,凝固过程,合金凝固相场模
- 基于Java实现 的 时间片的6位动态密码生成客户端谷歌令牌
- 基于JAVA+MYSQL实现的网络考试系统
- 基于mysql游戏数据存储系统实现【完整代码+简介+学习攻略】
- 劲舞团008ACV解打包软件(008背景制作)
- 劲舞团3DMAX人物敬礼BIP动作
- 基于c语言的文件读取功能代码实现完整代码 文件读取功能逻辑实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功