Skip to content

Instantly share code, notes, and snippets.

@jroehl
Last active April 14, 2024 15:30
Show Gist options
  • Save jroehl/3de9407adebee7c1e845f0c52b5a8df3 to your computer and use it in GitHub Desktop.
Save jroehl/3de9407adebee7c1e845f0c52b5a8df3 to your computer and use it in GitHub Desktop.
Download a github release, specified by version number

Bash script to download release from github

This script is useful if you want to download a release, specified by version number, unpack the tar file and smoke test the executable.

Usage in a script

  export RELEASE=0.0.1
  export REPO_OWNER=jroehl
  export REPO=go-suitesync
  export EXECUTABLE=suitesync
  curl -sL https://gist.github.com/jroehl/3de9407adebee7c1e845f0c52b5a8df3/raw/download-release.sh | bash
#!/usr/bin/env bash
# Define variables.
if [[ "$OSTYPE" == "linux-gnu" ]]; then
TAR="${REPO}_${RELEASE}_linux_amd64.tar.gz"
elif [[ "$OSTYPE" == "darwin"* ]]; then
TAR="${REPO}_${RELEASE}_darwin_amd64.tar.gz"
else
echo "Only \"MacOS\" and \"Linux\" are supported - not \"$OSTYPE\""
exit 1;
fi
TAG="v$RELEASE"
EXECUTABLE_DIR=$(pwd)/.$EXECUTABLE
GH_API="https://api.github.com"
GH_REPO="$GH_API/repos/${REPO_OWNER}/${REPO}"
GH_TAGS="$GH_REPO/releases/tags/$TAG"
# create dir
mkdir -p $EXECUTABLE_DIR
cd $EXECUTABLE_DIR
# Download release
echo ""
echo "Downloading $EXECUTABLE release \"$TAG\""
echo ""
# Read asset tags.
response=$(curl -s $GH_TAGS)
# Get ID of the asset based on given name.
eval $(echo "$response" | grep -C3 "name.:.\+$TAR" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
[ "$id" ] || { echo "Error: Failed to get asset id, response: $response" | awk 'length($0)<100' >&2; exit 1; }
wget --content-disposition --no-cookie -q --header "Accept: application/octet-stream" "$GH_REPO/releases/assets/$id" --show-progress
# unpack
tar -xf $TAR
rm -f $TAR
# make executable
chmod +x $EXECUTABLE_DIR/$EXECUTABLE
echo ""
# smoke test executable installation
if ! [ -f $EXECUTABLE_DIR/$EXECUTABLE ]; then
echo "$EXECUTABLE setup failed"
exit 1
fi
echo "$EXECUTABLE download successful"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment