发布于 

jpom构建java服务后自动部署,回滚,消息推送

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
141
142
143
144
145
146
147
148
149
150
151
152
153
# !/bin/bash

APP_DIR="/data/apps/touchs"
BIN_DIR="$APP_DIR/bin"
HISTORY_DIR="$BIN_DIR/history"
LOG_DIR="$APP_DIR/logs"
LOG_FILE="$LOG_DIR/info.log"
JAR_NAME="touchs.jar"
NEW_JAR="target/$JAR_NAME"

feishu_webhook="https://open.feishu.cn/open-apis/bot/v2/hook/token1"
feishu_webhook2="https://open.feishu.cn/open-apis/bot/v2/hook/token2"

export TZ="Asia/Shanghai"

mkdir -p $BIN_DIR $HISTORY_DIR $LOG_DIR

backup_old_version() {
if [ -f "$BIN_DIR/$JAR_NAME" ]; then
TIMESTAMP=$(date +%Y%m%d%H%M%S)
mv "$BIN_DIR/$JAR_NAME" "$HISTORY_DIR/${JAR_NAME%.jar}-$TIMESTAMP.jar"
push_to_feishu "旧版本已备份:$HISTORY_DIR/${JAR_NAME%.jar}-$TIMESTAMP.jar"
else
push_to_feishu "未发现旧版本 JAR 包,无需备份。"
fi
}

# 推送到飞书
function push_to_feishu() {

content="$1"

cur_datetime=$(date +"%Y-%m-%d %H:%M:%S")


message="当前操作:${content}\n"
message+="进行时间:${cur_datetime}\n"

read -r -d '' json_payload <<EOF
{
"msg_type": "text",
"content": {
"text": "${message}"
}
}
EOF

echo ">>> JSON Payload: ${json_payload}"

response=$(curl -H "Content-Type: application/json" -X POST -d "${json_payload}" "${feishu_webhook}")
echo ">>> 飞书响应: ${response} <<<"
response2=$(curl -H "Content-Type: application/json" -X POST -d "${json_payload}" "${feishu_webhook2}")
echo ">>> 飞书响应2: ${response2} <<<"
}

# 函数:启动应用
start_app() {
push_to_feishu "正在启动应用..."
nohup java -jar "$BIN_DIR/$JAR_NAME" > $LOG_FILE 2>&1 &
sleep 10
APP_PID=$(pgrep -f $JAR_NAME)
if [ -z "$APP_PID" ]; then
push_to_feishu "应用启动失败..."
return 1
else
push_to_feishu "应用启动成功,PID: $APP_PID"
return 0
fi
}

rollback() {
push_to_feishu "正在回滚到上一版本..."

push_to_feishu "正在停止当前服务..."
APP_PID=$(pgrep -f $JAR_NAME)
if [ -n "$APP_PID" ]; then
kill -9 $APP_PID
push_to_feishu "当前服务已停止,PID: $APP_PID"
echo "当前服务已停止,PID: $APP_PID" >> $LOG_FILE
else
push_to_feishu "当前服务未运行,无需停止..."
echo "当前服务未运行,无需停止。" >> $LOG_FILE
fi

# 查找最新备份文件
LATEST_BACKUP=$(ls -t $HISTORY_DIR | head -n 1)
if [ -n "$LATEST_BACKUP" ]; then
push_to_feishu "发现备份文件:$LATEST_BACKUP,正在回滚..."
echo "发现备份文件:$LATEST_BACKUP,正在回滚..." >> $LOG_FILE
mv "$HISTORY_DIR/$LATEST_BACKUP" "$BIN_DIR/$JAR_NAME"
push_to_feishu "备份文件已恢复为当前版本:$LATEST_BACKUP"
echo "备份文件已恢复为当前版本:$LATEST_BACKUP" >> $LOG_FILE
else
echo "未找到任何备份文件,回滚失败!" >> $LOG_FILE
return 1
fi

push_to_feishu "正在启动回滚版本..."
echo "正在启动回滚版本..." >> $LOG_FILE
start_app
if [ $? -eq 0 ]; then
push_to_feishu "回滚成功,服务已启动..."
echo "回滚成功,服务已启动。" >> $LOG_FILE
else
push_to_feishu "回滚失败,服务未能启动,请检查日志..."
echo "回滚失败,服务未能启动,请检查日志。" >> $LOG_FILE
fi
}

