build.sh
7.44 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
# 功能说明:把jar包打成tar.gz包并上传到阿里云存储
# 远程打包的配置
PASSWORD="vion2025"
# 定义模块配置:目录名->webhook URL
declare -A MODULE_CONFIG
MODULE_CONFIG["matchPerson"]="http://192.168.1.64:9505/api/apis/deploy/26/branch/"
#MODULE_CONFIG["VVAS-DataCenter-process"]="http://192.168.1.64:9505/api/apis/deploy/9/branch/"
#MODULE_CONFIG["VVAS-DataCenter-dbSave"]="http://192.168.1.64:9505/api/apis/deploy/10/branch/"
# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 打印带颜色的文本
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 显示菜单
show_menu() {
echo "=================================="
echo "本工具使用当前目录git的分支及已提交的最新commitId来打包!"
echo "如果需要根据历史版本打包,请访问界面手动选择commitID打包: http://192.168.1.64:9505/deploy/request "
echo "请选择打包方式:"
echo "1) 远程打包(默认),使用192.168.1.64服务器打包,速度更快"
echo "2) 本地打包"
echo "=================================="
read -p "请输入选项 (1 或 2, 默认为 1): " choice
choice=${choice:-1}
}
# 获取当前分支名
get_current_branch() {
git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown"
}
# 获取最新提交ID
get_commit_id() {
git rev-parse HEAD 2>/dev/null | cut -c1-8 || echo "unknown"
}
# 获取模块列表
get_module_list() {
local modules=()
for module in "${!MODULE_CONFIG[@]}"; do
modules+=("$module")
done
echo "${modules[@]}"
}
# 获取 Git 信息
BRANCH_NAME=$(get_current_branch)
COMMIT_ID=$(get_commit_id)
# 本地打包指定模块
local_build_module() {
local module_dir=$1
local webhook_url=${MODULE_CONFIG[$module_dir]}
# 检查模块是否在配置中
if [ -z "$webhook_url" ]; then
print_error "未知模块: $module_dir"
return 1
fi
print_info "开始本地打包模块: $module_dir"
# 切换到模块目录
#local module_path="$SCRIPT_DIR$module_dir"
local module_path="$(dirname "$SCRIPT_DIR")"
if [ ! -d "$module_path" ]; then
print_error "模块目录不存在: $module_path"
return 1
fi
cd "$module_path" || return 1
print_info "当前目录: $(pwd)"
# 复制 ../build/bin 目录到当前目录
print_info "复制 ../build/bin 目录到当前目录..."
if [ -d "../build/bin" ]; then
cp -r ../build/bin ./build/
print_info "复制完成"
else
print_error "../build/bin 目录不存在"
cd "$SCRIPT_DIR" || return 1
return 1
fi
if [ -f "../build/package.sh" ]; then
print_info "执行 ../build/package.sh..."
chmod +x ../build/package.sh
../build/package.sh
if [ $? -eq 0 ]; then
print_info "模块 $module_dir 本地打包完成"
rm -fr build/bin
else
print_error "模块 $module_dir 本地打包失败"+
rm -fr build/bin
cd "$SCRIPT_DIR" || return 1
return 1
fi
else
print_error "../build/package.sh 文件不存在"
rm -fr build/bin
cd "$SCRIPT_DIR" || return 1
return 1
fi
# 返回脚本所在目录
cd "$SCRIPT_DIR" || return 1
}
# 远程打包指定模块
remote_build_module() {
local module_dir=$1
local webhook_url=${MODULE_CONFIG[$module_dir]}
# 检查模块是否在配置中
if [ -z "$webhook_url" ]; then
print_error "未知模块: $module_dir"
return 1
fi
print_info "开始远程打包模块: $module_dir"
# 切换到模块目录
#local module_path="$SCRIPT_DIR/$module_dir"
local module_path="$(dirname "$SCRIPT_DIR")"
if [ ! -d "$module_path" ]; then
print_error "模块目录不存在: $module_path"
return 1
fi
cd "$module_path" || return 1
print_info "当前目录: $(pwd)"
# Webhook URL 和参数
WEBHOOK_URL="${webhook_url}?name=${BRANCH_NAME}&token=${PASSWORD}"
print_info "当前分支: $BRANCH_NAME"
print_info "提交ID: $COMMIT_ID"
# 模拟 GitLab webhook 数据
TIMESTAMP=$(date -Iseconds)
PAYLOAD=$(cat <<EOF
{
"object_kind": "push",
"ref": "refs/heads/$BRANCH_NAME",
"before": "",
"after": "$COMMIT_ID",
"repository": {
"name": "$module_dir",
"url": "",
"description": "",
"homepage": "",
"git_http_url": "",
"git_ssh_url": "",
"visibility_level": 0
},
"commits": [
{
"id": "$COMMIT_ID",
"message": "autoBuild",
"timestamp": "$TIMESTAMP",
"url": "",
"author": {
"name": "build-script",
"email": "build@script.local"
}
}
],
"total_commits_count": 1
}
EOF
)
# 发送请求
print_info "调用 webhook: $WEBHOOK_URL"
RESPONSE=$(curl -s -w "%{http_code}" -X POST \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"$WEBHOOK_URL")
HTTP_CODE=${RESPONSE: -3}
if [ "$HTTP_CODE" -eq 202 ]; then
print_info "模块 $module_dir 远程打包触发成功,正在打包,请稍等,预计30秒后打包成功!"
elif [ "$HTTP_CODE" -eq 200 ] ; then
print_warn "响应内容: ${RESPONSE%???}"
print_warn "模块 $module_dir 打包返回内容,可能有异常: $HTTP_CODE"
else
print_error "模块 $module_dir 远程打包触发失败,HTTP状态码: $HTTP_CODE"
print_error "响应内容: ${RESPONSE%???}"
cd "$SCRIPT_DIR" || return 1
return 1
fi
print_info "模块 $module_dir 打包上传成功!";
# 返回脚本所在目录
cd "$SCRIPT_DIR" || return 1
}
# 本地打包所有模块
local_build() {
print_info "开始本地打包所有模块..."
# 获取模块列表
local modules
read -ra modules <<< "$(get_module_list)"
for module in "${modules[@]}"; do
if ! local_build_module "$module"; then
print_error "模块 $module 本地打包失败"
return 1
fi
echo ""
done
print_info "所有模块打包上传成功,可访问地址进行部署发布: xxxx";
print_info "部署版本号:${BRANCH_NAME}-${COMMIT_ID:0:6}"
}
# 远程打包所有模块
remote_build() {
print_info "开始远程打包所有模块..."
# 获取模块列表
local modules
read -ra modules <<< "$(get_module_list)"
for module in "${modules[@]}"; do
if ! remote_build_module "$module"; then
print_error "模块 $module 远程打包失败"
return 1
fi
echo ""
done
print_info "所有模块正在打包,预计30秒后打包成功,可访问地址进行部署发布: xxxx";
print_info "查看打包进度:http://192.168.1.64:9505/deploy/request";
print_info "部署版本号:${BRANCH_NAME}-${COMMIT_ID:0:6}"
}
# 主程序
main() {
print_info "构建脚本,目录${SCRIPT_DIR}"
show_menu
case $choice in
1)
remote_build
;;
2)
local_build
;;
*)
print_warn "无效选项,使用默认远程打包"
remote_build
;;
esac
}
# 执行主程序
main "$@"