Jenkins+gitHub项目搭建配置
项目发布脚本
profilesScript.sh (支持不同环境配置文件)
#!/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
subscipt.sh (支持版本参数)
#!/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
项目是Spring Boot Jenkins会构建好了会Kill 所有的子进程 所以要注意 jar 启动的参数
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 &
原文地址:https://www.cnblogs.com/liu-king/p/11222379.html
时间: 2024-10-04 11:56:28