Ubuntu16 安装Jenkins 实现自动部署

首先安装JDK, MAVEN, Tomcat。www.jianshu.com/p/b6cf12bce5b5

1. 官网下载 Jenkins.war,放在tomcat-》webapps文件夹下。

2. 启动tomcat, 访问 ip:port/jenkins

3. 安装 Generic Webhook Trigger插件

4. Jenkins中新建一个 maven项目

Ubuntu16 安装Jenkins 实现自动部署_第1张图片
Ubuntu16 安装Jenkins 实现自动部署_第2张图片
Ubuntu16 安装Jenkins 实现自动部署_第3张图片


Ubuntu16 安装Jenkins 实现自动部署_第4张图片

通过配置 Optional Filter后,便可以实现URL 携带 名为 id,值为 feastbooking的 参数。


Ubuntu16 安装Jenkins 实现自动部署_第5张图片


Ubuntu16 安装Jenkins 实现自动部署_第6张图片


Ubuntu16 安装Jenkins 实现自动部署_第7张图片
Ubuntu16 安装Jenkins 实现自动部署_第8张图片

最后, 附上 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

截图7,8来自:blog.csdn.net/xlgen157387/article/details/72852428

你可能感兴趣的:(Ubuntu16 安装Jenkins 实现自动部署)