Jenkins+GitHub 項目環境搭建和發佈腳本(二)

Jenkins+gitHub項目搭建配置java

 

 

 

 

項目發佈腳本 git

profilesScript.sh (支持不一樣環境配置文件)bash

#!/bin/bash
ACTIVE=$1
JENKINS_PATH=/var/lib/jenkins/workspace
APP_NAME=sb-system
APP_PATH=$JENKINS_PATH/$APP_NAME/sb-base/src/main/resources

if [ -d $APP_PATH ]; then
echo "${APP_PATH}"
else
echo "${APP_NAME} is not exist "
fi

cd $APP_PATH
if [ $ACTIVE ]; then
ACTIVE_PROFILES=application-${ACTIVE}.yml
else
echo " use default profiles"
exit
fi

if [ -f ${ACTIVE_PROFILES} ]; then
 echo "${ACTIVE_PROFILES}  is active "
 mv -f $ACTIVE_PROFILES  application.yml
else
 echo "${ACTIVE_PROFILES} is not exist use default profiles application.yml"
fi
echo "active profiles is ${ACTIVE_PROFILES} ."
exit
View Code

subscipt.sh (支持版本參數)app

#!/bin/bash

#版本號例如:1.0.0
version=$1
#備份時間版本
date=`date +%Y%m%d%H%M%S`

#app jar基本路徑
APPBASE_PATH=/usr/local/jar

#app jar備份路徑
APPBACKUP_PATH=$APPBASE_PATH/backup

#app jar路徑
APP_PATH=$APPBASE_PATH/wapps

#jar包名稱 例如:sb-base-1.0.0-SNAPSHOT.jar
APP_NAME=sb-base-${version}-SNAPSHOT.jar

# jenkins 路徑
JENKINS_PATH=/var/lib/jenkins/workspace

# jenkins編譯好的jar
APP_CLASS_PATH=$JENKINS_PATH/sb-system/sb-base/target/$APP_NAME

#判斷程序是否已啓動 未啓動返回1 啓動放回0
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
if [ -z $pid ]; then
return 1
else
return 0
fi
}

is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is restart..."
echo "kill pid ${pid}"
kill -s 9 $pid
else
echo "${APP_NAME} is not running" 
fi

#等待2秒jenkins 編譯項目
sleep 2

#複製編譯好的jar包
cp -f $APP_CLASS_PATH $APPBASE_PATH/

###########備份############
cd $APP_PATH
if [ -e ${APP_NAME} ]; then
mv -f ${APP_NAME}  $APPBACKUP_PATH/${APP_NAME%%.*}-${date}.jar
echo "${APP_NAME} 備份成功"
else
echo "${APP_NAME} is first deploy"
fi

##########jar移到wapps############
cd $APPBASE_PATH
mv -f  ${APP_NAME} $APP_PATH

#########判斷而且賦予執行權限#################
cd $APP_PATH
if [ -e $APP_NAME ]; then
chmod +x *.jar
else
echo "${APP_NAME} is not exist "
exit
fi

#########啓動jar包##############
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ." 
else
echo "程序開始啓動..."
echo "start jar path ${APP_PATH}/${APP_NAME}"

nohup java -Dcom.sun.akuma.Daemon=daemonized -Dhudson.util.ProcessTree.disable=true -DAPP_HOME=$APP_PATH -jar $APP_PATH/$APP_NAME  --daemon  >$APPBASE_PATH/wapps-subscibe.log 2>&1 &

echo "程序已啓動..."
fi

#等待1秒 檢測是否已經啓動成功
sleep 1
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. pid is ${pid} "
else
echo "${APP_NAME} is not running start app is fail ."
fi
exit
View Code

 項目是Spring Boot Jenkins會構建好了會Kill 全部的子進程 因此要注意 jar 啓動的參數ide

BUILD_ID=dnotkillMe nohup java -Dcom.sun.akuma.Daemon=daemonized -Dhudson.util.ProcessTree.disable=true -DAPP_HOME=/usr/local/jar/wapps -jar /usr/local/jar/wapps/sb-base-1.0.0-SNAPSHOT.jar --daemon >/usr/local/jar/wapps-subscibe.log 2>&1 &spa

相關文章
相關標籤/搜索