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

142 lines
4.3 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"
# 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 with error handling
local latest_version
latest_version=$(curl -s --max-time 10 https://codeberg.org/api/v1/repos/forgejo/forgejo/releases/latest | \
grep -o '"tag_name":"v[0-9.]*' | \
sed -E 's/"tag_name":"v//g' || true)
2025-03-23 20:00:39 -04:00
if [ -z "$latest_version" ]; then
log "ERROR: Failed to retrieve the latest version from Codeberg API."
2025-03-23 20:00:39 -04:00
exit 1
fi
echo "$latest_version"
}
# Get current Forgejo version and image
get_current_image_details() {
local current_image
current_image=$(docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo || true)
if [ -z "$current_image" ]; then
log "ERROR: Failed to retrieve the current Forgejo image."
exit 1
fi
echo "$current_image"
2025-03-23 20:00:39 -04:00
}
# Update Forgejo to the latest version
update_forgejo() {
local latest_version=$1
local current_image=$(get_current_image_details)
local image_name=$(echo "$current_image" | cut -d':' -f1)
2025-03-23 20:00:39 -04:00
log "Current image: $current_image"
log "Image name: $image_name"
2025-03-23 20:00:39 -04:00
# 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 "$image_name:$latest_version"
2025-03-23 20:00:39 -04:00
# Update the docker-compose file with the new version
log "Updating docker-compose.yml with the new version..."
sed -i "s|$image_name:[0-9.]*\+*|$image_name:$latest_version|g" "$COMPOSE_FILE"
2025-03-23 20:00:39 -04:00
# Restart Forgejo with the new version
log "Restarting Forgejo with the new version..."
cd "{{ forgejo_data_dir }}" && docker-compose down && docker-compose up -d
# Wait for container to restart and verify
sleep 15
2025-03-23 20:00:39 -04:00
# Verify the update
NEW_IMAGE=$(get_current_image_details)
2025-03-23 20:00:39 -04:00
if [[ "$NEW_IMAGE" == *":$latest_version" ]]; then
2025-03-23 20:00:39 -04:00
log "Forgejo successfully updated to version $latest_version"
else
log "ERROR: Update verification failed. Current image: $NEW_IMAGE, Expected version: $latest_version"
2025-03-23 20:00:39 -04:00
log "Please check the container logs for more information."
exit 1
fi
}
# Main execution
log "=== Forgejo Auto-Update Script Started ==="
# Get versions
CURRENT_IMAGE=$(get_current_image_details)
2025-03-23 20:00:39 -04:00
LATEST_VERSION=$(get_latest_version)
CURRENT_VERSION=$(echo "$CURRENT_IMAGE" | cut -d':' -f2)
2025-03-23 20:00:39 -04:00
log "Current version: $CURRENT_VERSION"
log "Latest version: $LATEST_VERSION"
2025-03-23 20:00:39 -04:00
# 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 ==="