This repository has been archived by the owner on Jun 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
update-website.sh
executable file
Β·291 lines (249 loc) Β· 8.71 KB
/
update-website.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
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
284
285
286
287
288
289
290
291
#!/usr/bin/env bash
trap "exit 1" EXIT
function init() {
pth="$(setBasePath)"
. "$pth/config.sh"
updateMeta="$forceUpdateMeta"
updateRepo="$forceUpdateRepo"
export PATH="$path:$PATH"
NVM_DIR=$HOME/.nvm
if [ -s "$NVM_DIR/nvm.sh" ]; then
. "$NVM_DIR/nvm.sh" --no-use
nvm install 10
fi
if [[ ! $timeout =~ ^[0-9]+$ ]] || [[ $timeout -le 3 ]]; then
timeout=3
fi
eval logPath="$logPath"
if [ -z "$logPath" ] || [ ! -d "$logPath" ] || [ ! -w "$logPath" ]; then
logPath="$pth"
fi
[ "$logMode" = "clean" ] && rm -f "$logPath/$logFile"
. "$pth/colorEcho/dist/ColorEcho.bash" &> /dev/null || {
git -C "$pth" submodule update --init
. "$pth/colorEcho/dist/ColorEcho.bash"
}
}
function setBasePath() {
dirname "$(realpath "${BASH_SOURCE[0]}")"
}
function git-checkout-master-if-needed() {
currentBranch="$(git branch | awk '{ if ("*" == $1) print $2}')"
[ "$currentBranch" = "master" ] || run_retry git checkout master
}
function git-reset-hard-if-needed() {
# the bot already keeps the cdnjs repo up-to-date
echo ignore
# if ! git diff --quiet; then
# output Info "Repo diff found, so reset!"
# run_retry git reset --hard
# else
# output Info "Repo diff not found, so do not reset!"
# fi
}
function output() {
echo "$(date) [$1] $2" >> "$logPath/$logFile"
case "$1" in
"Warn")
echo.Red "$2"
;;
"Success")
echo.Green "$2"
;;
"Info")
echo.Cyan "$2"
;;
*)
echo "$1"
;;
esac
if [ -n "$3" ] && [ "$3" = "chat-room" ]; then
curl --silent -d message="[cronjob] $2" "$gitterHook" > /dev/null || output Warn "Error on curl!!! Message may not be posted on our gitter chatroom"
curl --silent -X POST --data-urlencode 'payload={"channel": "#'"$slackChannel"'", "username": "buildScript", "text": "'"$2"'", "icon_emoji": ":building_construction:"}' "$slackHook" > /dev/null || output Warn "Error on curl!!! Message may not be posted on our Slack chatroom"
fi
}
function error() {
local MSG
if [ "$#" = "0" ]; then
MSG="Error"
else
MSG="$*"
fi
output Warn "$MSG, pwd='$(pwd)'" chat-room
exit 1
}
function run() {
run_retry_times 1 "$@"
}
function run_retry() {
run_retry_times "${retryTimes}" "$@"
}
function run_retry_times() {
local timesLeft="${1}"
local timesLeftOrigin="${1}"
shift
while [ "$timesLeft" -ge 1 ]; do
timesLeft="$((timesLeft - 1))"
if [ $((timesLeftOrigin - timesLeft)) -gt 1 ]; then
sleep 3
echo "$(date) [command (try times: $((timesLeftOrigin - timesLeft))/${timesLeftOrigin})] $*" >> "$logPath/$logFile"
else
echo "$(date) [command] $*" >> "$logPath/$logFile"
fi
local isBuiltIn=false
local cmd
cmd="$(echo "$1" | awk '{print $1}')"
if type "$cmd" &> /dev/null; then
local temp
temp="$(type "$cmd" | head -n 1)"
[ "$temp" = "$cmd is a shell builtin" ] && isBuiltIn=true
else
error "'$cmd' command not found!"
fi
if [ "$isBuiltIn" = "false" ]; then
nice -n "$nice" timelimit -q -s 9 -t $((timeout - 2)) -T "$timeout" "$@" || continue
else
"$@" || continue
fi
return
done
error "Got error while running command: '$*'"
}
function build() {
[[ -d "$basePath/$mainRepo" ]] || error "Main repo '$basePath/$mainRepo' not found, exit now."
[[ -d "$basePath/$webRepo" ]] || error "website repo '$basePath/$webRepo' not found, exit now."
output Info "Start date time: $(date)"
output Info "Start website/api/index building process on $serverOwner's server ..." chat-room
output Info "PATH=$PATH"
output Info "bash path: $(type bash)"
output Info "bash version: $BASH_VERSION"
[[ -z "$NVM_BIN" ]] || output Info "nvm version: $(nvm --version)"
output Info "nodejs path: $(type node)"
output Info "nodejs version: $(node --version)"
output Info "npm path: $(type npm)"
output Info "npm version: $(npm --version)"
output Info "git path: $(type git)"
output Info "git version: $(git --version)"
output Info "Reset repository to prevent unstaged changes break the build"
run cd "$basePath/$mainRepo"
git-reset-hard-if-needed
# if [ "$hasLocalRepo" = true ] && [ -d "$basePath" ]; then
# output Success "Exist cdnjs local repo, fetch objects from local branch first"
# run_retry git fetch local
# else
# output Info "Local repo not found, will grab object(s) from GitHub"
# fi
git-checkout-master-if-needed
updated=true
# updated=false
# output Info "Pull cdnjs main repo with rebase from origin(GitHub)"
# run_retry git fetch origin master --tags
# if [ "$(run git log --oneline -1 origin/master)" != "$(run git log --oneline -1 master)" ]; then
# run_retry git rebase origin/master master
# updated=true
# fi
output Info "Current commit: $(run git log --pretty='format:%h - %s - %an %ai' -1)"
if [ "$updated" = false ] && [ "$updateMeta" = false ]; then
msg="Cdnjs main repo is up to date, no need to rebuild"
output Info "$msg" chat-room
else
msg="Cdnjs main repo updates found! Start the rebuild rebuild process"
output Info "$msg" chat-room
msg="Make sure npm package dependencies, do npm install && npm update"
output Info "$msg"
run_retry npm install --no-audit --no-save
run_retry npm update --no-save
msg="Run npm test before building the meta data/artifacts"
output Info "$msg"
run npm test -- --silent
msg="Reset and checkout website repository to meta branch"
output Info "$msg"
(run cd "$basePath/$webRepo" && git-reset-hard-if-needed)
run_retry git -C "$basePath/$webRepo" checkout meta
msg="Rebuild meta data phase 1"
output Info "$msg" chat-room
run_retry node build/packages.json.js
msg="Rebuild meta data phase 2"
output Info "$msg" chat-room
run cd "$basePath/$webRepo"
msg="Make sure npm package dependencies, do npm install & npm update"
output Info "$msg" chat-room
run_retry npm install --no-audit --no-save
run_retry npm update --no-save
run_retry node update.js
msg="Upload packages.min.json to Google storage"
output Info "$msg"
run_retry packages set < public/packages.min.json
msg="Commit meta data update in website repo"
output Info "$msg" chat-room
for file in atom.xml rss.xml sitemap.xml; do
run_retry git add public/$file
done
run_retry git add sri
run_retry git commit --message="meta data"
updateMeta=true
fi
output Info "Change directory into website repo"
run cd "$basePath/$webRepo"
output Info "Reset repository to prevent unstaged changes break the build"
git-reset-hard-if-needed
git-checkout-master-if-needed
output Info "Pull website repo with rebase from origin(Repo)"
webstatus="$(run_retry git pull --tags --rebase origin master | tail -n 1)"
output Info "Current commit: $(run git log --pretty='format:%h - %s - %an %ai' -1)"
if [ "$webstatus" = "Current branch master is up to date." ] || [ "$webstatus" = "Already up-to-date." ]; then
msg="Cdnjs website repo is up to date"
$updateMeta || msg="$msg too, no need to deploy."
$updateMeta && msg="$msg, but we'll still deploy artifacts since main repo has updates."
output Info "$msg" chat-room
else
msg="Cdnjs website repo updates found!"
output Info "$msg" chat-room
updateRepo=true
fi
if [ "$updateRepo" = true ]; then
output Info "Update/Initial submodule under website repo" chat-room
run_retry git submodule update --init
msg="Make sure npm package dependencies, do npm install & npm update"
output Info "$msg" chat-room
run_retry npm install --no-audit --no-save
run_retry npm update --no-save
fi
msg="Rebase website's meta branch on master"
output Info "$msg"
webstatus="$(run_retry git rebase master meta)"
if [[ "$webstatus" != "Current branch meta is up to date." ]] && [[ "$webstatus" != "Already up-to-date." ]]; then
updateRepo=true
fi
if [ "$updateMeta" = true ]; then
msg="Now push and deploy website & api"
output Info "$msg" chat-room
for remote in heroku-web heroku-api; do
run_retry git push "$remote" meta:master -f &
sleep 3
done
[ "$pushMetaOnGitHub" = true ] && run_retry git push origin meta -f &
wait
elif [ "$updateRepo" = true ]; then
msg="Now push and deploy website and api"
output Info "$msg" chat-room
for remote in heroku-web heroku-api; do
run_retry git push "$remote" meta:master -f &
sleep 3
done
[ "$pushMetaOnGitHub" = true ] && run_retry git push origin meta -f &
wait
else
msg="Didn't update anything, no need to push or deploy."
output Info "$msg" chat-room
fi
msg="Update finished."
output Success "$msg" chat-room
output Info "End date time: $(date)"
}
if [ "$1" = "build" ]; then
set +e
init
build
fi
trap - EXIT