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
This commit is contained in:
Evert Daniel Romero Garrido
2026-04-15 16:07:54 -06:00
parent 3e215f866f
commit 0c0126f3de
2 changed files with 131 additions and 18 deletions
+22 -18
View File
@@ -14,21 +14,21 @@ options:
audiences: audiences:
- sts.amazonaws.com - sts.amazonaws.com
definitions: definitions:
steps: steps:
- step: &notify-start - step: &notify-start
name: Notify Start name: Notify Start
script: script:
- export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}" - export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}"
- export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}" - export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}"
- bash ci-cd-commons/telegram_alert.sh "🚀 Iniciando pipeline de proyectosacc (${BITBUCKET_BRANCH})" - bash scripts/telegram-pipeline-notify.sh start
- step: &notify-fail - step: &notify-fail
name: Notify Failure name: Notify Failure
script: script:
- export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}" - export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}"
- export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}" - export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}"
- bash ci-cd-commons/telegram_alert.sh "❌ Pipeline de proyectosacc falló en el paso ${BITBUCKET_STEP_KEY}" - bash scripts/telegram-pipeline-notify.sh failure "Paso: ${BITBUCKET_STEP_KEY}"
pipelines: pipelines:
default: default:
@@ -54,7 +54,7 @@ pipelines:
- ssh-keyscan -p "${DEV_SSH_PORT_PROYECTOSACC:-22}" "${DEV_SERVER_IP_PROYECTOSACC}" >> ~/.ssh/known_hosts 2>/dev/null || true - ssh-keyscan -p "${DEV_SSH_PORT_PROYECTOSACC:-22}" "${DEV_SERVER_IP_PROYECTOSACC}" >> ~/.ssh/known_hosts 2>/dev/null || true
- export TELEGRAM_BOT_TOKEN="${DEV_TELEGRAM_BOT_TOKEN}" - export TELEGRAM_BOT_TOKEN="${DEV_TELEGRAM_BOT_TOKEN}"
- export TELEGRAM_CHAT_ID="${DEV_TELEGRAM_CHAT_ID}" - export TELEGRAM_CHAT_ID="${DEV_TELEGRAM_CHAT_ID}"
- bash ci-cd-commons/telegram_alert.sh "🚀 Iniciando pipeline DEV de proyectosacc" - bash scripts/telegram-pipeline-notify.sh start
- step: - step:
name: 02_repo-config name: 02_repo-config
@@ -132,7 +132,9 @@ pipelines:
"bash /home/thoth/deploy/setup/deploy.sh" "bash /home/thoth/deploy/setup/deploy.sh"
- export CLOUDFRONT_DISTRIBUTION_ID=$(python3 -c "import json; print(json.load(open('terraform/terraform-outputs.json'))['cloudfront_distribution_id']['value'])") - export CLOUDFRONT_DISTRIBUTION_ID=$(python3 -c "import json; print(json.load(open('terraform/terraform-outputs.json'))['cloudfront_distribution_id']['value'])")
- aws cloudfront create-invalidation --distribution-id "${CLOUDFRONT_DISTRIBUTION_ID}" --paths "/*" - aws cloudfront create-invalidation --distribution-id "${CLOUDFRONT_DISTRIBUTION_ID}" --paths "/*"
- bash ci-cd-commons/telegram_alert.sh "✅ Deploy DEV de proyectosacc completado exitosamente" - export TELEGRAM_BOT_TOKEN="${DEV_TELEGRAM_BOT_TOKEN}"
- export TELEGRAM_CHAT_ID="${DEV_TELEGRAM_CHAT_ID}"
- bash scripts/telegram-pipeline-notify.sh success "CloudFront invalidado"
master: master:
- step: - step:
@@ -146,7 +148,7 @@ pipelines:
- ssh-keyscan -p "${PROD_SSH_PORT_PROYECTOSACC:-22}" "${PROD_SERVER_IP_PROYECTOSACC}" >> ~/.ssh/known_hosts 2>/dev/null || true - ssh-keyscan -p "${PROD_SSH_PORT_PROYECTOSACC:-22}" "${PROD_SERVER_IP_PROYECTOSACC}" >> ~/.ssh/known_hosts 2>/dev/null || true
- export TELEGRAM_BOT_TOKEN="${PROD_TELEGRAM_BOT_TOKEN}" - export TELEGRAM_BOT_TOKEN="${PROD_TELEGRAM_BOT_TOKEN}"
- export TELEGRAM_CHAT_ID="${PROD_TELEGRAM_CHAT_ID}" - export TELEGRAM_CHAT_ID="${PROD_TELEGRAM_CHAT_ID}"
- bash ci-cd-commons/telegram_alert.sh "🚀 Iniciando pipeline PROD de proyectosacc" - bash scripts/telegram-pipeline-notify.sh start
- step: - step:
name: 02_repo-config name: 02_repo-config
@@ -224,4 +226,6 @@ pipelines:
"bash /home/thoth/deploy/setup/deploy.sh" "bash /home/thoth/deploy/setup/deploy.sh"
- export CLOUDFRONT_DISTRIBUTION_ID=$(python3 -c "import json; print(json.load(open('terraform/terraform-outputs.json'))['cloudfront_distribution_id']['value'])") - export CLOUDFRONT_DISTRIBUTION_ID=$(python3 -c "import json; print(json.load(open('terraform/terraform-outputs.json'))['cloudfront_distribution_id']['value'])")
- aws cloudfront create-invalidation --distribution-id "${CLOUDFRONT_DISTRIBUTION_ID}" --paths "/*" - aws cloudfront create-invalidation --distribution-id "${CLOUDFRONT_DISTRIBUTION_ID}" --paths "/*"
- bash ci-cd-commons/telegram_alert.sh "✅ Deploy PROD de proyectosacc completado exitosamente" - export TELEGRAM_BOT_TOKEN="${PROD_TELEGRAM_BOT_TOKEN}"
- export TELEGRAM_CHAT_ID="${PROD_TELEGRAM_CHAT_ID}"
- bash scripts/telegram-pipeline-notify.sh success "CloudFront invalidado"
+109
View File
@@ -0,0 +1,109 @@
#!/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