SpringBoot的Shell发版脚本
#!bin/bash
# 初始化变量
echo "开始部署..."
SERVICE_NAME=${1}
echo "服务名:${SERVICE_NAME}"
SERVICE_PORT=${2}
echo "服务端口:${SERVICE_PORT}"
BUILD_TAG=${3}
echo "构建标签为:${BUILD_TAG}"
ENVIROMENT=${4}
echo "当前环境为:${ENVIROMENT}"
# 基础路径
BASE_PATH=/opt/application
APPLICATION_PATH=${BASE_PATH}/${SERVICE_NAME}
source /etc/profile
cd ${APPLICATION_PATH}
# 重命名当前jar包
APP_NAME="${SERVICE_NAME}_${BUILD_TAG}"
mv ${SERVICE_NAME}.jar ${APP_NAME}.jar
echo "新jar包名字为:${APP_NAME}.jar"
# 初始化history-builds文件夹
if [ "`ls . | grep history-builds`" == "" ]; then
mkdir history-builds
fi
# 将上一版本jar包放到history-builds中
PREVIOUS_BUILD_JAR=`ls . | grep -v ${APP_NAME}.jar | grep .jar`
if [ "${PREVIOUS_BUILD_JAR}" != "" ]; then
temp=${PREVIOUS_BUILD_JAR#*_}
PREVIOUS_BUILD=${temp%.jar*}
echo "上一构建为:${PREVIOUS_BUILD}"
mv ${PREVIOUS_BUILD_JAR} history-builds/
fi
# 初始化history-logs文件夹
if [ "`ls . | grep history-logs`" == "" ]; then
mkdir history-logs
fi
# 将上一版本日志放到history-logs中
PREVIOUS_BUILD_LOG=`ls logs/ | grep -v ${APP_NAME}.log`
if [[ "${PREVIOUS_BUILD_LOG}" != "" && ${PREVIOUS_BUILD} != "" ]]; then
mv logs history-logs/${PREVIOUS_BUILD}
mkdir logs
fi
# 杀死进程, 存在误杀的可能性
# 根据端口获取pid,如果pid不为空,则停止运行
PID=`netstat -nlp | grep -w ${SERVICE_PORT} | sed -r 's#.* (.*)/.*#\1#'`
if [ "${PID}" != "" ]; then
echo "检测到端口${SERVICE_PORT} 被 PID:${PID}占用,开始尝试结束进程......"
KILL_COUNT=1
while [ "${PID}" != "" ]
do
echo "第${KILL_COUNT}次尝试结束${PID}进程......"
if [ ${KILL_COUNT} -lt 3 ];then
kill ${PID}
else
kill -9 ${PID}
fi
((KILL_COUNT++))
sleep 5
PID=`netstat -nlp | grep -w ${SERVICE_PORT} | sed -r 's#.* (.*)/.*#\1#'`
done
echo "----旧版本${SERVICE_NAME}服务应用已成功停止运行------"
else
echo "------没有检测到${SERVICE_NAME}服务应用------"
fi
echo "正在启动${SERVICE_NAME}服务应用......"
echo "正在运行${APP_NAME}.jar ......"
# 运行服务的命令
excute="java -jar -Xms256m -Xmx1024m ${APP_NAME}.jar --spring.profiles.active=${ENVIROMENT}"
echo "${excute}"
nohup ${excute} >> logs/${APP_NAME}.log 2>&1 &
sleep 20
cat logs/${APP_NAME}.log
NEW_PID=`netstat -nlp | grep -w ${SERVICE_PORT} | sed -r 's#.* (.*)/.*#\1#'`
echo "------------------"
if [ "${NEW_PID}" != "" ]; then
echo ${NEW_PID} > pid.txt
ps aux | sort -rnk 4 | grep 'java -jar'
echo "------------------"
echo "${SERVICE_NAME}服务应用发布成功,当前构建为:${BUILD_TAG}, 使用的配置环境为:${ENVIROMENT}"
exit 0
else
echo "${SERVICE_NAME}服务启动失败,请检查是否按规范进行服务应用的发布"
exit 500
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
帮我改善此页面 (opens new window)
上次更新: 2021/11/13, 06:02:18