forked from northsea4/mdcx-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare-src.sh
executable file
·226 lines (182 loc) · 4.9 KB
/
prepare-src.sh
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
#!/bin/sh
# 脚本说明:下载应用源码并解压到指定的目录(通过`context`指定)下的`.mdcx_src`目录
# 一般只用于构建镜像流程,普通用户可以忽略。
# UPDATE 2023-12-24 17:08:03 使用新的源码仓库:https://github.com/sqzw-x/mdcx
# UPDATE 2024-05-28 21:28:01 sqzw-x/mdcx目前基本只进行daily_release构建
# 检查是否有jq命令
if ! command -v jq &> /dev/null
then
echo "❌ 请先安装jq命令!参考:https://command-not-found.com/jq"
exit 1
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--context)
context="$2"
shift
shift
;;
--verbose)
verbose=1
shift
;;
--dry)
dry=1
shift
;;
-h|--help)
help=1
shift
;;
*)
shift
;;
esac
done
if [[ -z "$context" ]]; then
echo "❌ context is required!"
exit 1
fi
if [[ ! -d "$context" ]]; then
echo "❌ Dir $context is not exist!"
exit 1
fi
cd $context
echo "○ 将从发布仓库下载源码进行构建"
generate_app_version() {
local published_at="$1"
# 去除非数字字符
published_at=$(echo "$published_at" | tr -dc '0-9')
# 取前8位数字作为年月日,前缀为d
echo "d${published_at:0:8}"
}
find_release_by_tag_name() {
local repo=$1
local target_tag_name=$2
local url="https://api.github.com/repos/${repo}/releases"
# echo "URL: $url"
local target_release=""
let found=false
local page=1
while true; do
local response=$(curl -s "${url}?per_page=100&page=${page}")
if [[ -z "$response" ]]; then
break
fi
local releases=$(printf '%s' $response | jq -c '.[]')
for release in $releases; do
tag_name=$(printf '%s' $release | jq -r '.tag_name')
if [[ "$tag_name" == "$target_tag_name" ]]; then
found=true
echo $release
break
fi
done
if [[ $found ]]; then
break
fi
page=$((page + 1))
done
}
# 获取指定仓库和tag_name的release,并解析得到release信息
# 返回json对象:
# {
# "tag_name": "v1.0.0",
# "published_at": "2022-01-01T00:00:00Z",
# "release_version": "120220101",
# "tar_url": "https://api.github.com/repos/sqzw-x/mdcx/tarball/daily_release",
# "zip_url": "https://api.github.com/repos/sqzw-x/mdcx/zipball/daily_release"
# }
get_release_info() {
local repo="$1"
local tag_name="$2"
# echo "⏳ 正在获取仓库 ${repo} 中 tag_name=${tag_name} 的release..."
local release=$(find_release_by_tag_name "$repo" "$tag_name")
if [[ -z "$release" ]]; then
echo "❌ 找不到 tag_name=${tag_name} 的release!"
return 1
fi
tag_name=$(printf '%s' $release | jq -r '.tag_name')
if [[ -z "$tag_name" ]]; then
echo "❌ 找不到 tag_name!"
return 1
fi
published_at=$(printf '%s' $release | jq -r '.published_at')
if [[ -z "$published_at" ]]; then
echo "❌ 找不到 published_at!"
return 1
fi
release_version=$(generate_app_version "$published_at")
tar_url=$(printf '%s' $release | jq -r '.tarball_url')
if [[ -z "$tar_url" ]]; then
echo "❌ 从请求结果获取源码压缩包文件下载链接失败!"
return 1
fi
zip_url=$(printf '%s' $release | jq -r '.zipball_url')
if [[ -z "$zip_url" ]]; then
echo "❌ 从请求结果获取源码压缩包文件下载链接失败!"
return 1
fi
# 构建一个json对象
local data="{
\"tag_name\": \"${tag_name}\",
\"published_at\": \"${published_at}\",
\"release_version\": \"${release_version}\",
\"tar_url\": \"${tar_url}\",
\"zip_url\": \"${zip_url}\"
}"
echo $data
return 0
}
REPO="sqzw-x/mdcx"
TAG_NAME="daily_release"
info=$(get_release_info "$REPO" "$TAG_NAME")
if [[ $? -ne 0 ]]; then
echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!"
exit 1
else
echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!"
fi
echo $info | jq
# exit 0
# 发布时间
published_at=$(printf '%s' $info | jq -r ".published_at")
echo "📅 发布时间: $published_at"
# 版本号
release_version=$(printf '%s' $info | jq -r ".release_version")
echo "🔢 版本号: $release_version"
# 源码链接
file_url=$(printf '%s' $info | jq -r ".tar_url")
echo "🔗 下载链接: $file_url"
if [[ -z "$file_url" ]]; then
echo "❌ 从请求结果获取下载链接失败!"
exit 1
fi
if [[ -n "$dry" ]]; then
exit 0
fi
echo "⏳ 下载文件..."
tar_path="$release_version.tar.gz"
srcDir=".mdcx_src"
if [[ -n "$verbose" ]]; then
curl -o $tar_path $file_url -L
else
curl -so $tar_path $file_url -L
fi
if [[ $? -ne 0 ]]; then
echo "❌ 下载文件失败!"
exit 1
fi
echo "✅ 下载成功"
echo "⏳ 开始解压..."
# 使用tar命令解压
rm -rf $srcDir
mkdir -p $srcDir
tar -zxvf $tar_path -C $srcDir --strip-components 1
rm -f $tar_path
echo "✅ 源码已解压到 $srcDir"
if [ -n "$GITHUB_ACTIONS" ]; then
echo "APP_VERSION=$release_version" >> $GITHUB_OUTPUT
fi