#!/usr/bin/env bash # Notify the ops Telegram group after an admin (backoffice) production deploy. # # Usage: # ./scripts/notify-deploy.sh # DEPLOY_STATUS=failed ./scripts/notify-deploy.sh # # Optional env: # DEPLOY_SHA git sha (default: GITHUB_SHA or HEAD) # DEPLOY_STATUS success | failed set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" NOTIFY="$SCRIPT_DIR/notify-ops-telegram.sh" if [[ ! -x "$NOTIFY" ]]; then echo "[ghabilee-admin-deploy-notify] skipped: missing $NOTIFY" >&2 exit 0 fi STATUS="${DEPLOY_STATUS:-success}" RAW_SHA="${DEPLOY_SHA:-${GITHUB_SHA:-}}" if [[ -z "$RAW_SHA" ]] && command -v git >/dev/null 2>&1; then RAW_SHA="$(git rev-parse HEAD 2>/dev/null || true)" fi SHORT_SHA="${RAW_SHA:0:7}" if [[ -z "$SHORT_SHA" ]]; then SHORT_SHA="unknown" fi VERSION="${DEPLOY_VERSION:-}" if [[ -z "$VERSION" || "$VERSION" == "?" ]]; then if [[ -f package.json ]]; then VERSION="$( python3 -c 'import json,sys; print(json.load(open("package.json")).get("version") or "?")' \ 2>/dev/null || echo "?" )" else VERSION="?" fi fi SUBJECT="${DEPLOY_COMMIT_SUBJECT:-}" if [[ -z "$SUBJECT" ]] && command -v git >/dev/null 2>&1; then SUBJECT="$(git log -1 --format=%s 2>/dev/null || true)" fi # فقط عنوان commit — body چندخطی نباید در پیام بیاید if [[ -n "$SUBJECT" ]]; then SUBJECT="${SUBJECT%%$'\n'*}" SUBJECT="${SUBJECT%%$'\r'*}" fi if [[ ${#SUBJECT} -gt 100 ]]; then SUBJECT="${SUBJECT:0:97}..." fi MESSAGE="$( STATUS="$STATUS" VERSION="$VERSION" SHORT_SHA="$SHORT_SHA" SUBJECT="$SUBJECT" python3 - <<'PY' import os from datetime import datetime from zoneinfo import ZoneInfo def fa(s: str) -> str: return s.translate(str.maketrans("0123456789", "۰۱۲۳۴۵۶۷۸۹")) status = os.environ.get("STATUS", "success") version = os.environ.get("VERSION", "?") sha = os.environ.get("SHORT_SHA", "?") subject = os.environ.get("SUBJECT", "").strip() now = datetime.now(ZoneInfo("Asia/Tehran")) time_line = fa(now.strftime("%H:%M")) lines: list[str] = [] if status == "failed": lines.append("🛠 پنل ادمین قبیله گیر کرد — CI یه بار دیگه؟") else: lines.append("🛡 پنل ادمین قبیله به‌روز شد") lines.append("") lines.append(f"🕐 {time_line} · نسخه {fa(version)} · {sha}") if subject: lines.append(f"📝 {subject}") if status != "failed": lines.append("🏠 https://backoffice.ghabilee.ir") print("\n".join(lines)) PY )" printf '%s' "$MESSAGE" | "$NOTIFY"