Jenkins配置SpringBoot项目启动脚本

背景

上一篇Jenkins配置介绍了Jenkins远程部署的相关配置和步骤,但是最后的部署脚本只适用于部署原始tomcat下的war包应用,由于现在大部分后台项目已经重构成标准的SpingCloud微服务架构,所以更新了部署脚本来兼容SpringBoot应用。


脚本编写

#!/bin/bash
#这里可替换为你自己的执行程序,其他代码无需更改
APP_NAME=intelligent-family.jar
TARGET_PATH=/apps/intelligent-family
source /etc/profile
BUILD_ID=dontKillMe
ls_date=`date +%Y%m%d`

#启动方法

start(){

        pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`

        if [ "$pid" ]; then

                echo "$APP_NAME is already running. pid=$pid ."

        else

                cp ~/$APP_NAME $TARGET_PATH

                nohup java -jar $APP_NAME --spring.profiles.active=prod >> /export/logs/intelligentFamily/intelligent-family.log-$ls_date 2>&1 &

                sleep 10

                echo "$APP_NAME now is running"

        fi

}

#停止方法

stop(){

        pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`

        if [ "$pid" ]; then

                kill -9 $pid

                echo "Pid:$pid stopped"

        else

                echo "$APP_NAME is not running"

        fi

}

#输出运行状态

status(){

        pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`

        if [ "$pid" ]; then

                echo "$APP_NAME is running. Pid is ${pid}"

        else

                echo "$APP_NAME is NOT running."

        fi

}

#根据输入参数,选择执行对应方法,不输入则执行使用说明

case "$1" in

        start)

                start

                ;;

        stop)

                stop

                ;;

        status)

                status

                ;;

        restart)

                stop

                sleep 5

                start

                ;;

      *)

                echo "Usage:{start|stop|status|restart}"

                ;;

esac

exit 0

变量说明

  • source /etc/profile 加载环境变量,再部署时jenkins会报 “nohup: failed to run command java: No such file or directory ” 的错误,但是手动启动脚本没问题,加上后问题解决。

  • BUILD_ID=dontKillMe 解决Jenkins构建完成会在自动关闭进程及其子进程的问题

  • ls_date=date +%Y%m%d,日志文件重命名

使用说明

  • ./display.sh restart 应用重新启动

  • ./display.sh start 应用启动

  • ./display.sh stop 应用停止

  • ./display.sh status 应用运行状态


Q&A

jenkins部署时错误

http://smartair.haier.net/fastdfs/group1/M00/02/28/CsdgmF6BV8mAZawAAAJT5BYwlLs348.png

  • 原因:因为你是从一个非tty环境执行脚本;Jenkins不能正常从你的脚本中退出。
  • 解决办法:
    http://smartair.haier.net/fastdfs/group1/M00/02/28/CsdgmF6BWE-ADng1AAElw_zP_gE909.png

猜你喜欢

转载自www.cnblogs.com/jimoliunian/p/12965036.html
今日推荐