feat(self-update): resolve latest via Forgejo/gitea releases API
This commit is contained in:
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
# Publica dist/duckbrain.phar (+ su sha256) como assets de la release `vX.Y.Z`
|
||||
# en Forgejo, usando la version del instalador (Config::VERSION) como tag.
|
||||
# self-update detecta la nueva version eligiendo el mayor semver de la lista
|
||||
# de releases; no se necesita ningun tag magico `latest`.
|
||||
# Requiere FORGEJO_TOKEN (token de la API con scope write:repository).
|
||||
# Variables: FORGEJO_URL, FORGEJO_REPO, DUCKBRAIN_DIST.
|
||||
set -euo pipefail
|
||||
|
||||
: "${FORGEJO_TOKEN:?Define FORGEJO_TOKEN (token de la API de Forgejo con write:repository)}"
|
||||
|
||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
OUT="${DUCKBRAIN_DIST:-$ROOT/dist}"
|
||||
FORGEJO_URL="${FORGEJO_URL:-https://git.kj2.me}"
|
||||
FORGEJO_REPO="${FORGEJO_REPO:-kj/duckbrain-toolbox}"
|
||||
API="$FORGEJO_URL/api/v1/repos/$FORGEJO_REPO"
|
||||
|
||||
VERSION="$(grep -oP "const VERSION = '\K[^']+" "$ROOT/packages/commands/src/Toolbox/Config.php")"
|
||||
TAG="v$VERSION"
|
||||
|
||||
if [ ! -f "$OUT/duckbrain.phar" ] || [ ! -f "$OUT/duckbrain.phar.sha256" ]; then
|
||||
"$ROOT/scripts/build-phar"
|
||||
fi
|
||||
|
||||
# El tag apunta al commit actual (de donde se construye el phar).
|
||||
if ! git -C "$ROOT" rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
|
||||
git -C "$ROOT" tag -a "$TAG" -m "duckbrain-toolbox $TAG"
|
||||
fi
|
||||
git -C "$ROOT" push -q origin "refs/tags/$TAG"
|
||||
|
||||
body="$(mktemp)"
|
||||
trap 'rm -f "$body"' EXIT
|
||||
|
||||
code="$(curl -s -o "$body" -w '%{http_code}' -X POST "$API/releases" \
|
||||
-H "Authorization: token $FORGEJO_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d "{\"tag_name\":\"$TAG\",\"name\":\"$TAG\",\"draft\":false,\"prerelease\":false}")"
|
||||
|
||||
if [ "$code" = 409 ] || [ "$code" = 422 ]; then
|
||||
code="$(curl -s -o "$body" -w '%{http_code}' -H "Authorization: token $FORGEJO_TOKEN" "$API/releases/tags/$TAG")"
|
||||
fi
|
||||
|
||||
if [ "$code" != 200 ] && [ "$code" != 201 ]; then
|
||||
echo "Forgejo respondio HTTP $code al crear/leer la release $TAG:" >&2
|
||||
cat "$body" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
id="$(jq -r '.id // empty' "$body")"
|
||||
if [ -z "$id" ]; then
|
||||
echo "La respuesta no trae id de release:" >&2
|
||||
cat "$body" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Idempotente: quita assets previos con los nombres que se van a subir.
|
||||
for a in $(jq -r '.assets[]? | select(.name == "duckbrain.phar" or .name == "duckbrain.phar.sha256") | .id' "$body"); do
|
||||
curl -s -o /dev/null -X DELETE -H "Authorization: token $FORGEJO_TOKEN" "$API/releases/assets/$a"
|
||||
done
|
||||
|
||||
for f in duckbrain.phar duckbrain.phar.sha256; do
|
||||
resp="$(mktemp)"
|
||||
if ! curl -s -f -o "$resp" -X POST "$API/releases/$id/assets?name=$f" \
|
||||
-H "Authorization: token $FORGEJO_TOKEN" -F "attachment=@$OUT/$f"; then
|
||||
echo "Fallo subiendo $f:" >&2
|
||||
cat "$resp" >&2
|
||||
exit 1
|
||||
fi
|
||||
rm -f "$resp"
|
||||
done
|
||||
|
||||
echo "OK: release $TAG publicada con duckbrain.phar + duckbrain.phar.sha256"
|
||||
echo " $FORGEJO_URL/$FORGEJO_REPO/releases/tag/$TAG"
|
||||
Reference in New Issue
Block a user