forked from cdnjs/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot
executable file
·128 lines (114 loc) · 3.72 KB
/
bot
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
#!/usr/bin/env bash
if [ -z "$BOT_LOG_FILE" ]; then
echo "BOT_LOG_FILE missing"
exit 1
fi
if [ -z "$BOT_BASE_PATH" ]; then
echo "BOT_BASE_PATH missing"
exit 1
fi
if [ -z "$BOT_CDNJS_CHECK_DIR" ]; then
echo "BOT_CDNJS_CHECK_DIR missing"
exit 1
fi
if [ -z "$BOT_CDNJS_NPM_TEMP" ]; then
echo "BOT_CDNJS_NPM_TEMP missing"
exit 1
fi
NVM_DIR="$HOME/.nvm"
if [ -s "$NVM_DIR/nvm.sh" ] && ! command -v nvm &> /dev/null; then
. "$NVM_DIR/nvm.sh" --no-use
fi
if command -v nvm &> /dev/null; then
nvm install 10
echo "nvm version: $(nvm --version)"
fi
echo "git($(command -v git)) version: $(git --version | awk '{print $3}')"
echo "npm($(command -v npm)) version: $(npm --version)"
echo "node($(command -v node)) version: $(node --version)"
echo "BOT_LOG_FILE: $BOT_LOG_FILE"
echo "BOT_BASE_PATH: $BOT_BASE_PATH"
echo "BOT_CDNJS_CHECK_DIR: $BOT_CDNJS_CHECK_DIR"
echo "BOT_CDNJS_NPM_TEMP: $BOT_CDNJS_NPM_TEMP"
if [ "$1" = "debug" ]; then
set -x
export GIT_TRACE=true
fi
. "${BOT_BASE_PATH}/script/commonScript.sh" || exit
function log() {
echo "$@" >> "${BOT_LOG_FILE}"
}
function print-log() {
log "$@"
echo "$@"
}
if ! type cowsay &> /dev/null; then
alias cowsay="echo"
fi
while true; do
# Prevent the bot to run when another bot is already running
cdnjsBot2Status=$(systemctl is-active cdnjs-bot2)
if [ "$cdnjsBot2Status" == "active" ]; then
echo "cdnjs-bot2 is running; waiting"
sleep 60
continue
fi
touch $BOT_BASE_PATH/cdnjs-bot.lock
cowsay "Hi, I'm cdnjs bot"
StartTimestamp="$(date +%s)"
print-log "Start time: $(date)"
cd "${BOT_BASE_PATH}/cdnjs"
CurrentCommit="$(git log -1 --oneline)"
if [ "$(git branch | command grep ^* | awk '{print $2}')" != "master" ]; then
git checkout -f master
fi
git diff --quiet || (autoadd)
run git fetch origin master
if [ "$(git log --oneline -1 origin/master)" != "$(git log --oneline -1 master)" ]; then
run git rebase origin/master master
fi
run timelimit -q -t 180 -T 200 npm install --no-audit --prefix "${BOT_BASE_PATH}/autoupdate" && run timelimit -q -t 1200 -T 1230 "${BOT_BASE_PATH}/autoupdate/autoupdate.js" &
run timelimit -q -t 180 -T 200 npm install --no-audit && run timelimit -q -t 1200 -T 1230 node --max-old-space-size=8192 ./auto-update.js run &
wait
(autoadd)
if [ "${CurrentCommit}" = "$(git log -1 --oneline)" ]; then
print-log "No update detected, bypass pull & push operation"
else
print-log "Update detected, now run pull & push operation"
PUSHED=false
OLD_MASTER="$(git log --oneline -1 origin/master)"
run git fetch origin master
NEW_MASTER="$(git log --oneline -1 origin/master)"
if [ "${NEW_MASTER}" != "${OLD_MASTER}" ]; then
print-log "old :${OLD_MASTER}"
print-log "new :${NEW_MASTER}"
print-log "Remote master branch update detected! Update local branch!"
run git rebase origin/master master
fi
run git push --tags origin master && PUSHED=true
if [ ${PUSHED} = false ]; then
run git pull --rebase origin master
run git push --tags origin master && PUSHED=true
fi
if [ ${PUSHED} = true ]; then
run git push --tags gitlab master --no-verify
fi
run git status
fi
print-log "Clean up ..."
rm -rf $BOT_CDNJS_CHECK_DIR $BOT_CDNJS_NPM_TEMP
EndTimestamp="$(date +%s)"
spent="$((EndTimestamp - StartTimestamp))"
echo -e "\\nTotal time spent for this build is _${spent}_ second(s)\\n"
print-log "End time: $(date)"
print-log "Spent ${spent} second ..."
sleepSecs=$((480 - spent))
while [[ "${sleepSecs}" -le 60 ]]; do
sleepSecs=$((480 + sleepSecs))
done
if [ "$(date +%a)" = "Sun" ] || [ "$(date +%a)" = "Sat" ]; then
sleepSecs=$((300 + sleepSecs))
fi
rm $BOT_BASE_PATH/cdnjs-bot.lock
run sleep "${sleepSecs}"
done