mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
ci: upload release assets through the REST API
This commit is contained in:
1 file changed
+31
-15
@@ -1,18 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Usage: ./scripts/upload-release-assets.sh <tag> <file> [file...]
|
||||
# Usage: ./scripts/upload-release-assets.sh [--clobber] <tag> <file> [file...]
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
REPOSITORY="${GITHUB_REPOSITORY:-yuezk/GlobalProtect-openconnect}"
|
||||
TAG="${1:-}"
|
||||
MAX_ATTEMPTS=8
|
||||
CLOBBER=false
|
||||
|
||||
if [[ "${1:-}" == "--clobber" ]]; then
|
||||
CLOBBER=true
|
||||
shift
|
||||
fi
|
||||
TAG="${1:-}"
|
||||
|
||||
if [[ -z "$TAG" || "$#" -lt 2 ]]; then
|
||||
echo "Usage: $0 <tag> <file> [file...]" >&2
|
||||
echo "Usage: $0 [--clobber] <tag> <file> [file...]" >&2
|
||||
exit 2
|
||||
fi
|
||||
shift
|
||||
RELEASE_ID="$(gh api "repos/$REPOSITORY/releases/tags/$TAG" --jq .id)"
|
||||
|
||||
file_digest() {
|
||||
if command -v sha256sum >/dev/null; then
|
||||
@@ -22,13 +29,14 @@ file_digest() {
|
||||
fi
|
||||
}
|
||||
|
||||
upload_immutable() {
|
||||
upload_asset() {
|
||||
local file=$1
|
||||
local asset
|
||||
local asset_id
|
||||
local assets_json
|
||||
local encoded_asset
|
||||
local local_digest
|
||||
local remote_digest
|
||||
local remote_exists
|
||||
local attempt=1
|
||||
local delay
|
||||
|
||||
@@ -37,24 +45,32 @@ upload_immutable() {
|
||||
return 1
|
||||
fi
|
||||
asset="$(basename "$file")"
|
||||
encoded_asset="$(jq -rn --arg name "$asset" '$name | @uri')"
|
||||
local_digest="sha256:$(file_digest "$file")"
|
||||
|
||||
while (( attempt <= MAX_ATTEMPTS )); do
|
||||
assets_json="$(gh release view "$TAG" --repo "$REPOSITORY" --json assets)"
|
||||
remote_exists="$(jq -r --arg name "$asset" \
|
||||
'[.assets[] | select(.name == $name)] | length' <<<"$assets_json")"
|
||||
if [[ "$remote_exists" != "0" ]]; then
|
||||
assets_json="$(gh api "repos/$REPOSITORY/releases/$RELEASE_ID/assets?per_page=100")"
|
||||
asset_id="$(jq -r --arg name "$asset" \
|
||||
'[.[] | select(.name == $name)] | first | .id // empty' <<<"$assets_json")"
|
||||
if [[ -n "$asset_id" ]]; then
|
||||
remote_digest="$(jq -r --arg name "$asset" \
|
||||
'.assets[] | select(.name == $name) | .digest // empty' <<<"$assets_json")"
|
||||
if [[ "$remote_digest" == "$local_digest" ]]; then
|
||||
'.[] | select(.name == $name) | .digest // empty' <<<"$assets_json")"
|
||||
if [[ "$CLOBBER" == "false" && "$remote_digest" == "$local_digest" ]]; then
|
||||
echo "$asset is already uploaded with the expected digest"
|
||||
return 0
|
||||
fi
|
||||
echo "Refusing to replace immutable release asset: $asset" >&2
|
||||
return 1
|
||||
if [[ "$CLOBBER" == "false" ]]; then
|
||||
echo "Refusing to replace immutable release asset: $asset" >&2
|
||||
return 1
|
||||
fi
|
||||
gh api --method DELETE "repos/$REPOSITORY/releases/assets/$asset_id"
|
||||
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
|
||||
fi
|
||||
if (( attempt >= MAX_ATTEMPTS )); then
|
||||
@@ -69,5 +85,5 @@ upload_immutable() {
|
||||
}
|
||||
|
||||
for file in "$@"; do
|
||||
upload_immutable "$file"
|
||||
upload_asset "$file"
|
||||
done
|
||||
Reference in new issue
Block a user