69 lines
2.0 KiB
Bash
Executable File
69 lines
2.0 KiB
Bash
Executable File
#!/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 - <<PY
|
|
from pathlib import Path
|
|
import re
|
|
p = Path('$STACK_DIR/docker-compose.yml')
|
|
s = p.read_text()
|
|
s2 = re.sub(r'image:\s*gitea/gitea:[^\s]+', 'image: gitea/gitea:$TARGET_VERSION', s, count=1)
|
|
if s2 == s:
|
|
raise SystemExit('Could not find gitea/gitea image line to replace')
|
|
p.write_text(s2)
|
|
PY
|
|
|
|
docker compose config >/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
|