cleanup_logs() {
push_to_feishu "正在清理 15 天前的日志..."
echo "正在清理 15 天前的日志..." >> $LOG_FILE
find $LOG_DIR -type f -mtime +15 -exec rm -f {} \;
push_to_feishu "日志清理完成..."
echo "日志清理完成。" >> $LOG_FILE
}

# 主逻辑
echo "======== 部署开始 $(date) ========" > $LOG_FILE
push_to_feishu "服务端部署开始"
BUILD_JAR_PATH=$(pwd)
push_to_feishu "路径:$BUILD_JAR_PATH/$NEW_JAR ,$BIN_DIR/"
cd $APP_DIR || { echo "目录 $APP_DIR 不存在!" >> $LOG_FILE; exit 1; }

backup_old_version

# if [ -e "$BUILD_JAR_PATH/$NEW_JAR" ]; then
# push_to_feishu "文件 $(pwd)/$NEW_JAR 存在"
# else
# push_to_feishu "文件 $(pwd)/$NEW_JAR 不存在"
# fi


mv $BUILD_JAR_PATH/$NEW_JAR $BIN_DIR/ > $LOG_FILE 2>&1
push_to_feishu "正在停止旧应用..."
echo "正在停止旧应用..." >> $LOG_FILE

APP_PID=$(pgrep -f $JAR_NAME)
if [ -n "$APP_PID" ]; then
kill -9 $APP_PID
push_to_feishu "旧应用已停止..."
echo "旧应用已停止。" >> $LOG_FILE
else
push_to_feishu "未检测到运行中的应用..."
echo "未检测到运行中的应用。" >> $LOG_FILE
fi
start_app
if [ $? -ne 0 ]; then
rollback
fi
cleanup_logs
push_to_feishu "服务端部署完成..."
echo "======== 部署完成 $(date) ========" >> $LOG_FILE

把脚本放在在线构建-构建列表-选中要用的构建-发布操作-本地命令

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# !/bin/bash

APP_DIR="/data/apps/touchs"
BIN_DIR="$APP_DIR/bin"
HISTORY_DIR="$BIN_DIR/history"
LOG_DIR="$APP_DIR/logs"
LOG_FILE="$LOG_DIR/info.log"
JAR_NAME="touchs.jar"
NEW_JAR="target/$JAR_NAME"

feishu_webhook="https://open.feishu.cn/open-apis/bot/v2/hook/token1"
feishu_webhook2="https://open.feishu.cn/open-apis/bot/v2/hook/token2"

export TZ="Asia/Shanghai"

mkdir -p $BIN_DIR $HISTORY_DIR $LOG_DIR

backup_old_version() {
if [ -f "$BIN_DIR/$JAR_NAME" ]; then
TIMESTAMP=$(date +%Y%m%d%H%M%S)
mv "$BIN_DIR/$JAR_NAME" "$HISTORY_DIR/${JAR_NAME%.jar}-$TIMESTAMP.jar"
push_to_feishu "旧版本已备份:$HISTORY_DIR/${JAR_NAME%.jar}-$TIMESTAMP.jar"
else
push_to_feishu "未发现旧版本 JAR 包,无需备份。"
fi
}

# 推送到飞书
function push_to_feishu() {

content="$1"

cur_datetime=$(date +"%Y-%m-%d %H:%M:%S")


message="状态:${content}\n"
message+="时间:${cur_datetime}"

read -r -d '' json_payload <<EOF
{
"msg_type": "text",
"content": {
"text": "${message}"
}
}
EOF

echo ">>> JSON Payload: ${json_payload}"

response=$(curl -H "Content-Type: application/json" -X POST -d "${json_payload}" "${feishu_webhook}")
echo ">>> 飞书响应: ${response} <<<"
response2=$(curl -H "Content-Type: application/json" -X POST -d "${json_payload}" "${feishu_webhook2}")
echo ">>> 飞书响应2: ${response2} <<<"
}

