58 lines
1.7 KiB
Bash
58 lines
1.7 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
# SPDX-FileCopyrightText: 2025 Vital A Pi <code@vp.ag>
|
||
|
|
# SPDX-License-Identifier: MIT
|
||
|
|
#
|
||
|
|
# GitHub Release Asset Downloader with Integrity Check (Forgejo CI-Kit)
|
||
|
|
# Features:
|
||
|
|
# - Executes in an isolated subshell to prevent env/flags contamination.
|
||
|
|
# - Parses lists reliably using bash readarray.
|
||
|
|
# - Leverages native GitHub 2025+ SHA256 digests (.digest) for instant verification.
|
||
|
|
#
|
||
|
|
|
||
|
|
|
||
|
|
fetch_gh_asset() (
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
if [ $# -lt 3 ]; then
|
||
|
|
echo "Error: Missing arguments" >&2
|
||
|
|
echo "Usage: fetch_gh_asset <repo_slug> <release_tag> <asset_name> [<jq_predicate>]" >&2
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
local REPO_SLUG="$1"
|
||
|
|
local RELEASE_TAG="$2"
|
||
|
|
local ASSET_NAME_TO_LOOKUP="$3"
|
||
|
|
local JQ_PREDICATE="${4:-}"
|
||
|
|
|
||
|
|
if [ ! -z "$JQ_PREDICATE" ]; then
|
||
|
|
local JQ_FILTER=".name | $JQ_PREDICATE"
|
||
|
|
else
|
||
|
|
local JQ_FILTER='.name == $aname'
|
||
|
|
fi
|
||
|
|
|
||
|
|
readarray -t DATA < <(
|
||
|
|
curl -s -f -H 'Accept: application/vnd.github+json' \
|
||
|
|
"https://api.github.com/repos/${REPO_SLUG}/releases/tags/${RELEASE_TAG}" \
|
||
|
|
| jq -r --arg aname "$ASSET_NAME_TO_LOOKUP" \
|
||
|
|
".assets[] | select($JQ_FILTER) | .name, .digest, .browser_download_url"
|
||
|
|
)
|
||
|
|
if [ ${#DATA[@]} -ne 3 ]; then
|
||
|
|
echo "Error: Asset not found, or ambiguous matches found, or API request failed" >&2
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
local ASSET_NAME="${DATA[0]}"
|
||
|
|
# Releases now expose digests (SHA256) for release assets
|
||
|
|
# https://github.blog/changelog/2025-06-03-releases-now-expose-digests-for-release-assets/
|
||
|
|
local ASSET_DIGEST="${DATA[1]#*:}" # Comes as "sha256:..."
|
||
|
|
local ASSET_URL="${DATA[2]}"
|
||
|
|
|
||
|
|
curl -sfL -o "$ASSET_NAME" "$ASSET_URL"
|
||
|
|
|
||
|
|
echo "$ASSET_DIGEST $ASSET_NAME" | shasum -a 256 -c -
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||
|
|
fetch_gh_asset "$@"
|
||
|
|
fi
|