forgejo-config/ansible/templates/update-forgejo.sh.j2

121 lines
3.9 KiB
Text
Raw Normal View History

2025-03-23 20:00:39 -04:00
#!/bin/bash
# Script to automatically update Forgejo to the latest version
# Created: {{ ansible_date_time.date }}
# This script is managed by Ansible - manual changes will be overwritten
set -e
LOG_FILE="{{ forgejo_data_dir }}/logs/update.log"
COMPOSE_FILE="{{ forgejo_data_dir }}/docker-compose.yml"
CURRENT_VERSION=$(docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo | awk -F':' '{print $2}')
2025-03-23 20:00:39 -04:00
# Create log directory if it doesn't exist
mkdir -p "{{ forgejo_data_dir }}/logs"
# Function for logging
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# Function to check if Forgejo is running
is_forgejo_running() {
if docker ps | grep -q forgejo; then
return 0
else
return 1
fi
}
# Function to backup Forgejo data
backup_forgejo() {
local backup_dir="{{ forgejo_data_dir }}/backups"
local backup_file="forgejo_backup_$(date '+%Y%m%d_%H%M%S').tar.gz"
log "Creating backup of Forgejo data..."
mkdir -p "$backup_dir"
# Backup the data directory (excluding large objects if space is a concern)
tar -czf "$backup_dir/$backup_file" -C "{{ forgejo_data_dir }}" \
--exclude="data/gitea-repositories" \
--exclude="data/avatars" \
--exclude="data/attachments" \
--exclude="data/lfs" \
data/
log "Backup created at $backup_dir/$backup_file"
}
# Get the latest Forgejo version
get_latest_version() {
log "Checking for the latest Forgejo version..."
# Fetch the latest version from the Forgejo API
LATEST_VERSION=$(curl -s https://codeberg.org/api/v1/repos/forgejo/forgejo/releases/latest | grep -o '\"tag_name\":\"[^\"]*' | cut -d'\"' -f4 | sed 's/^v//')
2025-03-23 20:00:39 -04:00
if [ -z "$LATEST_VERSION" ]; then
log "ERROR: Failed to retrieve the latest version. Exiting."
exit 1
fi
log "Latest version: $LATEST_VERSION"
log "Current version: $CURRENT_VERSION"
# Return the latest version
echo "$LATEST_VERSION"
}
# Update Forgejo to the latest version
update_forgejo() {
local latest_version=$1
log "Starting Forgejo update process..."
# Check if Forgejo is running
if ! is_forgejo_running; then
log "ERROR: Forgejo is not running. Please start it before updating."
exit 1
fi
# Create a backup before updating
backup_forgejo
# Pull the latest image
log "Pulling the latest Forgejo image..."
docker pull "codeberg.org/forgejo/forgejo:$latest_version"
# Update the docker-compose file with the new version
log "Updating docker-compose.yml with the new version..."
sed -i "s/codeberg.org\/forgejo\/forgejo:[0-9]*\.[0-9]*\.[0-9]*/codeberg.org\/forgejo\/forgejo:$latest_version/g" "$COMPOSE_FILE"
# Restart Forgejo with the new version
log "Restarting Forgejo with the new version..."
cd "{{ forgejo_data_dir }}" && docker-compose down && docker-compose up -d
# Verify the update
sleep 10
NEW_VERSION=$(docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo | awk -F':' '{print $2}')
2025-03-23 20:00:39 -04:00
if [ "$NEW_VERSION" = "$latest_version" ]; then
log "Forgejo successfully updated to version $latest_version"
else
log "ERROR: Update verification failed. Current version: $NEW_VERSION, Expected: $latest_version"
log "Please check the container logs for more information."
exit 1
fi
}
# Main execution
log "=== Forgejo Auto-Update Script Started ==="
# Get the latest version
LATEST_VERSION=$(get_latest_version)
# Compare versions and update if needed
if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ]; then
log "A new version is available. Updating from $CURRENT_VERSION to $LATEST_VERSION..."
update_forgejo "$LATEST_VERSION"
else
log "Forgejo is already at the latest version ($CURRENT_VERSION). No update needed."
fi
log "=== Forgejo Auto-Update Script Completed ==="