2022-02-06 23:07:52 +03:00
#!/usr/bin/env bash
# This is an update script for gitea installed via the binary distribution
2023-04-23 09:29:03 +03:00
# from dl.gitea.com on linux as systemd service. It performs a backup and updates
2022-02-06 23:07:52 +03:00
# Gitea in place.
# NOTE: This adds the GPG Signing Key of the Gitea maintainers to the keyring.
2022-03-27 20:48:08 +03:00
# Depends on: bash, curl, xz, sha256sum. optionally jq, gpg
2022-02-06 23:07:52 +03:00
# See section below for available environment vars.
# When no version is specified, updates to the latest release.
# Examples:
# upgrade.sh 1.15.10
# giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh
2023-04-25 20:19:58 +03:00
# Check if gitea service is running
if ! pidof gitea & > /dev/null; then
echo "Error: gitea is not running."
exit 1
fi
# Continue with rest of the script if gitea is running
echo "Gitea is running. Continuing with rest of script..."
2022-03-27 20:48:08 +03:00
# apply variables from environment
: " ${ giteabin : = "/usr/local/bin/gitea" } "
: " ${ giteahome : = "/var/lib/gitea" } "
: " ${ giteaconf : = "/etc/gitea/app.ini" } "
: " ${ giteauser : = "git" } "
: " ${ sudocmd : = "sudo" } "
: " ${ arch : = "linux-amd64" } "
: " ${ service_start : = " $sudocmd systemctl start gitea " } "
: " ${ service_stop : = " $sudocmd systemctl stop gitea " } "
: " ${ service_status : = " $sudocmd systemctl status gitea " } "
: " ${ backupopts : = "" } " # see `gitea dump --help` for available options
2022-03-09 01:58:14 +03:00
2022-03-27 20:48:08 +03:00
function giteacmd {
if [ [ $sudocmd = "su" ] ] ; then
2022-04-27 06:30:29 +03:00
# `-c` only accept one string as argument.
" $sudocmd " - " $giteauser " -c " $( printf "%q " " $giteabin " "--config" " $giteaconf " "--work-path" " $giteahome " " $@ " ) "
2022-03-27 20:48:08 +03:00
else
" $sudocmd " --user " $giteauser " " $giteabin " --config " $giteaconf " --work-path " $giteahome " " $@ "
fi
}
2022-03-09 01:58:14 +03:00
function require {
for exe in " $@ " ; do
command -v " $exe " & >/dev/null || ( echo " missing dependency ' $exe ' " ; exit 1)
done
}
2022-03-27 20:48:08 +03:00
# parse command line arguments
while true; do
case " $1 " in
-v | --version ) giteaversion = " $2 " ; shift 2 ; ;
-y | --yes ) no_confirm = "yes" ; shift ; ;
--ignore-gpg) ignore_gpg = "yes" ; shift ; ;
"" | -- ) shift; break ; ;
* ) echo "Usage: [<environment vars>] upgrade.sh [-v <version>] [-y] [--ignore-gpg]" ; exit 1; ;
esac
done
2022-03-09 01:58:14 +03:00
2022-03-27 20:48:08 +03:00
# exit once any command fails. this means that each step should be idempotent!
set -euo pipefail
2022-03-09 01:58:14 +03:00
if [ [ -f /etc/os-release ] ] ; then
os_release = $( cat /etc/os-release)
if [ [ " $os_release " = ~ "OpenWrt" ] ] ; then
sudocmd = "su"
service_start = "/etc/init.d/gitea start"
service_stop = "/etc/init.d/gitea stop"
service_status = "/etc/init.d/gitea status"
else
require systemctl
fi
fi
2022-03-27 20:48:08 +03:00
require curl xz sha256sum " $sudocmd "
2022-02-06 23:07:52 +03:00
# select version to install
2022-03-27 20:48:08 +03:00
if [ [ -z " ${ giteaversion :- } " ] ] ; then
2022-02-06 23:07:52 +03:00
require jq
2023-04-23 09:29:03 +03:00
giteaversion = $( curl --connect-timeout 10 -sL https://dl.gitea.com/gitea/version.json | jq -r .latest.version)
2022-03-27 20:48:08 +03:00
echo " Latest available version is $giteaversion "
2022-02-06 23:07:52 +03:00
fi
# confirm update
2022-03-27 20:48:08 +03:00
echo "Checking currently installed version..."
2022-03-09 01:58:14 +03:00
current = $( giteacmd --version | cut -d ' ' -f 3)
2022-02-06 23:07:52 +03:00
[ [ " $current " = = " $giteaversion " ] ] && echo " $current is already installed, stopping. " && exit 1
2022-03-09 01:58:14 +03:00
if [ [ -z " ${ no_confirm :- } " ] ] ; then
echo "Make sure to read the changelog first: https://github.com/go-gitea/gitea/blob/main/CHANGELOG.md"
echo " Are you ready to update Gitea from ${ current } to ${ giteaversion } ? (y/N) "
read -r confirm
[ [ " $confirm " = = "y" ] ] || [ [ " $confirm " = = "Y" ] ] || exit 1
fi
echo " Upgrading gitea from $current to $giteaversion ... "
2022-02-06 23:07:52 +03:00
pushd " $( pwd ) " & >/dev/null
cd " $giteahome " # needed for gitea dump later
# download new binary
binname = " gitea- ${ giteaversion } - ${ arch } "
2023-04-23 09:29:03 +03:00
binurl = " https://dl.gitea.com/gitea/ ${ giteaversion } / ${ binname } .xz "
2022-02-06 23:07:52 +03:00
echo " Downloading $binurl ... "
curl --connect-timeout 10 --silent --show-error --fail --location -O " $binurl {,.sha256,.asc} "
2022-03-27 20:48:08 +03:00
# validate checksum & gpg signature
2022-03-09 01:58:14 +03:00
sha256sum -c " ${ binname } .xz.sha256 "
if [ [ -z " ${ ignore_gpg :- } " ] ] ; then
2022-03-27 20:48:08 +03:00
require gpg
2022-03-09 01:58:14 +03:00
gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
gpg --verify " ${ binname } .xz.asc " " ${ binname } .xz " || { echo 'Signature does not match' ; exit 1; }
fi
2022-02-06 23:07:52 +03:00
rm " ${ binname } " .xz.{ sha256,asc}
# unpack binary + make executable
2022-03-27 20:48:08 +03:00
xz --decompress --force " ${ binname } .xz "
2022-02-06 23:07:52 +03:00
chown " $giteauser " " $binname "
chmod +x " $binname "
# stop gitea, create backup, replace binary, restart gitea
2022-03-27 20:48:08 +03:00
echo " Flushing gitea queues at $( date) "
2022-02-06 23:07:52 +03:00
giteacmd manager flush-queues
2022-03-27 20:48:08 +03:00
echo " Stopping gitea at $( date) "
2022-03-09 01:58:14 +03:00
$service_stop
2022-02-06 23:07:52 +03:00
echo " Creating backup in $giteahome "
giteacmd dump $backupopts
echo " Updating binary at $giteabin "
2022-03-09 01:58:14 +03:00
cp -f " $giteabin " " $giteabin .bak " && mv -f " $binname " " $giteabin "
$service_start
$service_status
echo " Upgrade to $giteaversion successful! "
2022-02-06 23:07:52 +03:00
popd