← Retour
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════════
# NOTIFICATIONS.SH - Notifications de déploiement via ntfy
# Envoie des alertes push pour les événements de déploiement
#
# Usage:
# notify_deploy_start "mon-app" "prod-portainer"
# notify_deploy_success "mon-app" "prod-portainer" "abc1234"
# notify_deploy_failure "mon-app" "prod-portainer" "Error message"
# ═══════════════════════════════════════════════════════════════════════════
# ───────────────────────────────────────────────────────────────────────────
# Configuration
# ───────────────────────────────────────────────────────────────────────────
NTFY_URL="${NTFY_URL:-https://ntfy.33800.nowhere84.com}"
NTFY_TOPIC="${NTFY_TOPIC:-deploy}"
NTFY_ENABLED="${NTFY_ENABLED:-true}"
# ───────────────────────────────────────────────────────────────────────────
# Fonction interne d'envoi
# ───────────────────────────────────────────────────────────────────────────
_send_notification() {
local title="$1"
local message="$2"
local priority="${3:-default}"
local tags="${4:-}"
# Skip si désactivé
if [[ "$NTFY_ENABLED" != "true" ]]; then
return 0
fi
# Construire les headers
local headers=(-H "Title: $title")
headers+=(-H "Priority: $priority")
if [[ -n "$tags" ]]; then
headers+=(-H "Tags: $tags")
fi
# Envoyer la notification
curl -s -o /dev/null \
"${headers[@]}" \
-d "$message" \
"${NTFY_URL}/${NTFY_TOPIC}" 2>/dev/null || true
}
# ───────────────────────────────────────────────────────────────────────────
# Notification: Déploiement démarré
# ───────────────────────────────────────────────────────────────────────────
notify_deploy_start() {
local service="$1"
local target="$2"
local branch="${3:-main}"
_send_notification \
"Deploying ${service}" \
"Starting deployment of ${service} to ${target} (${branch})" \
"low" \
"rocket"
}
# ───────────────────────────────────────────────────────────────────────────
# Notification: Déploiement réussi
# ───────────────────────────────────────────────────────────────────────────
notify_deploy_success() {
local service="$1"
local target="$2"
local commit="${3:-unknown}"
local duration="${4:-}"
local message="Successfully deployed ${service} to ${target}"
message+="\nCommit: ${commit}"
if [[ -n "$duration" ]]; then
message+="\nDuration: ${duration}s"
fi
_send_notification \
"Deploy Success: ${service}" \
"$message" \
"default" \
"white_check_mark,${service}"
}
# ───────────────────────────────────────────────────────────────────────────
# Notification: Déploiement échoué
# ───────────────────────────────────────────────────────────────────────────
notify_deploy_failure() {
local service="$1"
local target="$2"
local error="${3:-Unknown error}"
_send_notification \
"Deploy FAILED: ${service}" \
"Deployment of ${service} to ${target} failed!\n\nError: ${error}" \
"high" \
"x,warning,${service}"
}
# ───────────────────────────────────────────────────────────────────────────
# Notification: Rollback effectué
# ───────────────────────────────────────────────────────────────────────────
notify_rollback() {
local service="$1"
local target="$2"
local from_version="${3:-current}"
local to_version="${4:-previous}"
_send_notification \
"Rollback: ${service}" \
"Rolled back ${service} on ${target}\nFrom: ${from_version}\nTo: ${to_version}" \
"high" \
"rewind,warning"
}
# ───────────────────────────────────────────────────────────────────────────
# Notification: Health check échoué
# ───────────────────────────────────────────────────────────────────────────
notify_health_failure() {
local service="$1"
local target="$2"
local endpoint="${3:-/health}"
_send_notification \
"Health Check Failed: ${service}" \
"Service ${service} on ${target} failed health check!\nEndpoint: ${endpoint}" \
"urgent" \
"ambulance,rotating_light"
}
# ───────────────────────────────────────────────────────────────────────────
# Notification personnalisée
# ───────────────────────────────────────────────────────────────────────────
notify_custom() {
local title="$1"
local message="$2"
local priority="${3:-default}"
local tags="${4:-}"
_send_notification "$title" "$message" "$priority" "$tags"
}