windows Jenkins自动化部署微服务到Linux

安装Jenkins

安装winscp

配置环境变量到path

将Jenkins所有jar上传到Linux,并且部署服务

@echo off
echo copying...
for /f "delims==" %%a in ('dir /b /s *.jar')do copy /y "%%a" upload
del ".\upload\*common*.jar"
winscp /console /command "option batch continue"  "open root:u01!@#$U01@192.168.6.11" "put upload /home/suncn/server/" "call /home/suncn/server/suncn.sh deploy all" "exit"
echo finish...
exit

自动化部署脚本

#! /bin/sh
# 端口号
PORTS=(9010 9004 9002 9003)
# 模块
MODULES=(gateway ubom message log)
# 模块名称
MODULE_NAMES=(网关服务 运管平台 消息服务 日志服务)
# jar包数组
JARS=(dataintell-gateway-1.0.0.jar dataintell-ubom-1.0.0.jar dataintell-message-1.0.0.jar dataintell-log-1.0.0.jar)
# jar包路径
SERVER_PATH='/home/suncn/server'
JAR_PATH=$SERVER_PATH'/jar'
# 日志路径
LOG_PATH=$SERVER_PATH'/log/'
#last jar包路径
LAST_PATH=$SERVER_PATH'/last'
#upload jar包路径
UPLOAD_PATH=$SERVER_PATH'/upload'

JVM=' -Xms512m -Xmx1024m '
#===获取启动参数
RUN_MODE_NAME=$1
SERVICE_NAME=$2

