QLFW-82: проверять доступность сервиса по сетевому псевдониму
This commit is contained in:
parent
fbb0974f13
commit
6e05478b06
@ -37,14 +37,10 @@ custom=$(jq -r --arg e "$env_name" '.deploy.hosts[$e] // empty' "$cfg")
|
||||
[[ -n "$custom" ]] && host="${custom//\{pr\}/$pr}"
|
||||
image="peppa/$slug:$sha"
|
||||
router=$(tr -c 'a-zA-Z0-9\n' - <<<"$project")
|
||||
container="$project-$service-1"
|
||||
service_alias="$project-$service"
|
||||
log=$(mktemp)
|
||||
exec > >(tee -a "$log") 2>&1
|
||||
|
||||
prev_image=$(docker inspect -f '{{.Config.Image}}' "$container" 2>/dev/null || true)
|
||||
prev_sha=""
|
||||
[[ $prev_image == peppa/$slug:* ]] && prev_sha="${prev_image##*:}"
|
||||
|
||||
report() { # report <status> [reason]
|
||||
local status="$1" reason="${2:-}" peppa_url commits body sig
|
||||
echo "peppa: $env_name $status${reason:+ — $reason}"
|
||||
@ -70,14 +66,17 @@ report() { # report <status> [reason]
|
||||
|
||||
dc() { # dc <image> <compose args…>
|
||||
local img="$1"; shift
|
||||
IMAGE="$img" HOST="$host" ROUTER="$router" APP_PORT="$port" docker compose -p "$project" -f "$compose" "$@"
|
||||
IMAGE="$img" HOST="$host" ROUTER="$router" APP_PORT="$port" SERVICE_ALIAS="$service_alias" \
|
||||
docker compose -p "$project" -f "$compose" "$@"
|
||||
}
|
||||
|
||||
healthy() {
|
||||
local url="${PEPPA_HEALTH_BASE:-http://$container:$port}" deadline=$((SECONDS + timeout))
|
||||
local url="${PEPPA_HEALTH_BASE:-http://$service_alias:$port}" deadline=$((SECONDS + timeout)) status
|
||||
url="${url//\{project\}/$project}$health"
|
||||
while ((SECONDS < deadline)); do
|
||||
curl -fsS -o /dev/null -m 5 "$url" && return 0
|
||||
if status=$(curl -fsS -o /dev/null -w '%{http_code}' -m 5 "$url") && [[ $status == 200 ]]; then
|
||||
return 0
|
||||
fi
|
||||
sleep "${PEPPA_HEALTH_INTERVAL:-3}"
|
||||
done
|
||||
return 1
|
||||
@ -95,7 +94,7 @@ screenshots() {
|
||||
((${#paths[@]})) || return 0
|
||||
local limit=()
|
||||
command -v timeout >/dev/null && limit=(timeout "${PEPPA_SCREENSHOT_TIMEOUT:-90}")
|
||||
local base="${PEPPA_SCREENSHOT_BASE:-${PEPPA_HEALTH_BASE:-http://$container:$port}}"
|
||||
local base="${PEPPA_SCREENSHOT_BASE:-${PEPPA_HEALTH_BASE:-http://$service_alias:$port}}"
|
||||
base="${base//\{project\}/$project}"
|
||||
file=$(mktemp)
|
||||
echo '[]' > "$file"
|
||||
@ -116,6 +115,14 @@ screenshots() {
|
||||
rm -f "$file"
|
||||
}
|
||||
|
||||
previous_container=$(dc "$image" ps -q --status running "$service")
|
||||
prev_image=""
|
||||
prev_sha=""
|
||||
if [[ -n "$previous_container" ]]; then
|
||||
prev_image=$(docker inspect -f '{{.Config.Image}}' "$previous_container" 2>/dev/null || true)
|
||||
[[ $prev_image == peppa/$slug:* ]] && prev_sha="${prev_image##*:}"
|
||||
fi
|
||||
|
||||
if [[ $mode == preview-down ]]; then
|
||||
dc "$image" down --remove-orphans || true
|
||||
report removed
|
||||
@ -129,17 +136,19 @@ fi
|
||||
if [[ -n "$migrate" ]]; then
|
||||
dc "$image" run --rm "$service" sh -c "$migrate" || { report failed "миграция не удалась"; exit 1; }
|
||||
fi
|
||||
dc "$image" up -d --remove-orphans || { report failed "docker compose up не удался"; exit 1; }
|
||||
if healthy; then
|
||||
if ! dc "$image" up --wait --wait-timeout "$timeout" --remove-orphans; then
|
||||
why="сервис не запустился"
|
||||
elif ! healthy; then
|
||||
why="проверка $health не прошла за ${timeout} с"
|
||||
else
|
||||
report healthy
|
||||
[[ $env_name == preview ]] && screenshots
|
||||
docker image prune -af --filter "label=peppa.project=$slug" --filter "until=336h" >/dev/null 2>&1 || true
|
||||
exit 0
|
||||
fi
|
||||
why="проверка $health не прошла за ${timeout} с"
|
||||
if [[ -n "$prev_image" && "$prev_image" != "$image" ]]; then
|
||||
echo "peppa: $why — откат на $prev_image"
|
||||
if dc "$prev_image" up -d --remove-orphans && healthy; then
|
||||
if dc "$prev_image" up --wait --wait-timeout "$timeout" --remove-orphans && healthy; then
|
||||
report rolled_back "$why"
|
||||
else
|
||||
report failed "$why; откат на $prev_image тоже не поднялся"
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
# Стенд проекта Пеппы (PEP-57). Деплой: docker compose -p <slug>-<env> -f deploy/compose.yml up -d
|
||||
# с переменными IMAGE (образ peppa/<slug>:<sha>), HOST, ROUTER, APP_PORT — их задаёт
|
||||
# с переменными IMAGE (образ peppa/<slug>:<sha>), HOST, ROUTER, APP_PORT, SERVICE_ALIAS — их задаёт
|
||||
# .gitea/peppa-deploy.sh. Сервис и порт — .peppa/project.json → deploy.service / deploy.port.
|
||||
# Секреты приложения — в secrets Actions репозитория, сюда только через environment.
|
||||
services:
|
||||
@ -13,7 +13,9 @@ services:
|
||||
volumes:
|
||||
- site-data:/app/data
|
||||
networks:
|
||||
- traefik_default
|
||||
traefik_default:
|
||||
aliases:
|
||||
- ${SERVICE_ALIAS:?SERVICE_ALIAS is required}
|
||||
labels:
|
||||
- traefik.enable=true
|
||||
- traefik.docker.network=traefik_default
|
||||
|
||||
Loading…
Reference in New Issue
Block a user