部署描述:
1.jenkins 通过maven编译成jar 项目包
2.shell 脚本从jenkins机器发布到:目标主机
注释:次脚本没有写jar包的备份,有时间加上
脚本内容:
#!/bin/bash #线上服务器列表 HOST_LIST=${@:2} #项目名 REMOTE_PROJECT=$1 #远程JAR包存放目录 REMOTE_JARDIR=/apps/project/${REMOTE_PROJECT} #远程备份目录 REMOTE_BACKUP="/apps/${REMOTE_PROJECT}/backup" #远程执行用户 REMOTEUSER='co_dev' # 逐个部署每台服务器的微服务 for REMOTE in `echo $HOST_LIST` do # 远程包更新启动 PACKAGE=`find ./ -name ${REMOTE_PROJECT}.jar` scp $PACKAGE ${REMOTEUSER}@${REMOTE}:$REMOTE_JARDIR/${REMOTE_PROJECT}.jar if [ $? -eq 0 ];then # JAR shutdown JAR_pid=`ssh ${REMOTEUSER}@${REMOTE} ps -ef | grep "${REMOTE_PROJECT}.jar" | grep -v "grep"|awk '{print $2}'` if [ -n $JAR_pid ];then ssh ${REMOTEUSER}@${REMOTE} kill -9 $JAR_pid else echo "进程更不存在可以继续部署" fi else echo "${REMOTE_PROJECT}.jar No such file or directory" exit 1 fi # 启动jar 服务并检查端口 sleep 2 echo ".......................................开始启动$REMOTE_PROJECT 服务" ssh ${REMOTEUSER}@${REMOTE} "nohup /apps/sofe/jdk1.8.0_131/bin/java -jar -Xms2048M -Xmx4096M -XX:+UseConcMarkSweepGC -XX:-PrintGCDetails -Dspring.profiles.active=prod1 ${REMOTE_JARDIR}/${REMOTE_PROJECT}.jar >>/apps/project/${REMOTE_PROJECT}/${REMOTE_PROJECT}.log 2>&1 &" sleep 10 java_pid=`ssh $REMOTEUSER@$REMOTE ps -ef | grep "$REMOTE_PROJECT" | egrep -v "grep|log"|wc -l` if [[ $java_pid -ge 1 ]];then echo "${REMOTE_PROJECT} startup is successful" else echo "${REMOTE_PROJECT} startup is filed" exit 3 fi echo "$REMOTE 服务器 ${REMOTE_PROJECT} 项目部署完毕...................................." echo -e " " done