start() {
  local MODULE=
  local MODULE_NAME=
  local JAR_NAME=
  local command=$1
  local commandOk=0
  local count=0
  local okCount=0
  local port=0
  for((i=0;i<${#MODULES[@]};i++))
  do
    MODULE=${MODULES[$i]}
    MODULE_NAME=${MODULE_NAMES[$i]}
    JAR_NAME=${JARS[$i]}
    PORT=${PORTS[$i]}
    if [ "$command" == "all" ] || [ "$command" == "$MODULE" ];then
      commandOk=1
      count=0
      PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
      if [ -n "$PID" ];then
        echo "$MODULE---$MODULE_NAME:已经运行,PID=$PID"
      else
        exec nohup java $JVM -jar $JAR_PATH/$JAR_NAME >> $LOG_PATH/$MODULE.log 2>&1 &
        PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        while [ -z "$PID" ]
        do
          if (($count == 30));then
            echo "$MODULE---$MODULE_NAME:$(expr $count \* 10)秒内未启动,请检查!"
            break
          fi
          count=$(($count+1))
          echo "$MODULE_NAME启动中.................."
          sleep 10s
          PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        done
        okCount=$(($okCount+1))
        echo "$MODULE---$MODULE_NAME:已经启动成功,PID=$PID"
      fi
    fi
  done
  if(($commandOk == 0));then
    echo "第二个参数输入错误"
  else
    echo "............本次共启动:$okCount个服务..........."
  fi
}
 
stop() {
  local MODULE=
  local MODULE_NAME=
  local JAR_NAME=
  local command="$1"
  local commandOk=0
  local okCount=0
  for((i=0;i<${#MODULES[@]};i++))
  do
    MODULE=${MODULES[$i]}
    MODULE_NAME=${MODULE_NAMES[$i]}
    JAR_NAME=${JARS[$i]}
    if [ "$command" = "all" ] || [ "$command" = "$MODULE" ];then
      commandOk=1
      PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
      if [ -n "$PID" ];then
        echo "$MODULE---$MODULE_NAME:准备结束,PID=$PID"
        kill -9 $PID
        PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        while [ -n "$PID" ]
        do
          sleep 3s
          PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        done
        echo "$MODULE---$MODULE_NAME:成功结束"
        okCount=$(($okCount+1))
      else
        echo "$MODULE---$MODULE_NAME:未运行"
      fi
    fi
  done
  if (($commandOk == 0));then
    echo "参数输入错误!"
  else
    echo "............本次共停止:$okCount个服务............"
  fi
}

#===是否显示帮助文档
SHOW_HELP="0"
if [ -n "$RUN_MODE_NAME" ] && [ "$RUN_MODE_NAME" != "start" ] && [ "$RUN_MODE_NAME" != "stop" ] && [ "$RUN_MODE_NAME" != "restart" ]&&[ "$RUN_MODE_NAME" != "deploy" ];then
   SHOW_HELP="1"
fi

if [ -n "$SERVICE_NAME" ] && [ "$SERVICE_NAME" != "all" ];then
    HAVE_MODULE="0"
    for((i=0;i<${#MODULES[@]};i++))
      do
        if [ "${MODULES[$i]}" == "$SERVICE_NAME" ];then
	    HAVE_MODULE="1"
	fi
      done
    if [ "$HAVE_MODULE" == "0" ];then
      SHOW_HELP="1"
    fi
fi

if [ "$SHOW_HELP" == "1" ];then
     echo "用法:suncn [start|stop|restart] server_name"
     echo "其中server_name值包括:"
     echo "        all:"
     echo "            所有服务"
     for((i=0;i<${#MODULES[@]};i++))
       do
         echo "        ${MODULES[$i]}:"
	 echo "            ${MODULE_NAMES[$i]}"
       done

       echo "示例:suncn start eureka"
     exit 1
fi

case "$RUN_MODE_NAME" in
  start)
    RUN_MODE_NAME=启动
    RUN_MODE=0
  ;;
  stop)
    RUN_MODE_NAME=停止
    RUN_MODE=1
  ;;
  restart)
    RUN_MODE_NAME=重启
    RUN_MODE=2
  ;;
  deploy)
	RUN_MODE_NAME=部署
	RUN_MODE=3
  ;;
  *)

  ;;
esac

#无参数启动,提示用户从控制台选择 模式
while [ -z $RUN_MODE_NAME ]
do

echo " 0: 启动"
echo " 1: 停止"
echo " 2: 重启服务"
echo -n "请选择运行模式[默认0]:"
read RUN_MODE
RUN_MODE=${RUN_MODE:-0}

case "$RUN_MODE" in
  0)
    RUN_MODE_NAME=启动
  ;;
  1)
    RUN_MODE_NAME=停止
  ;;
  2)
    RUN_MODE_NAME=重启
  ;;
  3)
    RUN_MODE_NAME=部署
  ;;
  *)
        echo "ERROR:==================="    
        echo "ERROR:==不存在的编号,请重新选择!!!=="    
        echo "ERROR:===================" 
  ;;
esac
done

#无参数启动,提示用户从控制台选择 服务
while [ -z $SERVICE_NAME ]
  do
    echo "0:所有服务"
    for((i=0;i<${#MODULES[@]};i++))
      do
        echo "$[i+1]:${MODULE_NAMES[$i]}"
      done
    echo -n "请选择需要【${RUN_MODE_NAME}】的服务[默认0]:"
    read  SERVICE_INDEX
    SERVICE_INDEX=${SERVICE_INDEX:-0}

    for((i=0;i<${#MODULES[@]};i++))
      do
        if [ "$SERVICE_INDEX" == "$[i+1]" ];then
          SERVICE_NAME=${MODULES[$i]}
        fi
      done

      if [ "$SERVICE_INDEX" == "0" ];then
	SERVICE_NAME=all
      fi
      if [ -z $SERVICE_NAME ];then
        echo "ERROR:==================="    
        echo "ERROR:==不存在的编号,请重新选择!!!=="    
        echo "ERROR:===================" 
      fi
  done


case "$RUN_MODE" in
  0)
    start "$SERVICE_NAME"
  ;;
  1)
    stop "$SERVICE_NAME"
  ;;
  2)
    stop "$SERVICE_NAME"
    sleep 3s
   start "$SERVICE_NAME"
  ;;
  3)
    stop "$SERVICE_NAME"
	mkdir -p $LAST_PATH
	mkdir -p $UPLOAD_PATH
	mkdir -p $JAR_PATH
	mkdir -p $LOG_PATH
	`cp -rf $JAR_PATH"/." $LAST_PATH`
	`cp -rf $UPLOAD_PATH"/." $JAR_PATH`
	rm -rf $UPLOAD_PATH"/*"
    sleep 3s
    start "$SERVICE_NAME"
  ;;
  *)
    echo "参数输入错误!"
    exit 1
  ;;
esac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值