#!/usr/bin/env bash set -euo pipefail STACK_DIR="${STACK_DIR:-/opt/gitea}" TARGET_VERSION="${TARGET_VERSION:?set TARGET_VERSION, e.g. TARGET_VERSION=1.26.2}" GITEA_CONTAINER="${GITEA_CONTAINER:-gitea}" APPLY="${APPLY:-0}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$STACK_DIR" if [ ! -f docker-compose.yml ]; then echo "docker-compose.yml not found in $STACK_DIR" >&2 exit 1 fi echo "== Current version ==" docker exec -u git "$GITEA_CONTAINER" gitea --version || true echo "== Creating backup first ==" "$SCRIPT_DIR/backup-gitea.sh" echo "== Pulling target image ==" docker pull "gitea/gitea:$TARGET_VERSION" docker run --rm --user git --entrypoint /usr/local/bin/gitea "gitea/gitea:$TARGET_VERSION" --version || true OLD_IMAGE_ID="$(docker image inspect "$(docker inspect --format '{{.Config.Image}}' "$GITEA_CONTAINER")" --format '{{.Id}}' 2>/dev/null || true)" if [ -n "$OLD_IMAGE_ID" ]; then ROLLBACK_TAG="gitea/gitea:rollback-$(date -u +%Y%m%d-%H%M%S)" docker tag "$OLD_IMAGE_ID" "$ROLLBACK_TAG" echo "Rollback image tag: $ROLLBACK_TAG" fi echo "== Pinning docker-compose.yml to target version ==" python3 - </dev/null if [ "$APPLY" != "1" ]; then echo "Dry-run complete. Compose file was pinned, but service was not recreated." echo "Review the diff, then run with APPLY=1 to recreate the Gitea service." exit 0 fi echo "== Recreating Gitea service only ==" docker compose up -d server echo "== Waiting for readiness ==" for _ in $(seq 1 90); do if curl -fsS "http://127.0.0.1:3001/api/v1/version" | grep -q "$TARGET_VERSION"; then echo echo "Gitea $TARGET_VERSION is ready." exit 0 fi sleep 2 done echo "Gitea did not report target version in time" >&2 exit 1