ci: upload release assets through the REST API

This commit is contained in:
Kevin Yue committed 2026-08-01 00:19:54 +08:00
1 parent cfe8430cae
commit b0b8e5ec32
1 file changed
+31 -15
+31 -15
View File
@@ -1,18 +1,25 @@
#!/bin/bash #!/bin/bash
# Usage: ./scripts/upload-release-assets.sh <tag> <file> [file...] # Usage: ./scripts/upload-release-assets.sh [--clobber] <tag> <file> [file...]
set -euo pipefail set -euo pipefail
REPOSITORY="${GITHUB_REPOSITORY:-yuezk/GlobalProtect-openconnect}" REPOSITORY="${GITHUB_REPOSITORY:-yuezk/GlobalProtect-openconnect}"
TAG="${1:-}"
MAX_ATTEMPTS=8 MAX_ATTEMPTS=8
CLOBBER=false
if [[ "${1:-}" == "--clobber" ]]; then
CLOBBER=true
shift
fi
TAG="${1:-}"
if [[ -z "$TAG" || "$#" -lt 2 ]]; then if [[ -z "$TAG" || "$#" -lt 2 ]]; then
echo "Usage: $0 <tag> <file> [file...]" >&2 echo "Usage: $0 [--clobber] <tag> <file> [file...]" >&2
exit 2 exit 2
fi fi
shift shift
RELEASE_ID="$(gh api "repos/$REPOSITORY/releases/tags/$TAG" --jq .id)"
file_digest() { file_digest() {
if command -v sha256sum >/dev/null; then if command -v sha256sum >/dev/null; then
@@ -22,13 +29,14 @@ file_digest() {
fi fi
} }
upload_immutable() { upload_asset() {
local file=$1 local file=$1
local asset local asset
local asset_id
local assets_json local assets_json
local encoded_asset
local local_digest local local_digest
local remote_digest local remote_digest
local remote_exists
local attempt=1 local attempt=1
local delay local delay
@@ -37,24 +45,32 @@ upload_immutable() {
return 1 return 1
fi fi
asset="$(basename "$file")" asset="$(basename "$file")"
encoded_asset="$(jq -rn --arg name "$asset" '$name | @uri')"
local_digest="sha256:$(file_digest "$file")" local_digest="sha256:$(file_digest "$file")"
while (( attempt <= MAX_ATTEMPTS )); do while (( attempt <= MAX_ATTEMPTS )); do
assets_json="$(gh release view "$TAG" --repo "$REPOSITORY" --json assets)" assets_json="$(gh api "repos/$REPOSITORY/releases/$RELEASE_ID/assets?per_page=100")"
remote_exists="$(jq -r --arg name "$asset" \ asset_id="$(jq -r --arg name "$asset" \
'[.assets[] | select(.name == $name)] | length' <<<"$assets_json")" '[.[] | select(.name == $name)] | first | .id // empty' <<<"$assets_json")"
if [[ "$remote_exists" != "0" ]]; then if [[ -n "$asset_id" ]]; then
remote_digest="$(jq -r --arg name "$asset" \ remote_digest="$(jq -r --arg name "$asset" \
'.assets[] | select(.name == $name) | .digest // empty' <<<"$assets_json")" '.[] | select(.name == $name) | .digest // empty' <<<"$assets_json")"
if [[ "$remote_digest" == "$local_digest" ]]; then if [[ "$CLOBBER" == "false" && "$remote_digest" == "$local_digest" ]]; then
echo "$asset is already uploaded with the expected digest" echo "$asset is already uploaded with the expected digest"
return 0 return 0
fi fi
echo "Refusing to replace immutable release asset: $asset" >&2 if [[ "$CLOBBER" == "false" ]]; then
return 1 echo "Refusing to replace immutable release asset: $asset" >&2
return 1
fi
gh api --method DELETE "repos/$REPOSITORY/releases/assets/$asset_id"
fi fi
if gh release upload "$TAG" "$file" --repo "$REPOSITORY"; then if gh api --method POST \
-H 'Content-Type: application/octet-stream' \
--input "$file" \
"https://uploads.github.com/repos/$REPOSITORY/releases/$RELEASE_ID/assets?name=$encoded_asset" \
>/dev/null; then
return 0 return 0
fi fi
if (( attempt >= MAX_ATTEMPTS )); then if (( attempt >= MAX_ATTEMPTS )); then
@@ -69,5 +85,5 @@ upload_immutable() {
} }
for file in "$@"; do for file in "$@"; do
upload_immutable "$file" upload_asset "$file"
done done