Configuración PWA: - Agregar estructura completa de Nuxt4 para PWA - Configurar .env.example con variables de entorno - Preparar aplicación para instalación offline Configuración Claude Code: - Agregar .claude/ con settings y hooks - Configurar entorno de desarrollo con Claude CI/CD: - Agregar .gitea/workflows para Gitea Actions - Preparar pipeline de despliegue automático Docker: - Actualizar docker-compose.yml con servicios PWA - Configurar networking entre servicios Git: - Actualizar .gitignore para excluir archivos de build - Ignorar node_modules y archivos temporales
198 lines
6.1 KiB
Bash
Executable File
198 lines
6.1 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="cataRio"
|
|
|
|
# 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 verificar si las actions están habilitadas
|
|
check_actions_enabled() {
|
|
local repo_info=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
|
|
"$GITEA_URL/api/v1/repos/$OWNER/$REPO")
|
|
echo "$repo_info" | jq -r '.has_actions // false'
|
|
}
|
|
|
|
# Función para consultar el estado de la última action
|
|
get_latest_action_status() {
|
|
local response=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
|
|
"$GITEA_URL/api/v1/repos/$OWNER/$REPO/actions/tasks?limit=1")
|
|
|
|
# Verificar si hay un error de permisos
|
|
if echo "$response" | jq -e '.message' > /dev/null 2>&1; then
|
|
echo "ERROR: $(echo "$response" | jq -r '.message')" >&2
|
|
echo ""
|
|
return 1
|
|
fi
|
|
|
|
echo "$response" | 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 workflow_name=$(echo "$task_data" | jq -r '.name // "N/A"')
|
|
local workflow_file=$(echo "$task_data" | jq -r '.workflow_id // "N/A"')
|
|
local run_number=$(echo "$task_data" | jq -r '.run_number // "N/A"')
|
|
local event=$(echo "$task_data" | jq -r '.event // "N/A"')
|
|
local branch=$(echo "$task_data" | jq -r '.head_branch // "N/A"')
|
|
local title=$(echo "$task_data" | jq -r '.display_title // "N/A"')
|
|
local created=$(echo "$task_data" | jq -r '.created_at // "N/A"')
|
|
local started=$(echo "$task_data" | jq -r '.run_started_at // .started_at // "N/A"')
|
|
local updated=$(echo "$task_data" | jq -r '.updated_at // .stopped_at // "N/A"')
|
|
local commit=$(echo "$task_data" | jq -r '.head_sha[0:8] // "N/A"')
|
|
local run_url=$(echo "$task_data" | jq -r '.url // ""')
|
|
|
|
# Calcular duración si es posible
|
|
local duration="N/A"
|
|
if [[ "$started" != "N/A" && "$updated" != "N/A" ]]; then
|
|
local start_ts=$(date -d "$started" +%s 2>/dev/null || echo "0")
|
|
local end_ts=$(date -d "$updated" +%s 2>/dev/null || echo "0")
|
|
if [[ $start_ts -gt 0 && $end_ts -gt 0 ]]; then
|
|
local diff=$((end_ts - start_ts))
|
|
if [[ $diff -lt 60 ]]; then
|
|
duration="${diff}s"
|
|
else
|
|
local mins=$((diff / 60))
|
|
local secs=$((diff % 60))
|
|
duration="${mins}m ${secs}s"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
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
|
|
|
|
# Construir URL de logs si no está disponible
|
|
if [[ -z "$run_url" || "$run_url" == "null" ]]; then
|
|
run_url="$GITEA_URL/$OWNER/$REPO/actions/runs/$id"
|
|
fi
|
|
|
|
cat <<EOF
|
|
{
|
|
"decision": "block",
|
|
"reason": "$emoji Gitea Action completada: $msg\n\n📋 Detalles:\n • Workflow: $workflow_name ($workflow_file)\n • Run #$run_number\n • Evento: $event\n • Branch: $branch\n • Commit: $commit\n • Título: $title\n • Duración: $duration\n • Iniciado: $started\n • Finalizado: $updated\n\n🔗 Ver logs completos:\n $run_url"
|
|
}
|
|
EOF
|
|
}
|
|
|
|
# Verificar si las actions están habilitadas
|
|
echo "🔍 Verificando si Actions están habilitadas..." >&2
|
|
ACTIONS_ENABLED=$(check_actions_enabled)
|
|
|
|
if [[ "$ACTIONS_ENABLED" != "true" ]]; then
|
|
cat <<EOF
|
|
{
|
|
"decision": "block",
|
|
"reason": "⚠️ Git push exitoso, pero las Gitea Actions NO están habilitadas en este repositorio.\n\n📝 Para habilitar Actions:\n1. Ve a: $GITEA_URL/$OWNER/$REPO/settings\n2. Busca la sección 'Actions' o 'Workflows'\n3. Activa las Actions\n\nLuego podrás ver tus workflows en:\n$GITEA_URL/$OWNER/$REPO/actions"
|
|
}
|
|
EOF
|
|
exit 0
|
|
fi
|
|
|
|
# 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
|