improved containers status check

This commit is contained in:
2026-07-16 14:26:49 +02:00
parent b4bc759eda
commit 284b187097
+47 -8
View File
@@ -26,6 +26,11 @@ on:
required: false required: false
type: string type: string
default: "" default: ""
health-timeout-seconds:
description: "Secondi massimi di attesa che tutti i container diventino running/healthy dopo il deploy"
required: false
type: string
default: "180"
jobs: jobs:
deploy: deploy:
@@ -288,41 +293,75 @@ jobs:
REMOTE_PATH="${DEPLOY_BASE_PATH}/${{ github.repository_owner }}/${{ github.event.repository.name }}" REMOTE_PATH="${DEPLOY_BASE_PATH}/${{ github.repository_owner }}/${{ github.event.repository.name }}"
COMPOSE="docker-compose-${ENV_NAME}.yml" COMPOSE="docker-compose-${ENV_NAME}.yml"
ENV_FILE=".env_${ENV_NAME}" ENV_FILE=".env_${ENV_NAME}"
TIMEOUT="${{ inputs.health-timeout-seconds }}"
CBASE="cd ${REMOTE_PATH} && DOCKER_CONFIG=/tmp/.docker-ci docker compose -f ${COMPOSE}" CBASE="cd ${REMOTE_PATH} && DOCKER_CONFIG=/tmp/.docker-ci docker compose -f ${COMPOSE}"
[ -f "$ENV_FILE" ] && CBASE="${CBASE} --env-file ${ENV_FILE}" [ -f "$ENV_FILE" ] && CBASE="${CBASE} --env-file ${ENV_FILE}"
echo "── Container status ──" echo "── Container status (iniziale) ──"
ssh ${DEPLOY_USER}@${HOST} "${CBASE} ps" ssh ${DEPLOY_USER}@${HOST} "${CBASE} ps"
# Verifica reale (non solo informativa): se un servizio non è "running" # Attende che tutti i container siano "running" e, se hanno un
# o risulta "unhealthy", lo step fallisce e innesca il rollback. # healthcheck definito, "healthy" - non basta una fotografia subito
# dopo "up -d", che coglie quasi sempre i servizi ancora in
# "starting" (specie servizi Java/Spring con avvio lento). Fallisce
# subito se un servizio è "unhealthy" o non "running" (nessuna
# attesa inutile), altrimenti continua a interrogare fino a TIMEOUT.
ssh ${DEPLOY_USER}@${HOST} " ssh ${DEPLOY_USER}@${HOST} "
set -e set -e
TIMEOUT=${TIMEOUT}
INTERVAL=5
elapsed=0
while true; do
ids=\$(${CBASE} ps -q) ids=\$(${CBASE} ps -q)
if [ -z \"\$ids\" ]; then if [ -z \"\$ids\" ]; then
echo 'ERRORE: nessun container in esecuzione dopo il deploy.' echo 'ERRORE: nessun container in esecuzione dopo il deploy.'
exit 1 exit 1
fi fi
fail=0 fail=0
pending=0
for cid in \$ids; do for cid in \$ids; do
svc=\$(docker inspect -f '{{ index .Config.Labels \"com.docker.compose.service\" }}' \$cid 2>/dev/null) svc=\$(docker inspect -f '{{ index .Config.Labels \"com.docker.compose.service\" }}' \$cid 2>/dev/null)
[ -z \"\$svc\" ] && svc=\$cid [ -z \"\$svc\" ] && svc=\$cid
status=\$(docker inspect -f '{{.State.Status}}' \$cid) status=\$(docker inspect -f '{{.State.Status}}' \$cid)
health=\$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}n/a{{end}}' \$cid) health=\$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}n/a{{end}}' \$cid)
echo \"Servizio \$svc: status=\$status health=\$health\"
if [ \"\$status\" != 'running' ]; then if [ \"\$status\" != 'running' ]; then
echo \"ERRORE: servizio \$svc non e' in stato running.\" echo \"ERRORE: servizio \$svc non e' in stato running (status=\$status).\"
fail=1 fail=1
fi elif [ \"\$health\" = 'unhealthy' ]; then
if [ \"\$health\" = 'unhealthy' ]; then
echo \"ERRORE: servizio \$svc risulta unhealthy.\" echo \"ERRORE: servizio \$svc risulta unhealthy.\"
fail=1 fail=1
elif [ \"\$health\" = 'starting' ]; then
pending=1
fi fi
done done
exit \$fail
if [ \"\$fail\" -eq 1 ]; then
echo '── Stato al momento del fallimento ──'
${CBASE} ps
exit 1
fi
if [ \"\$pending\" -eq 0 ]; then
echo 'Tutti i servizi sono running e (se previsto) healthy.'
break
fi
if [ \"\$elapsed\" -ge \"\$TIMEOUT\" ]; then
echo \"ERRORE: timeout (\${TIMEOUT}s) in attesa che tutti i servizi diventino healthy.\"
${CBASE} ps
exit 1
fi
echo \"In attesa che i servizi diventino healthy... (\${elapsed}s/\${TIMEOUT}s)\"
sleep \$INTERVAL
elapsed=\$((elapsed + INTERVAL))
done
" "
echo "── Container status (finale) ──"
ssh ${DEPLOY_USER}@${HOST} "${CBASE} ps"
# ───────────────────────────────────────────────────────────── # ─────────────────────────────────────────────────────────────
# HOSTS: dopo un deploy riuscito, recuperiamo l'IP del container # HOSTS: dopo un deploy riuscito, recuperiamo l'IP del container
# FE (label type=FE) e ci assicuriamo che /etc/hosts sul server # FE (label type=FE) e ci assicuriamo che /etc/hosts sul server