首先安裝JDK, MAVEN, Tomcat。www.lxweimin.com/p/b6cf12bce5b5
1. 官網下載 Jenkins.war,放在tomcat-》webapps文件夾下。
2. 啟動tomcat, 訪問 ip:port/jenkins
3. 安裝 Generic Webhook Trigger插件
4. Jenkins中新建一個 maven項目
通過配置 Optional Filter后,便可以實現URL 攜帶 名為 id,值為 feastbooking的 參數。
最后, 附上 spring-boot-jenkins.sh 的代碼:
#!/bin/bash
# COMMAND LINE VARIABLES
projectName=$1 #iplay
serverPort=$2
preferProp=$3
#active configuration, Ex: dev
if [ x$preferProp != x ]
then
properties=--spring.profiles.active=$preferProp
else
properties=""
fi
#### CONFIGURABLE VARIABLES ######
#destination absolute path. It must be pre created or you can
# improve this script to create if not exists
destAbsPath=/usr/local/$projectName
sourFile=$WORKSPACE/target/$projectName*.jar
destFile=$destAbsPath/$projectName.jar
logFile=initServer.log
dstLogFile=$destAbsPath/$logFile
whatToFind="Started "
msgAppStarted="Application Started... exiting buffer!"
### FUNCTIONS
##############
stopServer(){
echo " "
echo "Stoping process on port: $serverPort"
fuser -n tcp -k $serverPort > redirection &
echo " "
}
deleteFiles(){
echo "Deleting $destAbsPath"
rm -rf $destAbsPath
#echo "Deleting $dstLogFile"
#rm -rf $dstLogFile
echo " "
}
copyFiles(){
echo " "
if [ ! -d "$destAbsPath" ]
then
echo "create folder $destAbsPath"
mkdir "$destAbsPath"
fi
mv $sourFile $destFile
echo "Moving files from $sourFile to $destFile"
}
run(){
output="COMMAND: nohup nice java -jar $destFile --server.port=$serverPort "
if [ $properties = "" ]
then
nohup nice java -jar $destFile --server.port=$serverPort v&
echo "$output""$"
else
nohup nice java -jar $destFile --server.port=$serverPort --spring.profiles.active=$preferProp &
echo "$output""$properties"" &"
fi
echo " "
}
watch(){
tail -f $dstLogFile |
while IFS= read line
do
echo "$line"
if [[ "$line" == *"$whatToFind"* ]]
then
echo $msgAppStarted
pkill? tail
fi
done
}
### FUNCTIONS CALLS
#####################
# Use Example of this file. Args: project name | server port | configuration file: application.properties
# BUILD_ID=dontKillMe /path/to/this/file/spring-boot-jenkins.sh iplay 8081 application-run.properties
# 1 - stop server on port ...
stopServer
# 2 - delete destinations folder content
deleteFiles
# 3 - copy files to deploy dir
copyFiles
# 4 - start server
run
# 5 - watch loading messages until? ($whatToFind) message is found
#watch
注意:此代碼在linux下運行可能會產生格式問題,可以在vim中設置一下:
windows中編輯了sh文件后 放在linux中 使用vi轉碼
:set fileformat=unix
shell 代碼參考:github.com/rcoli/spring-boot-jenkins