diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index cb3a8fa..7e44ea0 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -14,21 +14,21 @@ options: audiences: - sts.amazonaws.com -definitions: - steps: - - step: ¬ify-start - name: Notify Start - script: - - export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}" - - export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}" - - bash ci-cd-commons/telegram_alert.sh "🚀 Iniciando pipeline de proyectosacc (${BITBUCKET_BRANCH})" + definitions: + steps: + - step: ¬ify-start + name: Notify Start + script: + - export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}" + - export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}" + - bash scripts/telegram-pipeline-notify.sh start - - step: ¬ify-fail - name: Notify Failure - script: - - export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}" - - export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}" - - bash ci-cd-commons/telegram_alert.sh "❌ Pipeline de proyectosacc falló en el paso ${BITBUCKET_STEP_KEY}" + - step: ¬ify-fail + name: Notify Failure + script: + - export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN}" + - export TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID}" + - bash scripts/telegram-pipeline-notify.sh failure "Paso: ${BITBUCKET_STEP_KEY}" pipelines: 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 - export TELEGRAM_BOT_TOKEN="${DEV_TELEGRAM_BOT_TOKEN}" - 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: name: 02_repo-config @@ -132,7 +132,9 @@ pipelines: "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'])") - 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: - 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 - export TELEGRAM_BOT_TOKEN="${PROD_TELEGRAM_BOT_TOKEN}" - 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: name: 02_repo-config @@ -224,4 +226,6 @@ pipelines: "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'])") - 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" diff --git a/scripts/telegram-pipeline-notify.sh b/scripts/telegram-pipeline-notify.sh new file mode 100755 index 0000000..5013058 --- /dev/null +++ b/scripts/telegram-pipeline-notify.sh @@ -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 [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 <&2 + # No fallamos el pipeline por un error de notificación + exit 0 +fi