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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM