1.前置环境

前置环境配置:jdk、maven、git

2.在Jenkins配置git凭据

请查看往期文章: https://blog.csdn.net/RookiexiaoMu_a/article/details/122655272?spm=1001.2014.3001.5501

3.安装Publish over SSH插件

4.配置SSH Servers

安装完Publish over SSH插件之后,进入Manager Jenkins—>Configure System 往下拉到最后,找到SSH Servers: 填完之后点击:高级 其他的使用默认配置即可,点击应用 保存.

5.新建一个自由风格项目

不知如何新建的同学请查看往期文章: https://blog.csdn.net/RookiexiaoMu_a/article/details/122655272?spm=1001.2014.3001.5501 5.1填写配置:

5.2增加构建步骤:

输入Shell命令

pwd

if [ -e "*.jar" ]; then

rm -rf *.jar

fi

cp /var/lib/jenkins/workspace/test-parent-package/test-wechatpush/target/*.jar test-wechatpush.jar

这一步还在我们的jenkins机器上执行的,命令的意思是: 1.输出当前路径(jenkins上每个新建的项目都会在jenkisn工作目录新建一个文件夹) 例如此时pwd的输出为:/var/lib/jenkins/workspace/test-wechatpush 2.将当前路径下的jar包都删除 3.将jenkins工作路径下的目标jar包复制到当前的路径

5.3增加构建后操作:

此步骤会将Source files的jar文件复制到Remote directory目录

Remote directory:远程目录 Exec command:在远程机器上要执行的命令

5.4 远程机器上要执行的命令:Exec command

source /etc/profile

cd /db/app/test-parent

if [ -e "/db/app/test-parent/test-wechatpush/*.jar"]; then

rm /db/app/test-parent/test-wechatpush/test-wechatpush.jar

fi

cp test-wechatpush.jar /db/app/test-parent/test-wechatpush/test-wechatpush.jar

cd /db/app/test-parent/test-wechatpush/

api_id=`ps -ef | grep 8080 | grep -v "grep" | grep "test-wechatpush" | awk '{print $2}'`

echo $api_id

for id in $api_id

do

kill -9 $id

echo "killed $id"

done

BUILD_ID=dontKillMe

nohup java -Xms64m -Xmx128m -jar /db/app/test-parent/test-wechatpush/test-wechatpush.jar --name=test-wechatpush --spring.profiles.active=dev --server.port=8080 > /dev/null 2>&1 &

第一行:刷新配置 第二行:切换目录 第三、四、五行:移除远程目录上的test-wechatpush.jar文件 第六行:将Remote Directory(5.3步骤)目录下的test-wechatpush.jar文件复制到远程机器的/db/app/test-parent/test-wechatpush目录下 第七行:切换目录(如果不切换到运行的jar包所在目录,那么日志文件将在当前目录下) 第八至十四行:将端口为8080的进程杀掉(8080是我们程序的端口) 第十五行:如果在shell里使用nohup发现还是不能后台运行,加上此命令即可 第十六行:启动java项目命令

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: