Created
May 28, 2024 18:28
-
-
Save rlindooren/c0c9ca71be4ac63a8cf19f75465c2a56 to your computer and use it in GitHub Desktop.
Bash script that can download an asset from a private Github repository
This file contains 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
#!/usr/bin/env bash | |
# Prerequisites: curl, jq, and a GitHub token (provided as environment variable `GITHUB_TOKEN`) | |
set -e | |
ORG=${ORG:-"your-default-org"} | |
REPO=${REPO:-"your-default-repo"} | |
API_URL="https://api.github.com/repos/${ORG}/${REPO}" | |
ARCH=${ARCH:-"linux_amd64"} | |
# Verify that the required environment variables are set | |
if [ -z "${TAG}" ]; then | |
echo "TAG environment variable is not set" | |
exit 1 | |
fi | |
if [ -z "${GITHUB_TOKEN}" ]; then | |
echo "GITHUB_TOKEN environment variable is not set" | |
exit 1 | |
fi | |
# Determine the identifier of the asset | |
echo "Determining asset identifier for ${API_URL}/releases/tags/${TAG} (file that contains '${ARCH}' in its name) ..." | |
curl --fail -sSL \ | |
-H "Authorization: token ${GITHUB_TOKEN}" \ | |
-H "Accept: application/vnd.github+json" \ | |
"${API_URL}/releases/tags/${TAG}" > all_assets_for_tag.json | |
jq -r '.assets[] | select(.name | contains("'"${ARCH}"'"))' < all_assets_for_tag.json > asset_info.json | |
ASSET_ID=$(jq -r '.id' < asset_info.json) | |
ASSET_NAME=$(jq -r '.name' < asset_info.json) | |
echo "Asset identifier: ${ASSET_ID}, file name: ${ASSET_NAME}" | |
rm all_assets_for_tag.json asset_info.json | |
# Download the asset, based on its identifier | |
echo "Downloading asset: ${API_URL}/releases/assets/${ASSET_ID} (${ASSET_NAME}) ..." | |
rm ${ASSET_NAME} || true | |
curl --fail -sSL -JO \ | |
-H "Authorization: token ${GITHUB_TOKEN}" \ | |
-H "Accept: application/octet-stream" \ | |
"${API_URL}/releases/assets/${ASSET_ID}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment