code.gitea.io/gitea@v1.22.3/contrib/upgrade.sh (about)

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