Last active
April 4, 2024 19:17
-
-
Save baztian/34141856aaa7f7ca95f6b045b7897d9f to your computer and use it in GitHub Desktop.
Update a debian package that's available as a download/realease on github
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Check if sufficient arguments are provided | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <github-repo> <package-name>" | |
exit 1 | |
fi | |
# Assign command line arguments to variables | |
REPO="$1" | |
PACKAGE_NAME="$2" | |
GITHUB_API="https://api.github.com/repos/$REPO/releases/latest" | |
# Fetch local version of the package | |
LOCAL_VERSION=$(dpkg-query -W -f='${Version}' $PACKAGE_NAME 2>/dev/null) | |
# Handle the case where the package is not installed | |
if [ -z "$LOCAL_VERSION" ]; then | |
echo "Package $PACKAGE_NAME is not installed." | |
exit 1 | |
fi | |
# Fetch the GitHub API result and cache it | |
API_RESULT=$(curl -s $GITHUB_API) | |
# Fetch the latest release version from GitHub | |
LATEST_VERSION=$(echo "$API_RESULT" | grep -Po '"tag_name": "\K.*?(?=")') | |
# Remove 'v' prefix from LATEST_VERSION if it exists | |
LATEST_VERSION=${LATEST_VERSION#v} | |
# Find the download URLs | |
DOWNLOAD_URLS=$(echo "$API_RESULT" | grep -Po '"browser_download_url": "\K.*?(?=")') | |
# Count the number of download URLs | |
URL_COUNT=$(echo "$DOWNLOAD_URLS" | wc -l) | |
# Select the correct download URL | |
if [ "$URL_COUNT" -gt 1 ]; then | |
# Get the system architecture | |
ARCH=$(dpkg --print-architecture) | |
DOWNLOAD_URL=$(echo "$DOWNLOAD_URLS" | grep "$ARCH") | |
else | |
DOWNLOAD_URL=$DOWNLOAD_URLS | |
fi | |
# Check if a download URL was found | |
if [ -z "$DOWNLOAD_URL" ]; then | |
echo "No appropriate download URL found." | |
exit 1 | |
fi | |
# Extract the file name from the download URL | |
FILE_NAME=$(basename "$DOWNLOAD_URL") | |
# Compare versions and download and install if a new version is available | |
if [ "$LATEST_VERSION" != "$LOCAL_VERSION" ]; then | |
echo "Updating $PACKAGE_NAME from version $LOCAL_VERSION to version $LATEST_VERSION" | |
DOWNLOAD_PATH="/tmp/$FILE_NAME" | |
curl -L -o "$DOWNLOAD_PATH" "$DOWNLOAD_URL" | |
echo "sha1: $(sha1sum $DOWNLOAD_PATH)" | |
echo "sha256: $(sha256sum $DOWNLOAD_PATH)" | |
sudo dpkg -i "$DOWNLOAD_PATH" | |
else | |
echo "$PACKAGE_NAME is already up to date at $LATEST_VERSION." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: