Skip to content

Instantly share code, notes, and snippets.

@baztian
Last active April 4, 2024 19:17
Show Gist options
  • Save baztian/34141856aaa7f7ca95f6b045b7897d9f to your computer and use it in GitHub Desktop.
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
#!/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
@baztian
Copy link
Author

baztian commented Apr 4, 2024

Usage example:

$ update-github-deb.sh IsmaelMartinez/teams-for-linux teams-for-linux
Updating teams-for-linux from version 1.4.16 to version 1.4.17
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 70.5M  100 70.5M    0     0  3610k      0  0:00:19  0:00:19 --:--:-- 3850k
83f173e1bf7f4cc63e8d398177b7a07e038a4c3788d326a444e64b8d26711d73  /tmp/teams-for-linux_1.4.17_amd64.deb
[sudo] password for user:          
(Reading database ... 564427 files and directories currently installed.)
Preparing to unpack .../teams-for-linux_1.4.17_amd64.deb ...
Unpacking teams-for-linux (1.4.17) over (1.4.16) ...
Setting up teams-for-linux (1.4.17) ...
$ update-github-deb.sh IsmaelMartinez/teams-for-linux teams-for-linux
teams-for-linux is already up to date at 1.4.17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment