gitea-updater/gitea-updater.sh

65 lines
1.9 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2020-12-13 14:09:28 +01:00
2022-04-18 23:42:24 +02:00
# All executed commands are printed to stdout
set -x
2022-04-18 23:42:24 +02:00
# Redirect stdout (and stderr to stdout) to a file
exec 1>/var/log/updater/gitea-updater/gitea-"$(date +%F)".log 2>&1
2022-04-18 23:42:24 +02:00
# Abort on nonzero exit status
2020-12-13 14:09:28 +01:00
set -o errexit
2022-04-18 23:42:24 +02:00
# Abort on unbound variable
2020-12-13 14:09:28 +01:00
set -o nounset
2022-04-18 23:42:24 +02:00
# Don't hide errors within pipes
2020-12-13 14:09:28 +01:00
set -o pipefail
# set your API key here
2020-12-13 14:09:28 +01:00
key=
# set your chat id here
2020-12-13 14:09:28 +01:00
chat_id=
function send_message() {
if ! [ $# -eq 0 ]; then
2022-04-18 23:42:24 +02:00
curl --silent --show-error --fail --request POST "https://api.telegram.org/$key/sendMessage" --data chat_id="$chat_id" --data text="$1" --output /dev/null
else
echo "No argument supplied, please specify the message to send"
exit 1
2022-04-18 23:42:24 +02:00
fi
2020-12-13 14:09:28 +01:00
}
function update_gitea() {
2022-04-18 23:42:24 +02:00
systemctl stop gitea.service
2020-12-13 14:09:28 +01:00
2022-04-18 23:42:24 +02:00
# Download the latest version
curl --location --output /usr/local/bin/gitea --silent --show-error --fail --request GET "https://dl.gitea.io/gitea/$1/gitea-$1-linux-amd64"
2020-12-13 14:09:28 +01:00
2022-04-18 23:42:24 +02:00
# Set the correct permissions
chmod +x /usr/local/bin/gitea
/sbin/restorecon -v /usr/local/bin/gitea
/sbin/restorecon -v /var/lib/gitea/log/gitea.log
2020-12-13 14:09:28 +01:00
if ! systemctl start gitea.service; then
2022-04-18 23:42:24 +02:00
send_message "[Gitea] - Gitea service did not start correctly. Please log in as soon as possible and see what went wrong"
exit 1
2022-04-18 23:42:24 +02:00
fi
send_message "[Gitea] - Gitea has just been updated"
2020-12-13 14:09:28 +01:00
}
2022-04-18 23:42:24 +02:00
# Retrieve local release
2020-12-13 14:09:28 +01:00
local_release=$(/usr/local/bin/gitea --version | awk '{print $3}')
2022-04-18 23:42:24 +02:00
# Retrieve the latest release
latest_release=$(curl --silent --show-error --fail --request GET "https://api.github.com/repos/go-gitea/gitea/releases/latest" | grep '"tag_name":' | sed --regexp-extended 's/.*"([^"]+)".*/\1/' | cut --characters=2-)
2020-12-13 14:09:28 +01:00
2022-04-18 23:42:24 +02:00
# Compare these two versions
if ! [ "$latest_release" == "$local_release" ]; then
2022-04-18 23:42:24 +02:00
send_message "[Gitea] - Gitea is not up to date (https://github.com/go-gitea/gitea/releases/tag/v$latest_release)"
update_gitea "$latest_release"
2020-12-13 14:09:28 +01:00
fi
send_message "[Gitea] - Gitea is up to date"