Files
importarr/deploy/repo-upgrade.sh
T
daniels a93480e48a
Deploy live / test (push) Failing after 36s
Deploy live / deploy (push) Skipped
Show build info in header
Deploy the CI-built wheel for the triggering SHA so the UI and\nbuild metadata stay in sync in production.
2026-08-11 10:16:37 +02:00

79 lines
2.3 KiB
Bash

#!/usr/bin/env sh
set -eu
if [ "$(id -u)" -ne 0 ]; then
echo "Run as root: sudo sh /opt/importarr/repo-upgrade.sh" >&2
exit 1
fi
ENV_FILE=${IMPORTARR_ENV_FILE:-/etc/importarr/importarr.env}
if [ -f "$ENV_FILE" ]; then
# shellcheck disable=SC1090
. "$ENV_FILE"
fi
PREFIX=${IMPORTARR_PREFIX:-/opt/importarr}
REPO_DIR=${IMPORTARR_REPO_DIR:-$PREFIX/repo}
SERVICE=${IMPORTARR_SERVICE:-importarr.service}
VENV=${IMPORTARR_VENV:-$PREFIX/venv}
if [ "$#" -gt 2 ]; then
echo "Usage: $0 [wheel [revision]]" >&2
exit 2
fi
WHEEL=${1:-}
REVISION=${2:-}
if [ -n "$WHEEL" ] && [ ! -f "$WHEEL" ]; then
echo "Built wheel not found: $WHEEL" >&2
exit 1
fi
LOCK_FILE=${IMPORTARR_UPGRADE_LOCK_FILE:-$PREFIX/repo-upgrade.lock}
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "Refusing to upgrade: another Importarr upgrade is already running." >&2
exit 1
fi
if [ ! -d "$REPO_DIR/.git" ]; then
echo "Importarr repo not found at $REPO_DIR" >&2
exit 1
fi
cd "$REPO_DIR"
if [ -n "$(git status --porcelain)" ]; then
echo "Refusing to upgrade: $REPO_DIR has uncommitted changes." >&2
git status --short >&2
exit 1
fi
if [ -n "$REVISION" ]; then
test "$(git rev-parse HEAD)" = "$(git rev-parse "$REVISION^{commit}")"
else
git fetch --prune origin main
git checkout -B main origin/main
test "$(git rev-parse HEAD)" = "$(git rev-parse origin/main)"
fi
"$VENV/bin/pip" install --upgrade --force-reinstall --no-cache-dir "${WHEEL:-$REPO_DIR}"
if [ -f "$ENV_FILE" ]; then
sed -i '/^[[:space:]]*IMPORTARR_UPDATE_COMMAND=/d' "$ENV_FILE"
fi
install -m 0644 "$REPO_DIR/deploy/importarr.service" /etc/systemd/system/importarr.service
install -m 0755 "$REPO_DIR/deploy/repo-upgrade.sh" "$PREFIX/repo-upgrade.sh"
systemctl daemon-reload
GIT_SHA="$(git rev-parse --short=12 HEAD 2>/dev/null || printf development)"
BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
VERSION="$(git rev-parse --short=12 HEAD)"
BUILD_ENV_TMP="$PREFIX/build.env.tmp.$$"
trap 'rm -f "$BUILD_ENV_TMP"' EXIT HUP INT TERM
cat > "$BUILD_ENV_TMP" <<EOF
IMPORTARR_VERSION=$VERSION
IMPORTARR_GIT_SHA=$GIT_SHA
IMPORTARR_BUILD_DATE=$BUILD_DATE
EOF
chmod 0644 "$BUILD_ENV_TMP"
mv -f "$BUILD_ENV_TMP" "$PREFIX/build.env"
trap - EXIT HUP INT TERM
systemctl restart "$SERVICE"
systemctl restart manual-media-import.timer
systemctl --no-pager --full status "$SERVICE"