Improve auto-update script with better error handling and version extraction
This commit is contained in:
parent
18616f39f1
commit
050983c345
1 changed files with 31 additions and 13 deletions
|
|
@ -6,7 +6,6 @@
|
||||||
set -e
|
set -e
|
||||||
LOG_FILE="{{ forgejo_data_dir }}/logs/update.log"
|
LOG_FILE="{{ forgejo_data_dir }}/logs/update.log"
|
||||||
COMPOSE_FILE="{{ forgejo_data_dir }}/docker-compose.yml"
|
COMPOSE_FILE="{{ forgejo_data_dir }}/docker-compose.yml"
|
||||||
CURRENT_VERSION=$(docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo | awk -F':' '{print $2}')
|
|
||||||
|
|
||||||
# Create log directory if it doesn't exist
|
# Create log directory if it doesn't exist
|
||||||
mkdir -p "{{ forgejo_data_dir }}/logs"
|
mkdir -p "{{ forgejo_data_dir }}/logs"
|
||||||
|
|
@ -48,19 +47,32 @@ backup_forgejo() {
|
||||||
get_latest_version() {
|
get_latest_version() {
|
||||||
log "Checking for the latest Forgejo version..."
|
log "Checking for the latest Forgejo version..."
|
||||||
|
|
||||||
# Fetch the latest version from the Forgejo API
|
# Fetch the latest version from the Forgejo API with error handling
|
||||||
LATEST_VERSION=$(curl -s https://codeberg.org/api/v1/repos/forgejo/forgejo/releases/latest | grep -o '\"tag_name\":\"[^\"]*' | cut -d'\"' -f4 | sed 's/^v//')
|
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 's/"tag_name":"v//' || true)
|
||||||
|
|
||||||
if [ -z "$LATEST_VERSION" ]; then
|
if [ -z "$latest_version" ]; then
|
||||||
log "ERROR: Failed to retrieve the latest version. Exiting."
|
log "ERROR: Failed to retrieve the latest version from Codeberg API."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log "Latest version: $LATEST_VERSION"
|
echo "$latest_version"
|
||||||
log "Current version: $CURRENT_VERSION"
|
}
|
||||||
|
|
||||||
# Return the latest version
|
# Get current Forgejo version
|
||||||
echo "$LATEST_VERSION"
|
get_current_version() {
|
||||||
|
local current_version
|
||||||
|
current_version=$(docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo | \
|
||||||
|
awk -F':' '{print $2}' || true)
|
||||||
|
|
||||||
|
if [ -z "$current_version" ]; then
|
||||||
|
log "ERROR: Failed to retrieve the current Forgejo version."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$current_version"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Update Forgejo to the latest version
|
# Update Forgejo to the latest version
|
||||||
|
|
@ -84,15 +96,17 @@ update_forgejo() {
|
||||||
|
|
||||||
# Update the docker-compose file with the new version
|
# Update the docker-compose file with the new version
|
||||||
log "Updating docker-compose.yml 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"
|
sed -i "s/codeberg.org\/forgejo\/forgejo:[0-9.]*\+*/codeberg.org\/forgejo\/forgejo:$latest_version/g" "$COMPOSE_FILE"
|
||||||
|
|
||||||
# Restart Forgejo with the new version
|
# Restart Forgejo with the new version
|
||||||
log "Restarting Forgejo with the new version..."
|
log "Restarting Forgejo with the new version..."
|
||||||
cd "{{ forgejo_data_dir }}" && docker-compose down && docker-compose up -d
|
cd "{{ forgejo_data_dir }}" && docker-compose down && docker-compose up -d
|
||||||
|
|
||||||
|
# Wait for container to restart and verify
|
||||||
|
sleep 15
|
||||||
|
|
||||||
# Verify the update
|
# Verify the update
|
||||||
sleep 10
|
NEW_VERSION=$(get_current_version)
|
||||||
NEW_VERSION=$(docker inspect --format='{% raw %}{{.Config.Image}}{% endraw %}' forgejo | awk -F':' '{print $2}')
|
|
||||||
|
|
||||||
if [ "$NEW_VERSION" = "$latest_version" ]; then
|
if [ "$NEW_VERSION" = "$latest_version" ]; then
|
||||||
log "Forgejo successfully updated to version $latest_version"
|
log "Forgejo successfully updated to version $latest_version"
|
||||||
|
|
@ -106,9 +120,13 @@ update_forgejo() {
|
||||||
# Main execution
|
# Main execution
|
||||||
log "=== Forgejo Auto-Update Script Started ==="
|
log "=== Forgejo Auto-Update Script Started ==="
|
||||||
|
|
||||||
# Get the latest version
|
# Get versions
|
||||||
|
CURRENT_VERSION=$(get_current_version)
|
||||||
LATEST_VERSION=$(get_latest_version)
|
LATEST_VERSION=$(get_latest_version)
|
||||||
|
|
||||||
|
log "Current version: $CURRENT_VERSION"
|
||||||
|
log "Latest version: $LATEST_VERSION"
|
||||||
|
|
||||||
# Compare versions and update if needed
|
# Compare versions and update if needed
|
||||||
if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ]; then
|
if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ]; then
|
||||||
log "A new version is available. Updating from $CURRENT_VERSION to $LATEST_VERSION..."
|
log "A new version is available. Updating from $CURRENT_VERSION to $LATEST_VERSION..."
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue