#!/usr/bin/env bash # ============================================================================= # Download latest Linux amd64 release from GitHub # ============================================================================= set -euo pipefail # ========================= CONFIGURATION ========================= OWNER="${1:-}" # GitHub owner (user/org) REPO="${2:-}" # GitHub repository name # Optional: custom pattern (regex) to match the asset name PATTERN="${3:-linux.*amd64}" # ========================= VALIDATION ========================= if [[ -z "$OWNER" || -z "$REPO" ]]; then echo "Usage: $0 [pattern]" echo "Example: $0 prometheus node_exporter" echo " $0 kubernetes kubernetes 'linux-amd64.tar.gz'" exit 1 fi echo "Fetching latest release for ${OWNER}/${REPO}..." # Get latest release JSON JSON=$(curl -sL \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ "https://api.github.com/repos/${OWNER}/${REPO}/releases/latest") # Extract tag name (for info) TAG=$(echo "$JSON" | jq -r '.tag_name') echo "Latest release: ${TAG}" # Find the first asset matching the pattern (case insensitive) DOWNLOAD_URL=$(echo "$JSON" | jq -r \ --arg pattern "$PATTERN" ' .assets[] | select(.name | test($pattern; "i")) | .browser_download_url ' | head -n 1) if [[ -z "$DOWNLOAD_URL" || "$DOWNLOAD_URL" == "null" ]]; then echo "❌ No matching Linux amd64 asset found for pattern: $PATTERN" echo "Available assets:" echo "$JSON" | jq -r '.assets[].name' exit 1 fi FILENAME=$(basename "$DOWNLOAD_URL") echo "✅ Found: $FILENAME" echo "Downloading..." curl -L --progress-bar -o "$FILENAME" "$DOWNLOAD_URL" echo "✅ Download completed: $FILENAME"