238 lines
6.4 KiB
Bash
238 lines
6.4 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# ============================================================
|
|
# Lobby Systems - Gitea Release Script
|
|
# Linux / macOS
|
|
# ============================================================
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cd "$SCRIPT_DIR" || exit 1
|
|
|
|
GITEA_BASE_URL="${GITEA_BASE_URL:-https://git.darto.id}"
|
|
GITEA_OWNER="${GITEA_OWNER:-dartokloning}"
|
|
GITEA_REPO="${GITEA_REPO:-lobby-systems}"
|
|
GRADLE_PROPERTIES="$SCRIPT_DIR/gradle.properties"
|
|
|
|
info() { echo "[INFO] $1"; }
|
|
ok() { echo "[OK] $1"; }
|
|
warn() { echo "[WARN] $1"; }
|
|
error() { echo "[ERROR] $1"; }
|
|
separator() { echo "============================================"; }
|
|
read_property() { grep -E "^${1}=" "$GRADLE_PROPERTIES" | head -n 1 | cut -d'=' -f2- | tr -d '\r'; }
|
|
|
|
MOD_VERSION="$(read_property mod_version)"
|
|
MINECRAFT_VERSION="$(read_property minecraft_version)"
|
|
TAG="v$MOD_VERSION"
|
|
JAR_PATH="$SCRIPT_DIR/build/libs/lobby-systems-$MOD_VERSION.jar"
|
|
JAR_NAME="lobby-systems-$MOD_VERSION.jar"
|
|
|
|
separator
|
|
echo " Lobby Systems - Release $TAG"
|
|
echo " Minecraft: $MINECRAFT_VERSION"
|
|
echo " Gitea: $GITEA_BASE_URL/$GITEA_OWNER/$GITEA_REPO"
|
|
separator
|
|
|
|
echo
|
|
info "Menjalankan build terlebih dahulu..."
|
|
"$SCRIPT_DIR/compile.sh" --build-only
|
|
BUILD_RESULT=$?
|
|
|
|
if [[ $BUILD_RESULT -ne 0 ]]; then
|
|
error "Release dibatalkan karena build gagal."
|
|
exit "$BUILD_RESULT"
|
|
fi
|
|
|
|
if [[ ! -f "$JAR_PATH" ]]; then
|
|
error "JAR release tidak ditemukan: $JAR_PATH"
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v git >/dev/null 2>&1; then
|
|
error "Git tidak ditemukan di PATH."
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v curl >/dev/null 2>&1; then
|
|
error "curl tidak ditemukan di PATH."
|
|
exit 1
|
|
fi
|
|
|
|
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
error "Folder ini bukan Git repository."
|
|
exit 1
|
|
fi
|
|
|
|
CURRENT_BRANCH="$(git branch --show-current)"
|
|
if [[ -z "$CURRENT_BRANCH" ]]; then
|
|
error "Tidak dapat menentukan branch Git aktif."
|
|
exit 1
|
|
fi
|
|
|
|
if ! git remote get-url origin >/dev/null 2>&1; then
|
|
error "Remote 'origin' belum dikonfigurasi."
|
|
exit 1
|
|
fi
|
|
|
|
echo
|
|
separator
|
|
echo " Git Release Preparation"
|
|
separator
|
|
|
|
CHANGES="$(git status --porcelain)"
|
|
if [[ -n "$CHANGES" ]]; then
|
|
echo
|
|
echo "Perubahan source belum di-commit:"
|
|
git status --short
|
|
echo
|
|
read -r -p "Commit dan push perubahan sebelum membuat release? [Y/N]: " SYNC_CONFIRM
|
|
case "$SYNC_CONFIRM" in
|
|
y|Y|yes|YES|Yes) ;;
|
|
*)
|
|
warn "Release dibatalkan. Source harus bersih agar tag sesuai dengan build."
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
read -r -p "Commit message [Prepare Lobby Systems $TAG release]: " COMMIT_MESSAGE
|
|
[[ -z "$COMMIT_MESSAGE" ]] && COMMIT_MESSAGE="Prepare Lobby Systems $TAG release"
|
|
|
|
git add -A || exit 2
|
|
git commit -m "$COMMIT_MESSAGE" || exit 2
|
|
fi
|
|
|
|
info "Push branch $CURRENT_BRANCH ke origin..."
|
|
if ! git push origin "$CURRENT_BRANCH"; then
|
|
error "Push branch gagal. Release dibatalkan."
|
|
exit 2
|
|
fi
|
|
|
|
HEAD_COMMIT="$(git rev-parse HEAD)"
|
|
|
|
if git rev-parse "$TAG" >/dev/null 2>&1; then
|
|
TAG_COMMIT="$(git rev-list -n 1 "$TAG")"
|
|
if [[ "$TAG_COMMIT" != "$HEAD_COMMIT" ]]; then
|
|
error "Tag $TAG sudah ada tetapi menunjuk commit lain."
|
|
echo "Tag : $TAG_COMMIT"
|
|
echo "HEAD: $HEAD_COMMIT"
|
|
echo "Naikkan mod_version sebelum membuat release baru."
|
|
exit 2
|
|
fi
|
|
info "Tag lokal $TAG sudah ada pada commit saat ini."
|
|
else
|
|
info "Membuat tag $TAG..."
|
|
git tag -a "$TAG" -m "Lobby Systems $TAG" || exit 2
|
|
fi
|
|
|
|
if git ls-remote --exit-code --tags origin "refs/tags/$TAG" >/dev/null 2>&1; then
|
|
info "Tag $TAG sudah ada di origin."
|
|
else
|
|
info "Push tag $TAG ke origin..."
|
|
git push origin "$TAG" || exit 2
|
|
fi
|
|
|
|
echo
|
|
separator
|
|
echo " Gitea Release"
|
|
separator
|
|
|
|
read -r -p "Buat Release $TAG dan upload $JAR_NAME? [Y/N]: " RELEASE_CONFIRM
|
|
case "$RELEASE_CONFIRM" in
|
|
y|Y|yes|YES|Yes) ;;
|
|
*)
|
|
info "Pembuatan Gitea Release dilewati. Tag $TAG tetap sudah tersimpan."
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
if [[ -z "${GITEA_TOKEN:-}" ]]; then
|
|
echo
|
|
read -r -s -p "Gitea Personal Access Token: " GITEA_TOKEN
|
|
echo
|
|
fi
|
|
|
|
if [[ -z "$GITEA_TOKEN" ]]; then
|
|
error "Token Gitea kosong."
|
|
exit 2
|
|
fi
|
|
|
|
API_BASE="$GITEA_BASE_URL/api/v1/repos/$GITEA_OWNER/$GITEA_REPO"
|
|
TMP_DIR="$(mktemp -d)"
|
|
trap 'rm -rf "$TMP_DIR"' EXIT
|
|
RELEASE_JSON="$TMP_DIR/release.json"
|
|
CREATE_JSON="$TMP_DIR/create.json"
|
|
ASSET_JSON="$TMP_DIR/asset.json"
|
|
|
|
info "Memeriksa apakah Release $TAG sudah ada..."
|
|
HTTP_CODE="$(curl -sS -o "$RELEASE_JSON" -w '%{http_code}' \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
"$API_BASE/releases/tags/$TAG")"
|
|
|
|
if [[ "$HTTP_CODE" == "200" ]]; then
|
|
info "Release $TAG sudah ada. Akan menggunakan release tersebut."
|
|
elif [[ "$HTTP_CODE" == "404" ]]; then
|
|
cat > "$CREATE_JSON" <<JSON
|
|
{
|
|
"tag_name": "$TAG",
|
|
"target_commitish": "$CURRENT_BRANCH",
|
|
"name": "Lobby Systems $TAG",
|
|
"body": "Lobby Systems $TAG untuk Minecraft $MINECRAFT_VERSION.",
|
|
"draft": false,
|
|
"prerelease": false
|
|
}
|
|
JSON
|
|
|
|
info "Membuat Release $TAG di Gitea..."
|
|
HTTP_CODE="$(curl -sS -o "$RELEASE_JSON" -w '%{http_code}' \
|
|
-X POST \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
--data-binary "@$CREATE_JSON" \
|
|
"$API_BASE/releases")"
|
|
|
|
if [[ "$HTTP_CODE" != "201" ]]; then
|
|
error "Gitea gagal membuat release. HTTP $HTTP_CODE"
|
|
cat "$RELEASE_JSON"
|
|
exit 2
|
|
fi
|
|
else
|
|
error "Gagal memeriksa release. HTTP $HTTP_CODE"
|
|
cat "$RELEASE_JSON"
|
|
exit 2
|
|
fi
|
|
|
|
RELEASE_ID="$(grep -oE '"id"[[:space:]]*:[[:space:]]*[0-9]+' "$RELEASE_JSON" | head -n 1 | grep -oE '[0-9]+')"
|
|
|
|
if [[ -z "$RELEASE_ID" ]]; then
|
|
error "Tidak dapat membaca release ID dari response Gitea."
|
|
cat "$RELEASE_JSON"
|
|
exit 2
|
|
fi
|
|
|
|
info "Release ID: $RELEASE_ID"
|
|
info "Mengupload $JAR_NAME..."
|
|
|
|
ASSET_CODE="$(curl -sS -o "$ASSET_JSON" -w '%{http_code}' \
|
|
-X POST \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-F "attachment=@$JAR_PATH" \
|
|
"$API_BASE/releases/$RELEASE_ID/assets?name=$JAR_NAME")"
|
|
|
|
if [[ "$ASSET_CODE" == "201" ]]; then
|
|
echo
|
|
ok "Release $TAG berhasil dibuat dan JAR berhasil diupload."
|
|
elif [[ "$ASSET_CODE" == "409" ]]; then
|
|
echo
|
|
warn "Release sudah ada dan asset $JAR_NAME tampaknya sudah terupload."
|
|
else
|
|
error "Upload JAR gagal. HTTP $ASSET_CODE"
|
|
cat "$ASSET_JSON"
|
|
exit 2
|
|
fi
|
|
|
|
echo
|
|
separator
|
|
echo " RELEASE SUCCESS"
|
|
echo " Tag: $TAG"
|
|
echo " Asset: $JAR_NAME"
|
|
separator
|