@ -1,6 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env bash
set -euo pipefail
# This is an update script for gitea installed via the binary distribution
# This is an update script for gitea installed via the binary distribution
# from dl.gitea.io on linux as systemd service. It performs a backup and updates
# from dl.gitea.io on linux as systemd service. It performs a backup and updates
# Gitea in place.
# Gitea in place.
@ -13,6 +11,42 @@ set -euo pipefail
# upgrade.sh 1.15.10
# upgrade.sh 1.15.10
# giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh
# giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh
while true; do
case " $1 " in
-v | --version ) ver = " $2 " ; shift 2 ; ;
-y | --yes ) no_confirm = "yes" ; shift ; ;
--ignore-gpg) ignore_gpg = "yes" ; shift ; ;
-- ) shift; break ; ;
* ) break ; ;
esac
done
set -euo pipefail
function require {
for exe in " $@ " ; do
command -v " $exe " & >/dev/null || ( echo " missing dependency ' $exe ' " ; exit 1)
done
}
require curl xz sha256sum gpg
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
# apply variables from environment
# apply variables from environment
: " ${ giteabin : = "/usr/local/bin/gitea" } "
: " ${ giteabin : = "/usr/local/bin/gitea" } "
: " ${ giteahome : = "/var/lib/gitea" } "
: " ${ giteahome : = "/var/lib/gitea" } "
@ -20,34 +54,40 @@ set -euo pipefail
: " ${ giteauser : = "git" } "
: " ${ giteauser : = "git" } "
: " ${ sudocmd : = "sudo" } "
: " ${ sudocmd : = "sudo" } "
: " ${ arch : = "linux-amd64" } "
: " ${ 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
: " ${ backupopts : = "" } " # see `gitea dump --help` for available options
function giteacmd {
function giteacmd {
if [ [ $sudocmd = "su" ] ] ; then
" $sudocmd " - " $giteauser " -c " $giteabin " --config " $giteaconf " --work-path " $giteahome " " $@ "
else
" $sudocmd " --user " $giteauser " " $giteabin " --config " $giteaconf " --work-path " $giteahome " " $@ "
" $sudocmd " --user " $giteauser " " $giteabin " --config " $giteaconf " --work-path " $giteahome " " $@ "
fi
}
}
function require {
for exe in " $@ " ; do
command -v " $exe " & >/dev/null || ( echo " missing dependency ' $exe ' " ; exit 1)
done
}
require systemctl curl xz sha256sum gpg " $sudocmd "
# select version to install
# select version to install
if [ [ -z " ${ 1 :- } " ] ] ; then
if [ [ -z " ${ ver :- } " ] ] ; then
require jq
require jq
giteaversion = $( curl --connect-timeout 10 -sL https://dl.gitea.io/gitea/version.json | jq -r .latest.version)
giteaversion = $( curl --connect-timeout 10 -sL https://dl.gitea.io/gitea/version.json | jq -r .latest.version)
else
else
giteaversion = " $1 "
giteaversion = " $ver "
fi
fi
# confirm update
# confirm update
current = $( giteacmd --version | cut -- delimiter= ' ' -- fields= 3)
current = $( giteacmd --version | cut -d ' ' -f 3)
[ [ " $current " = = " $giteaversion " ] ] && echo " $current is already installed, stopping. " && exit 1
[ [ " $current " = = " $giteaversion " ] ] && echo " $current is already installed, stopping. " && exit 1
if [ [ -z " ${ no_confirm :- } " ] ] ; then
echo "Make sure to read the changelog first: https://github.com/go-gitea/gitea/blob/main/CHANGELOG.md"
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) "
echo " Are you ready to update Gitea from ${ current } to ${ giteaversion } ? (y/N) "
read -r confirm
read -r confirm
[ [ " $confirm " = = "y" ] ] || [ [ " $confirm " = = "Y" ] ] || exit 1
[ [ " $confirm " = = "y" ] ] || [ [ " $confirm " = = "Y" ] ] || exit 1
fi
echo " Upgrading gitea from $current to $giteaversion ... "
pushd " $( pwd ) " & >/dev/null
pushd " $( pwd ) " & >/dev/null
cd " $giteahome " # needed for gitea dump later
cd " $giteahome " # needed for gitea dump later
@ -59,9 +99,11 @@ echo "Downloading $binurl..."
curl --connect-timeout 10 --silent --show-error --fail --location -O " $binurl {,.sha256,.asc} "
curl --connect-timeout 10 --silent --show-error --fail --location -O " $binurl {,.sha256,.asc} "
# validate checksum & gpg signature (exit script if error)
# validate checksum & gpg signature (exit script if error)
sha256sum --check " ${ binname } .xz.sha256 "
sha256sum -c " ${ binname } .xz.sha256 "
if [ [ -z " ${ ignore_gpg :- } " ] ] ; then
gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
gpg --verify " ${ binname } .xz.asc " " ${ binname } .xz " || { echo 'Signature does not match' ; exit 1; }
gpg --verify " ${ binname } .xz.asc " " ${ binname } .xz " || { echo 'Signature does not match' ; exit 1; }
fi
rm " ${ binname } " .xz.{ sha256,asc}
rm " ${ binname } " .xz.{ sha256,asc}
# unpack binary + make executable
# unpack binary + make executable
@ -72,12 +114,14 @@ chmod +x "$binname"
# stop gitea, create backup, replace binary, restart gitea
# stop gitea, create backup, replace binary, restart gitea
echo " Stopping gitea at $( date) "
echo " Stopping gitea at $( date) "
giteacmd manager flush-queues
giteacmd manager flush-queues
$sudocmd systemctl stop gitea
$service_stop
echo " Creating backup in $giteahome "
echo " Creating backup in $giteahome "
giteacmd dump $backupopts
giteacmd dump $backupopts
echo " Updating binary at $giteabin "
echo " Updating binary at $giteabin "
mv --force --backup " $binname " " $giteabin "
cp -f " $giteabin " " $giteabin .bak " && mv -f " $binname " " $giteabin "
$sudocmd systemctl start gitea
$service_start
$sudocmd systemctl status gitea
$service_status
echo " Upgrade to $giteaversion successful! "
popd
popd