#!/bin/bash # Deploy script: build release APK and upload to Bitrix server # Usage: ./deploy.sh [--mandatory] # # Requires BITRIX_WEBHOOK_URL environment variable, e.g.: # export BITRIX_WEBHOOK_URL="https://bitrix.powerhousegym.ru/rest/1/abc123xyz/" # Create webhook in Bitrix: Applications → Webhooks → Inbound webhook set -e SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" cd "$SCRIPT_DIR" export ANDROID_HOME="${ANDROID_HOME:-D:/android-sdk}" # Check webhook URL if [ -z "$BITRIX_WEBHOOK_URL" ]; then echo "BITRIX_WEBHOOK_URL is not set." echo "Create an inbound webhook in Bitrix24 (Applications → Webhooks)" echo "Then: export BITRIX_WEBHOOK_URL=\"https://bitrix.powerhousegym.ru/rest/1/your-token/\"" exit 1 fi # Read version from build.gradle.kts VERSION_CODE=$(grep 'versionCode' app/build.gradle.kts | head -1 | grep -o '[0-9]*') VERSION_NAME=$(grep 'versionName' app/build.gradle.kts | head -1 | grep -o '"[^"]*"' | tr -d '"') # Server URL SERVER_URL=$(echo "$BITRIX_WEBHOOK_URL" | grep -o 'https://[^/]*') # Extract webhook token from URL (last path segment before trailing slash) WEBHOOK_TOKEN=$(echo "$BITRIX_WEBHOOK_URL" | grep -oP 'rest/\d+/\K[^/]+') WEBHOOK_USER=$(echo "$BITRIX_WEBHOOK_URL" | grep -oP 'rest/\K\d+') MANDATORY="false" if [ "$1" = "--mandatory" ]; then MANDATORY="true" fi echo "=== ContactSync Deploy ===" echo " Version: $VERSION_NAME ($VERSION_CODE)" echo " Server: $SERVER_URL" echo " Webhook: user=$WEBHOOK_USER" echo " Mandatory: $MANDATORY" echo "" # Step 1: Build release APK echo "[1/3] Building release APK..." ./gradlew.bat assembleRelease --no-daemon -q APK_PATH="app/build/outputs/apk/release/app-release.apk" if [ ! -f "$APK_PATH" ]; then echo "ERROR: APK not found at $APK_PATH" exit 1 fi APK_SIZE=$(ls -lh "$APK_PATH" | awk '{print $5}') echo " APK built: $APK_SIZE" # Step 2: Upload to server echo "[2/3] Uploading to $SERVER_URL..." UPLOAD_URL="${SERVER_URL}/local/modules/contact.sync/api/upload_apk.php" RESPONSE=$(curl -s -X POST "$UPLOAD_URL" \ -F "apk=@$APK_PATH" \ -F "version_code=$VERSION_CODE" \ -F "version_name=$VERSION_NAME" \ -F "mandatory=$MANDATORY" \ -F "webhook_user=$WEBHOOK_USER" \ -F "webhook_token=$WEBHOOK_TOKEN") echo " Server response: $RESPONSE" # Check success SUCCESS=$(echo "$RESPONSE" | grep -o '"success":true' || echo "") if [ -z "$SUCCESS" ]; then echo "ERROR: Upload failed!" exit 1 fi # Step 3: Verify echo "[3/3] Verifying..." VERIFY=$(curl -s "${SERVER_URL}/local/modules/contact.sync/api/version.php") echo " Version on server: $VERIFY" echo "" echo "=== Deploy complete ===" echo " All devices will see the update on next app open."