Deeplearning/Scripts/publish_to_gitea.sh

146 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# One-command publish flow:
# 1) Build conda package
# 2) Commit current changes (if any)
# 3) Tag as v<version>
# 4) Push branch + tag to origin (your Gitea remote)
# 5) Optionally create Gitea Release and upload built package when GITEA_TOKEN is set
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT_DIR"
if ! command -v python >/dev/null 2>&1; then
echo "python not found in PATH"
exit 1
fi
if ! command -v conda >/dev/null 2>&1; then
echo "conda not found in PATH"
exit 1
fi
if ! command -v conda-build >/dev/null 2>&1; then
echo "conda-build is not installed in current environment."
echo "Run: conda install -n Deeplearning -y conda-build"
exit 1
fi
VERSION="$(python - <<'PY'
import pathlib
import re
text = pathlib.Path('pyproject.toml').read_text(encoding='utf-8')
m = re.search(r'^version\s*=\s*"([^"]+)"', text, flags=re.M)
if not m:
raise SystemExit('Could not read version from pyproject.toml')
print(m.group(1))
PY
)"
TAG="v${VERSION}"
echo "Building conda package for version: ${VERSION}"
PKG_PATH="$(conda-build conda_env/recipe --output)"
conda-build conda_env/recipe
if git diff --quiet && git diff --cached --quiet; then
echo "No local file changes to commit."
else
git add pyproject.toml conda_env/recipe/meta.yaml conda_env/recipe/README.md Scripts/publish_to_gitea.sh README.md
git commit -m "release: ${VERSION}" || true
fi
if git rev-parse "${TAG}" >/dev/null 2>&1; then
echo "Tag ${TAG} already exists locally."
else
git tag -a "${TAG}" -m "Release ${TAG}"
fi
echo "Pushing current branch and tag to origin"
git push origin HEAD
git push origin "${TAG}"
echo "Pushed to origin."
if [[ -z "${GITEA_TOKEN:-}" ]]; then
echo "GITEA_TOKEN not set. Skip creating Gitea Release."
echo "Built package: ${PKG_PATH}"
exit 0
fi
ORIGIN_URL="$(git remote get-url origin)"
parse_owner_repo() {
local url="$1"
if [[ "$url" =~ ^git@([^:]+):(.+)\.git$ ]]; then
echo "${BASH_REMATCH[1]}|${BASH_REMATCH[2]}"
return
fi
if [[ "$url" =~ ^https?://([^/]+)/(.+)\.git$ ]]; then
echo "${BASH_REMATCH[1]}|${BASH_REMATCH[2]}"
return
fi
echo ""
}
PARSED="$(parse_owner_repo "$ORIGIN_URL")"
if [[ -z "$PARSED" ]]; then
echo "Cannot parse origin URL: $ORIGIN_URL"
exit 1
fi
GITEA_HOST="${PARSED%%|*}"
OWNER_REPO="${PARSED#*|}"
API_BASE="https://${GITEA_HOST}/api/v1/repos/${OWNER_REPO}"
echo "Creating or updating Gitea release ${TAG} on ${OWNER_REPO}"
RELEASE_JSON="$(curl -sS -X POST "${API_BASE}/releases" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"tag_name\":\"${TAG}\",\"name\":\"${TAG}\",\"draft\":false,\"prerelease\":false}" || true)"
RELEASE_ID="$(printf '%s' "$RELEASE_JSON" | python - <<'PY'
import json,sys
text=sys.stdin.read().strip()
if not text:
print('')
raise SystemExit
try:
obj=json.loads(text)
except Exception:
print('')
raise SystemExit
print(obj.get('id',''))
PY
)"
if [[ -z "$RELEASE_ID" ]]; then
RELEASE_ID="$(curl -sS -H "Authorization: token ${GITEA_TOKEN}" "${API_BASE}/releases/tags/${TAG}" | python - <<'PY'
import json,sys
text=sys.stdin.read().strip()
try:
obj=json.loads(text)
except Exception:
print('')
raise SystemExit
print(obj.get('id',''))
PY
)"
fi
if [[ -z "$RELEASE_ID" ]]; then
echo "Failed to get release id for tag ${TAG}"
exit 1
fi
PKG_FILE="$(basename "$PKG_PATH")"
echo "Uploading asset ${PKG_FILE}"
curl -sS -X POST "${API_BASE}/releases/${RELEASE_ID}/assets?name=${PKG_FILE}" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/octet-stream" \
--data-binary "@${PKG_PATH}" >/dev/null
echo "Release published: ${TAG}"
echo "Asset uploaded: ${PKG_FILE}"