#!/bin/bash set -euo pipefail # Variables FORGEJO_API_URL="https://code.forgejo.org/api/v1/repos/forgejo/runner/releases" ARCH=$(uname -m) OS=$(uname -s | tr '[:upper:]' '[:lower:]') # Map architecture names to expected binary names case "$ARCH" in x86_64) ARCH="amd64" ;; aarch64) ARCH="arm64" ;; armv7l) ARCH="armv7" ;; *) echo "Unsupported architecture: $ARCH"; exit 1 ;; esac # Check for sudo permissions if ! sudo -v; then echo "This script requires sudo permissions. Please run it with a user that has sudo access." exit 1 fi # Fetch the latest release information echo "Fetching the latest release information..." RELEASE_INFO=$(curl -s "$FORGEJO_API_URL" | jq '.[0]') if [ -z "$RELEASE_INFO" ]; then echo "Failed to fetch release information." exit 1 fi # Extract the latest version LATEST_VERSION=$(echo "$RELEASE_INFO" | jq -r '.tag_name') # Check if the binary already exists if [ -f "./forgejo-runner" ]; then # Get the local version LOCAL_VERSION=$(./forgejo-runner --version 2>/dev/null | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+') if [ "$LOCAL_VERSION" == "$LATEST_VERSION" ]; then echo "The latest version ($LATEST_VERSION) is already installed." exit 0 else echo "A different version is installed locally: $LOCAL_VERSION." echo "Updating to the latest version: $LATEST_VERSION." fi fi # Extract the download URL for the correct binary DOWNLOAD_URL=$(echo "$RELEASE_INFO" | jq -r --arg arch "$ARCH" --arg os "$OS" '.assets[] | select(.name | test($os + ".*" + $arch + "$")) | .browser_download_url') if [ -z "$DOWNLOAD_URL" ]; then echo "No binary found for architecture: $ARCH and OS: $OS" exit 1 fi # Download the binary echo "Downloading the binary from: $DOWNLOAD_URL" curl --progress-bar -LO "$DOWNLOAD_URL" if [ $? -ne 0 ]; then echo "Failed to download the binary." exit 1 fi # Create a temporary directory for the checksum file TEMP_DIR=$(mktemp -d) CHECKSUM_URL="${DOWNLOAD_URL}.sha256" CHECKSUM_FILE="$TEMP_DIR/$(basename "$CHECKSUM_URL")" # Download the checksum file echo "Downloading the checksum file from: $CHECKSUM_URL" curl --progress-bar -o "$CHECKSUM_FILE" "$CHECKSUM_URL" if [ $? -ne 0 ]; then echo "Failed to download the checksum file." rm -rf "$TEMP_DIR" exit 1 fi # Verify the checksum echo "Verifying the integrity of the downloaded file..." sha256sum -c "$CHECKSUM_FILE" if [ $? -ne 0 ]; then echo "Checksum verification failed. The file may be corrupted." rm -rf "$TEMP_DIR" exit 1 fi echo "Checksum verification passed." # Clean up the temporary directory rm -rf "$TEMP_DIR" # Extract the filename from the URL FILENAME=$(basename "$DOWNLOAD_URL") # Make the file executable echo "Making the file executable..." chmod +x "$FILENAME" echo "Linking the binary to ${PWD}/forgejo-runner..." sudo ln -sf "$FILENAME" ./forgejo-runner if [ $? -eq 0 ]; then echo "Forgejo Runner installed successfully as 'forgejo-runner'." else echo "Failed to install Forgejo Runner." exit 1 fi # Find all forgejo-runner binaries, sort them by version, and keep only the latest two echo "Cleaning up old versions of forgejo-runner binaries..." ls -1 ./forgejo-runner-* 2>/dev/null | sort -V | head -n -2 | while read -r OLD_BINARY; do echo "Removing old binary: $OLD_BINARY" sudo rm -f "$OLD_BINARY" done echo "Cleanup complete. Latest two versions are retained." # Ask whether to restart the systemd service echo "Do you want to restart the forgejo-runner.service? (y/n)" read -r RESTART_RESPONSE if [[ "$RESTART_RESPONSE" == "y" ]]; then echo "Restarting forgejo-runner.service..." sudo systemctl restart forgejo-runner.service if [ $? -eq 0 ]; then echo "forgejo-runner.service restarted successfully." else echo "Failed to restart forgejo-runner.service." exit 1 fi else echo "Skipping service restart." fi