From fb3199b4a993be16804129111c0bb35cc39a70f3 Mon Sep 17 00:00:00 2001 From: Lena Date: Wed, 1 Jul 2026 00:00:00 +0000 Subject: ci: make release.sh idempotent Re-running on the same tag died creating a release that already existed. Reuse the tag's release when present, replace a stale asset before uploading, and parse API responses with python3 instead of sed. --- ci/release.sh | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'ci') diff --git a/ci/release.sh b/ci/release.sh index cec2b42..260efba 100755 --- a/ci/release.sh +++ b/ci/release.sh @@ -19,18 +19,33 @@ apk=app/build/outputs/apk/release/app-release.apk [ -f "$apk" ] || { echo "release: $apk not found; run ci/build.sh first" >&2; exit 1; } api="$base/api/v1/repos/$CODEBERG_REPO" +auth="Authorization: token $CODEBERG_TOKEN" +name="rsend-$tag.apk" -# Create the release for the tag and capture its id. -id=$(curl -fsS -X POST "$api/releases" \ - -H "Authorization: token $CODEBERG_TOKEN" \ - -H "Content-Type: application/json" \ - -d "{\"tag_name\":\"$tag\",\"name\":\"$tag\"}" \ - | sed -n 's/.*"id":\([0-9][0-9]*\).*/\1/p' | head -1) +# Reuse the tag's release if it exists, else create it. Re-runs are idempotent: +# same release, and a stale asset from an earlier run is replaced below. +id=$(curl -fsS -H "$auth" "$api/releases/tags/$tag" 2>/dev/null \ + | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])' 2>/dev/null) || id= +if [ -n "$id" ]; then + curl -fsS -H "$auth" "$api/releases/$id/assets" \ + | python3 -c 'import json,sys +for a in json.load(sys.stdin): + if a["name"] == sys.argv[1]: + print(a["id"])' "$name" \ + | while read -r aid; do + curl -fsS -X DELETE -H "$auth" "$api/releases/$id/assets/$aid" + done +else + id=$(curl -fsS -X POST "$api/releases" \ + -H "$auth" -H "Content-Type: application/json" \ + -d "{\"tag_name\":\"$tag\",\"name\":\"$tag\"}" \ + | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])') +fi [ -n "$id" ] || { echo "release: could not create release for $tag" >&2; exit 1; } # Upload the APK as a release asset. -curl -fsS -X POST "$api/releases/$id/assets?name=rsend-$tag.apk" \ - -H "Authorization: token $CODEBERG_TOKEN" \ +curl -fsS -X POST "$api/releases/$id/assets?name=$name" \ + -H "$auth" \ -F "attachment=@$apk" >/dev/null -echo "release: uploaded rsend-$tag.apk to $CODEBERG_REPO ($tag)" +echo "release: uploaded $name to $CODEBERG_REPO ($tag)" -- cgit v1.2.3