26 lines
1.5 KiB
Bash
Executable File
26 lines
1.5 KiB
Bash
Executable File
#!/bin/sh
|
||
# Attach files to this tag's release on Gitea (made if it isn't there yet).
|
||
# deploy/ci/upload-release.sh <tag> <file>…
|
||
# Uses GITHUB_SERVER_URL, GITHUB_REPOSITORY and RELEASE_TOKEN (set by the workflow).
|
||
set -e
|
||
TAG=${1:?tag}; shift
|
||
API="$GITHUB_SERVER_URL/api/v1/repos/$GITHUB_REPOSITORY"
|
||
AUTH="Authorization: token $RELEASE_TOKEN"
|
||
json() { python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('$1','') if isinstance(d,dict) else '')"; }
|
||
ID=$(curl -fsS -H "$AUTH" "$API/releases/tags/$TAG" 2>/dev/null | json id || true)
|
||
if [ -z "$ID" ]; then
|
||
# Two jobs may get here at once: whoever is second finds the first one's release.
|
||
ID=$(curl -fsS -H "$AUTH" -H "Content-Type: application/json" -X POST "$API/releases" \
|
||
-d "{\"tag_name\":\"$TAG\",\"name\":\"AnyBot $TAG\",\"body\":\"自動建置:macOS、Ubuntu、沒有螢幕的 Linux、伺服器。\"}" | json id) \
|
||
|| ID=$(curl -fsS -H "$AUTH" "$API/releases/tags/$TAG" | json id)
|
||
fi
|
||
[ -n "$ID" ] || { echo "找不到也建不了 $TAG 的 release" >&2; exit 1; }
|
||
for f in "$@"; do
|
||
name=$(basename "$f")
|
||
# Built again: replace the old file.
|
||
old=$(curl -fsS -H "$AUTH" "$API/releases/$ID/assets" | python3 -c "import json,sys; print(next((str(a['id']) for a in json.load(sys.stdin) if a['name']=='$name'),''))")
|
||
[ -n "$old" ] && curl -fsS -H "$AUTH" -X DELETE "$API/releases/$ID/assets/$old" >/dev/null
|
||
curl -fsS -H "$AUTH" -F "attachment=@$f" "$API/releases/$ID/assets?name=$name" >/dev/null
|
||
echo "已上傳 $name"
|
||
done
|