-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
81 lines (65 loc) · 2.29 KB
/
run.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
#!/bin/bash
# Config
GITHUB_REPO="adclock/server"
JAR_FILE="server.jar"
DELAY=5 # seconds
# Functions
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
# Main
while :
do
# (1) Check if release version was passed as parameter
if [ -z $1 ]; then
RELEASE=""
else
RELEASE="$1"
echo "Using first parameter '$1' as release."
fi
# (2) if not, check against github
if [ -z $RELEASE ]; then
# Checkout latest release
LATEST_RELEASE=$(get_latest_release $GITHUB_REPO)
if [ -z $LATEST_RELEASE ]; then
echo "Release check against github api failed. (Maybe API rate limit exceeded?)"
else
RELEASE=$LATEST_RELEASE
echo "Latest release of $GITHUB_REPO is $RELEASE."
fi
fi
# (3) if github check fails, take latest local version
if [ -z $RELEASE ]; then
LOCAL_RELEASE=$(ls -td */$JAR_FILE | head -n 1 | cut -d'/' -f1)
if [ -z $LOCAL_RELEASE ]; then
echo "No local release folder found. Can't retrive release version. Exiting ..."
exit 1
else
RELEASE=$LOCAL_RELEASE
echo "Using latest local release $RELEASE."
fi
fi
echo "Using release $RELEASE"
if [ ! -d "./$RELEASE" ]; then
echo "No subfolder with name $RELEASE found. Creating new one ..."
mkdir "./$RELEASE"
fi
cd "./$RELEASE" # enter release folder
if [ ! -f "./$JAR_FILE" ]; then
echo "$JAR_FILE not found. Downloading from GitHub $GITHUB_REPO @ $RELEASE ..."
curl --silent -O -J -L "https://github.com/$GITHUB_REPO/releases/download/$RELEASE/$JAR_FILE"
# TODO remove release folder, if downloaded jar file was not found.
fi
# Run JAR_FILE
java -jar $JAR_FILE
rc=$?
echo "$JAR_FILE returned $rc."
if [ $rc -eq 1 ]; then
echo "An error occurred. Waiting $DELAY seconds ..."
sleep $DELAY # seconds
fi
cd .. # leave release folder
echo "Try to restart $JAR_FILE ... "
done