企业级jenkins部署项目shell脚本

#!/bin/bash

SpringBoot=$2
environment=$3
directory=$4
debug=$5

export JAVA_HOME=/home/java/jdk1.8.0_171
export PATH=$PATH:$JAVA_HOME/bin

if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {start|stop|restart|status|copy} \033[0m"
    exit 1
fi

if [ "$SpringBoot" = "" ];
then
    echo -e "\033[0;31m 未输入应用名 \033[0m"
    exit 1
fi

if [ "$environment" = "" ];
then
    echo -e "\033[0;31m 未输入环境类型 \033[0m"
    exit 1
fi


function start()
{
    echo "准备启动 $SpringBoot"
	count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$正在停止应用程序..."
		restart
    else
		if [ "$debug" != "debug" ];
		then
			echo "正在启动 $SpringBoot"
			BUILD_ID=dontKillMe
			nohup java -jar $SpringBoot --spring.profiles.active=$environment > /dev/null 2>&1 &
			echo "$SpringBoot 启动完成....."
		else
			echo "正在以DEBUG启动 $SpringBoot"
			BUILD_ID=dontKillMe
			nohup java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5678,suspend=n -jar $SpringBoot --spring.profiles.active=$environment > /dev/null 2>&1 &
			echo "$SpringBoot 以DEBUG启动完成....."
		fi
    fi
}

function stop()
{
    echo "正在停止 $SpringBoot"
    boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    echo "结束进程ID $boot_id,$SpringBoot 有 $count 个存在"
    if [ $count > 0 ];then
        kill $boot_id
        echo "杀死"
        count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
        echo "再次统计 $count"
        boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
        kill -9 $boot_id
    fi
}


function restart()
{
    stop
    sleep 2
    start
}

function copy(){
	#jar_path=/var/lib/jenkins/workspace/$directory/default/syh/${SpringBoot%.jar*}/target
	jar_path=/var/lib/jenkins/workspace/$directory/${SpringBoot%.jar*}/target
    echo "编译目录 $jar_path"
	target_path=/root/work/syh-project
	cd $jar_path
	cp $jar_path/$SpringBoot $target_path
    echo "成功复制到运行目录 $target_path"
	cd $target_path 
	restart
}

function status()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot 已经在运行"
    else
        echo "$SpringBoot 没有运行"
    fi
}

case $1 in
    start)
    start;;
    stop)
    stop;;
    restart)
    restart;;
    status)
    status;;
	copy)
	copy;;
    *)

    echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {start|stop|restart|status|copy}  {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
      \033[0;33m sh  $0  start test.jar \033[0m"
esac

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值