gitea-updater/gitea-updater

63 lines
1.9 KiB
Bash

#! /usr/bin/env bash
# All executed commands are printed to stdout
set -x
# Redirect stdout (and stderr to stdout) to a file
exec 1>/var/log/updater/gitea-updater/gitea-"$(date +%F)".log 2>&1
# Abort on nonzero exit status
set -o errexit
# Abort on unbound variable
set -o nounset
# Don't hide errors within pipes
set -o pipefail
# Set your API key here
key=
# Set your chat id here
chat_id=
function send_message() {
if [ $# -eq 0 ]; then
echo "No argument supplied, please specify the message to send"
else
curl --silent --show-error --fail --request POST "https://api.telegram.org/$key/sendMessage" --data chat_id="$chat_id" --data text="$1" --output /dev/null
fi
}
function update_gitea() {
systemctl stop gitea.service
# 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"
# Set the correct permissions
/usr/bin/chmod +x /usr/local/bin/gitea
/usr/sbin/restorecon /usr/local/bin/gitea
/usr/sbin/restorecon /var/lib/gitea/log/gitea.log
if systemctl start gitea.service; then
send_message "[Gitea] - Gitea has just been updated"
else
send_message "[Gitea] - Gitea service did not start correctly. Please log in as soon as possible and see what went wrong"
fi
}
# Retrieve local release
local_release=$(/usr/local/bin/gitea --version | awk '{print $3}')
# 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-)
# Compare these two versions
if [ "$latest_release" == "$local_release" ]; then
send_message "[Gitea] - Gitea is up to date"
else
send_message "[Gitea] - Gitea is not up to date (https://github.com/go-gitea/gitea/releases/tag/v$latest_release)"
update_gitea "$latest_release"
fi