-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitea-update.sh
executable file
·117 lines (107 loc) · 3.75 KB
/
gitea-update.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
#!/bin/sh
# A shell script to automatically update Gitea
# Depends only on basic shell utilities (curl, cut, find, grep, sed)
# Assumes use of systemd for Gitea start/stop
DIR=/usr/local/bin/gitea # Set location of gitea binary on local system
URL=https://github.com/go-gitea/gitea/releases/download
ARCH=linux-amd64 # Set architecture type:
# darwin-10.6.386 darwin-10.6-amd64 linux-386
# linux-arm-5,6,7,arm64,mips,mips64,mips64le
USER=root # User for file permissions on Gitea binary
GROUP=git # Group for file permissions on Gitea binary
INIT_TYPE=systemd # Specify init script type (only systemd now)
PRUNE=1 # If TRUE, script will delete older versions
RC=0 # If TRUE, script will download Release Candidates
DEBUG=1 # If TRUE, debug messages are printed to STDOUT
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
cut -c 2- # Remove the leading "v"
# Usage
# $ get_latest_release "creationix/nvm"
# 0.31.4
# Adapted from:
# https://gist.github.com/lukechilds/a83e1d7127b78fef38c2914c4ececc3c
}
get_current_version() {
eval $1 -v | cut -d " " -f 3
}
# Set variable #new_ver by checking release status from GitHub
NEW_VER=$(get_latest_release "go-gitea/gitea")
if [ $DEBUG -eq 1 ]; then
echo "New Version: $NEW_VER"
fi
# Check if new version is a Release Candidate (contains "-rc")
case $NEW_VER in *-rc*)
if [ $RC -eq 0 ]; then
if [ $DEBUG -eq 1 ]; then
echo "New Version is Release Candidate, quitting"
fi
exit 0
fi
esac
# Check if gitea binary exists at specified $FILE
if test -f "$DIR/gitea"; then
if [ $DEBUG -eq 1 ]; then
echo "$DIR/gitea exists"
fi
else
echo "ERROR: $DIR/gitea does not exist"
exit 0
fi
# Check current version
CUR_VER=$(get_current_version $DIR/gitea)
if [ $DEBUG -eq 1 ]; then
echo "Current Version: $CUR_VER"
fi
if [ $NEW_VER != $CUR_VER ]; then
if [ $DEBUG -eq 1 ]; then
echo "There is a newer release available, downloading..."
fi
# Download the latest version of Gitea binary
#wget -N https://github.com/go-gitea/gitea/releases/download/v$NEW_VER/gitea-$NEW_VER-$ARCH -P $DIR/bin/
( cd $DIR/bin && curl -s -O -L $URL/v$NEW_VER/gitea-$NEW_VER-$ARCH )
# Verify the checksum of the latest Gitea binary
SHA_CHECK=$(cd $DIR/bin && curl -s -L $URL/v$NEW_VER/gitea-$NEW_VER-$ARCH.sha256 | sha256sum -c | cut -d " " -f 2)
if [ $SHA_CHECK = "OK" ]; then
if [ $DEBUG -eq 1 ]; then
echo "SHA256 verified"
fi
else
echo "ERROR: SHA256 check failed"
exit 0
fi
# Set USER/GROUP ownership for new Gitea binary
chown $USER:$GROUP $DIR/bin/gitea-$NEW_VER-$ARCH
# Set permissions for new Gitea binary (rwxr-x---)
chmod 0750 $DIR/bin/gitea-$NEW_VER-$ARCH
# Stop the Gitea service
case $INIT_TYPE in
systemd)
service gitea stop
;;
*)
esac
# Update the symlink at $DIR/gitea to pint to latest Gitea binary
ln -sf $DIR/bin/gitea-$NEW_VER-$ARCH $DIR/gitea
# Start the Gitea service
case $INIT_TYPE in
systemd)
service gitea start
;;
*)
esac
if [ $PRUNE -eq 1 ]; then
find $DIR/bin/ -maxdepth 1 -type f ! -newer $DIR/bin/gitea-$CUR_VER-$ARCH ! \
-wholename $DIR/bin/gitea-$CUR_VER-$ARCH -delete
fi
if [ $DEBUG -eq 1 ]; then
echo "Gitea upgraded to v$NEW_VER"
fi
else
if [ $DEBUG -eq 1 ]; then
echo "The latest version is already installed"
exit 1
fi
fi