9
0
mirror of https://github.com/BX-Team/DivineMC.git synced 2025-12-19 14:59:25 +00:00

update release script and workflow (again)

This commit is contained in:
NONPLAYT
2025-12-14 15:31:20 +03:00
parent c74ca05912
commit 735220b6b7
2 changed files with 116 additions and 78 deletions

View File

@@ -1,76 +1,90 @@
#!/bin/bash
#!/usr/bin/env bash
set -e
prop() {
grep "${1}" gradle.properties | cut -d'=' -f2 | sed 's/\r//'
}
[ -z "$API_URL" ] && { echo "Error: API_URL not set"; exit 1; }
[ -z "$API_KEY" ] && { echo "Error: API_KEY not set"; exit 1; }
[ -z "$PROJECT_KEY" ] && { echo "Error: PROJECT_KEY not set"; exit 1; }
commitid=$(git log --pretty='%h' -1)
mcversion=$(prop mcVersion)
version=$(prop version)
experimental=$(prop experimental)
tagid="$mcversion-$BUILD_NUMBER-$commitid"
jarName="divinemc-$mcversion-$BUILD_NUMBER.jar"
divinemcid="DivineMC-$tagid"
MC_VERSION=$(prop mcVersion)
[ -z "$MC_VERSION" ] && { echo "Error: mcVersion not found in gradle.properties"; exit 1; }
channel=$([ "$experimental" = "true" ] && echo "BETA" || echo "STABLE")
VERSION_BRANCH=${VERSION_BRANCH:-"ver/$MC_VERSION"}
mv divinemc-server/build/libs/divinemc-paperclip-"$version"-mojmap.jar "$jarName"
if [ "$(prop experimental)" = "true" ]; then
CHANNEL="BETA"
else
CHANNEL="STABLE"
fi
echo "📦 Collecting commits..."
last_tag=$(git describe --tags --abbrev=0)
number=$(git log --oneline "$last_tag"..HEAD | wc -l)
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -n "$LAST_TAG" ]; then
COMMIT_COUNT=$(git log --oneline "$VERSION_BRANCH" ^"$LAST_TAG" 2>/dev/null | wc -l | tr -d ' ')
else
COMMIT_COUNT=$(git log --oneline "$VERSION_BRANCH" 2>/dev/null | wc -l | tr -d ' ')
fi
commits_json="["
first=true
if [ "$COMMIT_COUNT" -gt 0 ]; then
COMMITS_JSON="["
FIRST=true
while IFS= read -r sha && IFS= read -r message && IFS= read -r time; do
if [ "$FIRST" = true ]; then
FIRST=false
else
COMMITS_JSON="$COMMITS_JSON,"
fi
while IFS= read -r line; do
commit_sha=$(echo "$line" | awk '{print $1}')
commit_message=$(echo "$line" | cut -d' ' -f2-)
commit_time=$(git show -s --format=%cI "$commit_sha")
message=$(echo "$message" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\t/\\t/g' | tr -d '\n\r')
COMMITS_JSON="$COMMITS_JSON{\"sha\":\"$sha\",\"message\":\"$message\",\"time\":\"$time\"}"
done < <(git log --pretty='%H%n%s%n%cI' "$VERSION_BRANCH" -"$COMMIT_COUNT")
COMMITS_JSON="$COMMITS_JSON]"
else
COMMITS_JSON="[]"
fi
if [ "$first" = true ]; then
first=false
else
commits_json+=","
fi
METADATA=$(cat <<EOF
{
"buildNumber": ${BUILD_NUMBER},
"channel": "$CHANNEL",
"commits": $COMMITS_JSON
}
EOF
)
escaped_message=$(echo "$commit_message" | sed 's/"/\\"/g')
echo "Uploading: $PROJECT_KEY $MC_VERSION build #$BUILD_NUMBER ($CHANNEL)"
commits_json+="{\"sha\":\"$commit_sha\",\"message\":\"$escaped_message\",\"time\":\"$commit_time\"}"
done < <(git log --pretty='%h %s' "-$number")
UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" \
-X POST "$API_URL/v2/projects/$PROJECT_KEY/versions/$MC_VERSION/builds/upload" \
-H "x-api-key: $API_KEY" \
-F "file=@$JAR_FILE" \
-F "metadata=$METADATA")
commits_json+="]"
HTTP_BODY=$(echo "$UPLOAD_RESPONSE" | head -n -1)
HTTP_CODE=$(echo "$UPLOAD_RESPONSE" | tail -n 1)
metadata_json="{\"buildNumber\":$BUILD_NUMBER,\"channel\":\"$channel\",\"commits\":$commits_json}"
if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 201 ]; then
BUILD_ID=$(echo "$HTTP_BODY" | grep -o '"id":[0-9]*' | grep -o '[0-9]*')
echo "✓ Build #$BUILD_ID uploaded successfully"
exit 0
else
echo "✗ Upload failed (HTTP $HTTP_CODE)"
echo "$HTTP_BODY"
echo "$metadata_json" | jq . > metadata.json 2>/dev/null || echo "$metadata_json" > metadata.json
API_URL="https://api.bxteam.org/v2/projects/divinemc/versions/$mcversion/builds/upload"
API_KEY="${API_KEY:-}"
if [ -z "$API_KEY" ]; then
echo "❌ Error: API_KEY environment variable is not set"
exit 1
fi
echo ""
echo "🚀 Uploading build to API..."
echo " URL: $API_URL"
echo " File: $jarName"
echo " Build: $BUILD_NUMBER"
echo " Channel: $channel"
echo " Commits: $number"
response=$(curl -X POST "$API_URL" \
-H "Authorization: Bearer $API_KEY" \
-F "file=@$jarName" \
-F "metadata=$metadata_json" \
-w "\n%{http_code}" \
-s)
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$d')
echo ""
echo "📡 Response:"
echo "$response_body" | jq . 2>/dev/null || echo "$response_body"
if [ "$http_code" -ge 200 ] && [ "$http_code" -lt 300 ]; then
echo ""
echo "✅ Build uploaded successfully!"
echo " Build Number: $BUILD_NUMBER"
echo " Version: $mcversion"
echo " Channel: $channel"
else
echo ""
echo "❌ Upload failed with HTTP status: $http_code"
exit 1
fi