infra/roles/app_deployer/files/rollback.sh
Jeremie Fraeys b9c5cdff12
Add app deployer role for automated deployments
- Systemd service and timer for deployment orchestration
- Webhook listener for Git-triggered deployments
- Forgejo Actions workflow for CI/CD pipeline
- Deployment scripts with rollback capability
- Deploy token validation for security
2026-02-21 18:31:12 -05:00

52 lines
1.5 KiB
Bash

#!/bin/bash
# Rollback script - revert to previous version of an app
# Usage: rollback.sh <app_name> [version]
# If version not specified, shows available versions
set -e
APP="$1"
TARGET_VERSION="$2"
ARTIFACTS_DIR="/opt/artifacts"
APP_DIR="/opt/apps/${APP}"
if [ -z "$APP" ]; then
echo "Usage: $0 <app_name> [version]"
echo "Examples:"
echo " $0 my-api # List available versions"
echo " $0 my-api abc123def # Rollback to specific version"
exit 1
fi
# List available versions if no target specified
if [ -z "$TARGET_VERSION" ]; then
echo "Available versions for ${APP}:"
ls -lt "${ARTIFACTS_DIR}/${APP}"-* 2>/dev/null | grep -v '.sha256$' | head -10 | while read line; do
version=$(echo "$line" | awk '{print $NF}' | sed "s|${ARTIFACTS_DIR}/${APP}-||")
echo " ${version}"
done
echo ""
echo "Current version:"
cat "${APP_DIR}/.current-version" 2>/dev/null || echo " (unknown)"
exit 0
fi
# Check if target version exists
if [ ! -f "${ARTIFACTS_DIR}/${APP}-${TARGET_VERSION}" ]; then
echo "ERROR: Version ${TARGET_VERSION} not found in ${ARTIFACTS_DIR}/"
echo "Run '$0 ${APP}' to see available versions"
exit 1
fi
echo "Rolling back ${APP} to version ${TARGET_VERSION}..."
# Update symlink
ln -sf "${APP_DIR}/app-${TARGET_VERSION}" "${APP_DIR}/app"
# Update version file
echo "${TARGET_VERSION}" > "${APP_DIR}/.current-version"
# Restart the service
sudo systemctl restart "${APP}"
echo "Rollback complete: ${APP} is now running version ${TARGET_VERSION}"