Files
proyectosacc-mirror/scripts/telegram-pipeline-notify.sh
T
Evert Daniel Romero Garrido 0c0126f3de feat(pipeline): implementa notificaciones enriquecidas de Telegram para IT-238
- Crea scripts/telegram-pipeline-notify.sh con detalles de branch, commit, build y autor
- Actualiza bitbucket-pipelines.yml para usar notificaciones locales en todos los ambientes
- Corrige bug donde 01_image-setup referenciaba ci-cd-commons antes de clonarlo
- Usa formato MarkdownV2 para mensajes en Telegram
2026-04-15 16:07:54 -06:00

110 lines
3.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ===============================================================================================================
# telegram-pipeline-notify.sh - Notificaciones enriquecidas de Telegram para pipelines de Bitbucket
# Descripción:
# Envía alertas detalladas a Telegram incluyendo branch, commit, build number,
# autor y estado del despliegue. Requiere TELEGRAM_BOT_TOKEN y TELEGRAM_CHAT_ID.
#
# Uso:
# bash scripts/telegram-pipeline-notify.sh <start|success|failure> [mensaje_extra]
#
# Autor: Área de Tecnología y Desarrollo - CCsoft
# ===============================================================================================================
set -euo pipefail
NOTIFY_TYPE="${1:-}"
EXTRA_MESSAGE="${2:-}"
TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN:-${DEV_TELEGRAM_BOT_TOKEN:-${PROD_TELEGRAM_BOT_TOKEN:-}}}"
TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID:-${DEV_TELEGRAM_CHAT_ID:-${PROD_TELEGRAM_CHAT_ID:-}}}"
if [[ -z "$TELEGRAM_BOT_TOKEN" ]]; then
echo "[WARN] TELEGRAM_BOT_TOKEN no definido. Saltando notificación." >&2
exit 0
fi
if [[ -z "$TELEGRAM_CHAT_ID" ]]; then
echo "[WARN] TELEGRAM_CHAT_ID no definido. Saltando notificación." >&2
exit 0
fi
if [[ -z "$NOTIFY_TYPE" ]]; then
echo "[ERROR] Debe proporcionar el tipo de notificación: start, success o failure" >&2
exit 1
fi
# -------------------------------------------------------------------------------
# Variables de contexto del pipeline
# -------------------------------------------------------------------------------
BRANCH="${BITBUCKET_BRANCH:-unknown}"
COMMIT="${BITBUCKET_COMMIT:-unknown}"
BUILD_NUMBER="${BITBUCKET_BUILD_NUMBER:-unknown}"
REPO="${BITBUCKET_REPO_SLUG:-proyectosacc}"
WORKSPACE="${BITBUCKET_WORKSPACE:-ccsoft1}"
AUTHOR="${BITBUCKET_STEP_TRIGGERER_UUID:-desconocido}"
# Truncar commit a 7 caracteres si es largo
if [[ "${#COMMIT}" -gt 7 ]]; then
COMMIT_SHORT="${COMMIT:0:7}"
else
COMMIT_SHORT="$COMMIT"
fi
# Timestamp actual
TIMESTAMP="$(date '+%Y-%m-%d %H:%M:%S %Z')"
# -------------------------------------------------------------------------------
# Armar mensaje según tipo
# -------------------------------------------------------------------------------
case "$NOTIFY_TYPE" in
start)
EMOJI="🚀"
STATUS="INICIANDO"
;;
success)
EMOJI="✅"
STATUS="COMPLETADO EXITOSAMENTE"
;;
failure)
EMOJI="❌"
STATUS="FALLÓ"
;;
*)
EMOJI="️"
STATUS="$NOTIFY_TYPE"
;;
esac
MESSAGE=$(cat <<EOF
${EMOJI} Pipeline *${REPO}* ${STATUS}
*Branch:* \`${BRANCH}\`
*Commit:* \`${COMMIT_SHORT}\`
*Build:* #${BUILD_NUMBER}
*Autor:* ${AUTHOR}
*Hora:* ${TIMESTAMP}
EOF
)
if [[ -n "$EXTRA_MESSAGE" ]]; then
MESSAGE="${MESSAGE}
*Detalle:* ${EXTRA_MESSAGE}"
fi
# -------------------------------------------------------------------------------
# Enviar mensaje
# -------------------------------------------------------------------------------
RESPONSE=$(curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d "chat_id=${TELEGRAM_CHAT_ID}" \
--data-urlencode "text=${MESSAGE}" \
-d "parse_mode=MarkdownV2")
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo "[INFO] Notificación enviada a Telegram"
else
echo "[WARN] Error enviando notificación: $RESPONSE" >&2
# No fallamos el pipeline por un error de notificación
exit 0
fi