master_setup/github_package_update.bash

46 lines
1.6 KiB
Bash

#!/bin/bash
# Function to check and pull the latest version of a package from GitHub
check_and_update_package() {
local repo_owner="$1"
local repo_name="$2"
local local_path="$3"
echo "Checking for updates for $repo_name..."
# Get the latest release info from GitHub API
latest_release=$(curl -s "https://api.github.com/repos/$repo_owner/$repo_name/releases/latest")
latest_version=$(echo "$latest_release" | grep -oP '(?<="tag_name": ")([^"]+)')
if [ -z "$latest_version" ]; then
echo "Failed to fetch latest version for $repo_name."
return 1
fi
# Check if the package is already installed at the local path
if [ -d "$local_path" ]; then
# Check if the installed version matches the latest
current_version=$(cd "$local_path" && git describe --tags 2>/dev/null || echo 'unknown')
if [ "$current_version" == "$latest_version" ]; then
echo "$repo_name is already up to date (version: $current_version)."
return 0
else
echo "Updating $repo_name from version $current_version to $latest_version..."
cd "$local_path" && git pull origin "$latest_version"
fi
else
echo "Installing $repo_name version $latest_version..."
git clone "https://github.com/$repo_owner/$repo_name.git" "$local_path"
cd "$local_path" && git checkout "$latest_version"
fi
if [ $? -eq 0 ]; then
echo "$repo_name has been updated to version $latest_version."
else
echo "Failed to update $repo_name."
return 1
fi
}
# Example usage:
# check_and_update_package 'owner' 'repository' '/path/to/install'