140 lines
3.8 KiB
Bash
Executable File
140 lines
3.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Monitor Gitea Action after git push
|
|
# Este script se ejecuta después de un git push y espera a que termine la Gitea Action
|
|
|
|
set -euo pipefail
|
|
|
|
# Configuración
|
|
GITEA_URL="https://gitea.nucleoriofrio.com"
|
|
OWNER="nucleo000"
|
|
REPO="plantillaNuxtAuthentikProxy"
|
|
|
|
# Intentar cargar el token desde el entorno o desde ~/.bashrc
|
|
GITEA_TOKEN="${GITEA_TOKEN:-}"
|
|
if [ -z "$GITEA_TOKEN" ] && [ -f "$HOME/.bashrc" ]; then
|
|
# Intentar extraer el token de .bashrc
|
|
GITEA_TOKEN=$(grep -oP "export GITEA_TOKEN=['\"]?\K[^'\"]*" "$HOME/.bashrc" 2>/dev/null || echo "")
|
|
fi
|
|
|
|
MAX_WAIT_SECONDS=600 # 10 minutos
|
|
POLL_INTERVAL=10 # Consultar cada 10 segundos
|
|
|
|
# Leer el input JSON del hook
|
|
INPUT=$(cat)
|
|
|
|
# Verificar si el comando fue un git push
|
|
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // ""')
|
|
if [[ ! "$COMMAND" =~ git[[:space:]]+push ]]; then
|
|
# No fue un git push, salir sin hacer nada
|
|
exit 0
|
|
fi
|
|
|
|
# Verificar que existe el token
|
|
if [ -z "$GITEA_TOKEN" ]; then
|
|
cat <<EOF
|
|
{
|
|
"decision": "block",
|
|
"reason": "⚠️ Git push exitoso, pero no puedo monitorear la Gitea Action: falta GITEA_TOKEN.\n\nConfigura el token en ~/.bashrc o ~/.zshrc:\nexport GITEA_TOKEN='tu_token_aqui'"
|
|
}
|
|
EOF
|
|
exit 0
|
|
fi
|
|
|
|
# Función para consultar el estado de la última action
|
|
get_latest_action_status() {
|
|
curl -s -H "Authorization: token $GITEA_TOKEN" \
|
|
"$GITEA_URL/api/v1/repos/$OWNER/$REPO/actions/tasks?limit=1" | \
|
|
jq -r '.workflow_runs[0] // .data[0] // empty'
|
|
}
|
|
|
|
# Función para formatear el resultado
|
|
format_result() {
|
|
local status="$1"
|
|
local task_data="$2"
|
|
|
|
local id=$(echo "$task_data" | jq -r '.id // "N/A"')
|
|
local created=$(echo "$task_data" | jq -r '.created_at // "N/A"')
|
|
local started=$(echo "$task_data" | jq -r '.started_at // "N/A"')
|
|
local stopped=$(echo "$task_data" | jq -r '.stopped_at // "N/A"')
|
|
local commit=$(echo "$task_data" | jq -r '.head_sha[0:8] // "N/A"')
|
|
|
|
case "$status" in
|
|
success)
|
|
local emoji="✅"
|
|
local msg="EXITOSO"
|
|
;;
|
|
failure)
|
|
local emoji="❌"
|
|
local msg="FALLÓ"
|
|
;;
|
|
cancelled)
|
|
local emoji="🚫"
|
|
local msg="CANCELADO"
|
|
;;
|
|
*)
|
|
local emoji="⚠️"
|
|
local msg="DESCONOCIDO ($status)"
|
|
;;
|
|
esac
|
|
|
|
cat <<EOF
|
|
{
|
|
"decision": "block",
|
|
"reason": "$emoji Gitea Action completada: $msg\n\n📋 Detalles:\n • ID: $id\n • Commit: $commit\n • Iniciado: $started\n • Finalizado: $stopped\n\n🔗 Ver en Gitea: $GITEA_URL/$OWNER/$REPO/actions"
|
|
}
|
|
EOF
|
|
}
|
|
|
|
# Notificar que empezamos a monitorear
|
|
echo "🔄 Monitoreando Gitea Action (máximo ${MAX_WAIT_SECONDS}s)..." >&2
|
|
|
|
# Esperar un poco antes de la primera consulta (dar tiempo a que Gitea cree la action)
|
|
sleep 5
|
|
|
|
# Polling loop
|
|
elapsed=0
|
|
while [ $elapsed -lt $MAX_WAIT_SECONDS ]; do
|
|
# Consultar el estado
|
|
TASK_DATA=$(get_latest_action_status)
|
|
|
|
if [ -z "$TASK_DATA" ]; then
|
|
echo "⏳ Esperando que Gitea cree la action... (${elapsed}s)" >&2
|
|
sleep $POLL_INTERVAL
|
|
elapsed=$((elapsed + POLL_INTERVAL))
|
|
continue
|
|
fi
|
|
|
|
STATUS=$(echo "$TASK_DATA" | jq -r '.status // "unknown"')
|
|
|
|
echo "📊 Estado actual: $STATUS (${elapsed}s)" >&2
|
|
|
|
# Verificar si terminó
|
|
case "$STATUS" in
|
|
success|failure|cancelled)
|
|
# Action terminó!
|
|
format_result "$STATUS" "$TASK_DATA"
|
|
exit 0
|
|
;;
|
|
running|pending|waiting)
|
|
# Todavía corriendo
|
|
sleep $POLL_INTERVAL
|
|
elapsed=$((elapsed + POLL_INTERVAL))
|
|
;;
|
|
*)
|
|
# Estado desconocido
|
|
echo "⚠️ Estado desconocido: $STATUS" >&2
|
|
sleep $POLL_INTERVAL
|
|
elapsed=$((elapsed + POLL_INTERVAL))
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Timeout alcanzado
|
|
cat <<EOF
|
|
{
|
|
"decision": "block",
|
|
"reason": "⏱️ Timeout: La Gitea Action todavía está corriendo después de ${MAX_WAIT_SECONDS}s.\n\n🔗 Verifica el estado manualmente en:\n$GITEA_URL/$OWNER/$REPO/actions"
|
|
}
|
|
EOF
|