# 函数:启动应用
start_app() {
push_to_feishu "正在启动新版本..."
nohup java -jar "$BIN_DIR/$JAR_NAME" > $LOG_FILE 2>&1 &
sleep 10
APP_PID=$(pgrep -f $JAR_NAME)
if [ -z "$APP_PID" ]; then
push_to_feishu "新版本启动失败..."
return 1
else
push_to_feishu "新版本启动成功,PID: $APP_PID"
return 0
fi
}

rollback() {
push_to_feishu "正在回滚到上一版本..."

push_to_feishu "正在停止当前服务..."
APP_PID=$(pgrep -f $JAR_NAME)
if [ -n "$APP_PID" ]; then
kill -9 $APP_PID
push_to_feishu "当前服务已停止,PID: $APP_PID"
echo "当前服务已停止,PID: $APP_PID" >> $LOG_FILE
else
push_to_feishu "当前服务未运行,无需停止..."
echo "当前服务未运行,无需停止。" >> $LOG_FILE
fi

# 查找最新备份文件
LATEST_BACKUP=$(ls -t $HISTORY_DIR | head -n 1)
if [ -n "$LATEST_BACKUP" ]; then
push_to_feishu "发现备份文件:$LATEST_BACKUP,正在回滚..."
echo "发现备份文件:$LATEST_BACKUP,正在回滚..." >> $LOG_FILE
mv "$HISTORY_DIR/$LATEST_BACKUP" "$BIN_DIR/$JAR_NAME"
push_to_feishu "备份文件已恢复为当前版本:$LATEST_BACKUP"
echo "备份文件已恢复为当前版本:$LATEST_BACKUP" >> $LOG_FILE
else
echo "未找到任何备份文件,回滚失败!" >> $LOG_FILE
return 1
fi

push_to_feishu "正在启动回滚版本..."
echo "正在启动回滚版本..." >> $LOG_FILE
start_app
if [ $? -eq 0 ]; then
push_to_feishu "回滚成功,服务已启动..."
echo "回滚成功,服务已启动。" >> $LOG_FILE
else
push_to_feishu "回滚失败,服务未能启动,请检查日志..."
echo "回滚失败,服务未能启动,请检查日志。" >> $LOG_FILE
fi
}

cleanup_logs() {
push_to_feishu "正在清理 15 天前的日志..."
echo "正在清理 15 天前的日志..." >> $LOG_FILE
find $LOG_DIR -type f -mtime +15 -exec rm -f {} \;
push_to_feishu "日志清理完成..."
echo "日志清理完成。" >> $LOG_FILE
}

# 主逻辑
echo "======== 部署开始 $(date) ========" > $LOG_FILE
push_to_feishu "打包完毕"
push_to_feishu "开始部署"
BUILD_JAR_PATH=$(pwd)

cd $APP_DIR || { echo "目录 $APP_DIR 不存在!" >> $LOG_FILE; exit 1; }

backup_old_version

# if [ -e "$BUILD_JAR_PATH/$NEW_JAR" ]; then
# push_to_feishu "文件 $(pwd)/$NEW_JAR 存在"
# else
# push_to_feishu "文件 $(pwd)/$NEW_JAR 不存在"
# fi


mv $BUILD_JAR_PATH/$NEW_JAR $BIN_DIR/ > $LOG_FILE 2>&1
push_to_feishu "正在停止旧版本..."
echo "正在停止旧版本..." >> $LOG_FILE

APP_PID=$(pgrep -f $JAR_NAME)
if [ -n "$APP_PID" ]; then
kill -9 $APP_PID
push_to_feishu "旧版本已停止..."
echo "旧版本已停止。" >> $LOG_FILE
else
push_to_feishu "未检测到运行中的应用..."
echo "未检测到运行中的应用。" >> $LOG_FILE
fi
start_app
if [ $? -ne 0 ]; then
rollback
fi
cleanup_logs
push_to_feishu "部署完成..."
echo "======== 部署完成 $(date) ========" >> $LOG_FILE