You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upgrade.sh 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env bash
  2. # This is an update script for gitea installed via the binary distribution
  3. # from dl.gitea.com on linux as systemd service. It performs a backup and updates
  4. # Gitea in place.
  5. # NOTE: This adds the GPG Signing Key of the Gitea maintainers to the keyring.
  6. # Depends on: bash, curl, xz, sha256sum. optionally jq, gpg
  7. # See section below for available environment vars.
  8. # When no version is specified, updates to the latest release.
  9. # Examples:
  10. # upgrade.sh 1.15.10
  11. # giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh
  12. # Check if gitea service is running
  13. if ! pidof gitea &> /dev/null; then
  14. echo "Error: gitea is not running."
  15. exit 1
  16. fi
  17. # Continue with rest of the script if gitea is running
  18. echo "Gitea is running. Continuing with rest of script..."
  19. # apply variables from environment
  20. : "${giteabin:="/usr/local/bin/gitea"}"
  21. : "${giteahome:="/var/lib/gitea"}"
  22. : "${giteaconf:="/etc/gitea/app.ini"}"
  23. : "${giteauser:="git"}"
  24. : "${sudocmd:="sudo"}"
  25. : "${arch:="linux-amd64"}"
  26. : "${service_start:="$sudocmd systemctl start gitea"}"
  27. : "${service_stop:="$sudocmd systemctl stop gitea"}"
  28. : "${service_status:="$sudocmd systemctl status gitea"}"
  29. : "${backupopts:=""}" # see `gitea dump --help` for available options
  30. function giteacmd {
  31. if [[ $sudocmd = "su" ]]; then
  32. # `-c` only accept one string as argument.
  33. "$sudocmd" - "$giteauser" -c "$(printf "%q " "$giteabin" "--config" "$giteaconf" "--work-path" "$giteahome" "$@")"
  34. else
  35. "$sudocmd" --user "$giteauser" "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
  36. fi
  37. }
  38. function require {
  39. for exe in "$@"; do
  40. command -v "$exe" &>/dev/null || (echo "missing dependency '$exe'"; exit 1)
  41. done
  42. }
  43. # parse command line arguments
  44. while true; do
  45. case "$1" in
  46. -v | --version ) giteaversion="$2"; shift 2 ;;
  47. -y | --yes ) no_confirm="yes"; shift ;;
  48. --ignore-gpg) ignore_gpg="yes"; shift ;;
  49. "" | -- ) shift; break ;;
  50. * ) echo "Usage: [<environment vars>] upgrade.sh [-v <version>] [-y] [--ignore-gpg]"; exit 1;;
  51. esac
  52. done
  53. # exit once any command fails. this means that each step should be idempotent!
  54. set -euo pipefail
  55. if [[ -f /etc/os-release ]]; then
  56. os_release=$(cat /etc/os-release)
  57. if [[ "$os_release" =~ "OpenWrt" ]]; then
  58. sudocmd="su"
  59. service_start="/etc/init.d/gitea start"
  60. service_stop="/etc/init.d/gitea stop"
  61. service_status="/etc/init.d/gitea status"
  62. else
  63. require systemctl
  64. fi
  65. fi
  66. require curl xz sha256sum "$sudocmd"
  67. # select version to install
  68. if [[ -z "${giteaversion:-}" ]]; then
  69. require jq
  70. giteaversion=$(curl --connect-timeout 10 -sL https://dl.gitea.com/gitea/version.json | jq -r .latest.version)
  71. echo "Latest available version is $giteaversion"
  72. fi
  73. # confirm update
  74. echo "Checking currently installed version..."
  75. current=$(giteacmd --version | cut -d ' ' -f 3)
  76. [[ "$current" == "$giteaversion" ]] && echo "$current is already installed, stopping." && exit 1
  77. if [[ -z "${no_confirm:-}" ]]; then
  78. echo "Make sure to read the changelog first: https://github.com/go-gitea/gitea/blob/main/CHANGELOG.md"
  79. echo "Are you ready to update Gitea from ${current} to ${giteaversion}? (y/N)"
  80. read -r confirm
  81. [[ "$confirm" == "y" ]] || [[ "$confirm" == "Y" ]] || exit 1
  82. fi
  83. echo "Upgrading gitea from $current to $giteaversion ..."
  84. pushd "$(pwd)" &>/dev/null
  85. cd "$giteahome" # needed for gitea dump later
  86. # download new binary
  87. binname="gitea-${giteaversion}-${arch}"
  88. binurl="https://dl.gitea.com/gitea/${giteaversion}/${binname}.xz"
  89. echo "Downloading $binurl..."
  90. curl --connect-timeout 10 --silent --show-error --fail --location -O "$binurl{,.sha256,.asc}"
  91. # validate checksum & gpg signature
  92. sha256sum -c "${binname}.xz.sha256"
  93. if [[ -z "${ignore_gpg:-}" ]]; then
  94. require gpg
  95. gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
  96. gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; }
  97. fi
  98. rm "${binname}".xz.{sha256,asc}
  99. # unpack binary + make executable
  100. xz --decompress --force "${binname}.xz"
  101. chown "$giteauser" "$binname"
  102. chmod +x "$binname"
  103. # stop gitea, create backup, replace binary, restart gitea
  104. echo "Flushing gitea queues at $(date)"
  105. giteacmd manager flush-queues
  106. echo "Stopping gitea at $(date)"
  107. $service_stop
  108. echo "Creating backup in $giteahome"
  109. giteacmd dump $backupopts
  110. echo "Updating binary at $giteabin"
  111. cp -f "$giteabin" "$giteabin.bak" && mv -f "$binname" "$giteabin"
  112. $service_start
  113. $service_status
  114. echo "Upgrade to $giteaversion successful!"
  115. popd