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..."
|
|
|
|
|
|
2025-03-23 20:54:06 -04:00
|
|
|
# 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.]*' | \
|
2025-03-23 21:20:40 -04:00
|
|
|
sed -E 's/"tag_name":"v//g')
|
2025-03-23 20:00:39 -04:00
|
|
|
|
2025-03-23 20:54:06 -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
|
|
|
|
|
|
2025-03-23 20:54:06 -04:00
|
|
|
echo "$latest_version"
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 21:20:40 -04:00
|
|
|
# Get current version
|
|
|
|
|
get_current_version() {
|
|
|
|
|
docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo | cut -d':' -f2
|
2025-03-23 20:00:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Update Forgejo to the latest version
|
|
|
|
|
update_forgejo() {
|
|
|
|
|
local latest_version=$1
|
2025-03-23 21:11:51 -04:00
|
|
|
local image_name="codeberg.org/forgejo/forgejo"
|
2025-03-23 20:00:39 -04:00
|
|
|
|
2025-03-23 21:11:51 -04:00
|
|
|
log "Current image: $image_name:$(get_current_version)"
|
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
|
2025-03-23 21:11:51 -04:00
|
|
|
log "Pulling the latest Forgejo image... $image_name:$latest_version"
|
2025-03-23 20:57:42 -04:00
|
|
|
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..."
|
2025-03-23 21:11:51 -04:00
|
|
|
sed -i "s|codeberg.org/forgejo/forgejo:[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
|
|
|
|
|
|
2025-03-23 20:54:06 -04:00
|
|
|
# Wait for container to restart and verify
|
|
|
|
|
sleep 15
|
|
|
|
|
|
2025-03-23 20:00:39 -04:00
|
|
|
# Verify the update
|
2025-03-23 21:11:51 -04:00
|
|
|
NEW_VERSION=$(docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo | cut -d':' -f2)
|
2025-03-23 20:00:39 -04:00
|
|
|
|
2025-03-23 21:11:51 -04:00
|
|
|
if [ "$NEW_VERSION" = "$latest_version" ]; then
|
2025-03-23 20:00:39 -04:00
|
|
|
log "Forgejo successfully updated to version $latest_version"
|
|
|
|
|
else
|
2025-03-23 21:11:51 -04:00
|
|
|
log "ERROR: Update verification failed. Current version: $NEW_VERSION, Expected: $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 ==="
|
|
|
|
|
|
2025-03-23 20:54:06 -04:00
|
|
|
# Get versions
|
2025-03-23 21:11:51 -04:00
|
|
|
CURRENT_VERSION=$(get_current_version)
|
2025-03-23 20:00:39 -04:00
|
|
|
LATEST_VERSION=$(get_latest_version)
|
|
|
|
|
|
2025-03-23 20:54:06 -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 ==="
|