Files
watermaps/deploy/scripts/deploy.sh
T
BuTzZ c9a20aacd7
Test and publish container images / test (push) Successful in 2m50s
Test and publish container images / publish (push) Failing after 54s
Reorganised deployment scripts and added rollback functionality. Updated documentation and workflow for container image builds.
2026-07-25 12:36:35 +02:00

153 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
WM_DEPLOY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=common.sh
source "$WM_DEPLOY_DIR/scripts/common.sh"
images_env_file="$WM_IMAGES_ENV_FILE"
usage() {
printf 'Verwendung: %s [--images-file DATEI]\n' "$0"
}
while [[ "$#" -gt 0 ]]; do
case "$1" in
--images-file)
[[ "$#" -ge 2 ]] || wm_die "Wert für --images-file fehlt."
images_env_file="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
wm_die "Unbekannte Option: $1"
;;
esac
done
[[ "$(id -u)" -eq 0 ]] ||
wm_die "Dieses Skript muss als root ausgeführt werden."
wm_load_env
wm_assert_data_mount
wm_use_images_env "$images_env_file"
install -d -m 0755 \
"$WATERMAPS_DATA_DIR/geofabrik" \
"$WATERMAPS_DATA_DIR/local" \
"$WATERMAPS_DATA_DIR/certbot/www" \
"$WATERMAPS_DATA_DIR/certbot/letsencrypt" \
"$WATERMAPS_RUNTIME_DIR/nginx/conf.d" \
"$WATERMAPS_RUNTIME_DIR/locks" \
"$WATERMAPS_RUNTIME_DIR/deployments"
wm_acquire_route_lock ||
wm_die "Deployment abgebrochen, weil gerade Routingdaten aktualisiert werden."
active_images_file="$WM_DEPLOY_DIR/.env.images"
rollback_images_file=""
resolved_images_file=""
deployment_complete=false
if [[ -f "$active_images_file" ]]; then
wm_validate_images_env "$active_images_file"
rollback_images_file="$(
mktemp "$WATERMAPS_RUNTIME_DIR/deployments/.rollback-images.XXXXXX"
)"
install -m 0644 "$active_images_file" "$rollback_images_file"
fi
wm_finish_deployment() {
local status=$?
trap - EXIT HUP INT TERM
set +e
if [[ "$status" -ne 0 && "$deployment_complete" != "true" ]]; then
if [[ -n "$rollback_images_file" && -f "$rollback_images_file" ]]; then
wm_log "Deployment fehlgeschlagen; vorheriges Container-Release wird wiederhergestellt."
wm_use_images_env "$rollback_images_file"
wm_compose up --detach --no-build --remove-orphans watermaps nginx
wm_wait_for_health watermaps 240
wm_wait_for_health nginx 120
if wm_route_data_ready; then
wm_smoke_test_route
fi
else
wm_log "Deployment fehlgeschlagen; für das erste Release existiert noch kein Rollback."
fi
fi
[[ -z "$resolved_images_file" ]] || rm -f "$resolved_images_file"
[[ -z "$rollback_images_file" ]] || rm -f "$rollback_images_file"
exit "$status"
}
trap wm_finish_deployment EXIT
trap 'exit 130' HUP INT TERM
if [[ ! -f "$WATERMAPS_RUNTIME_DIR/nginx/conf.d/default.conf" ]]; then
install -m 0644 \
"$WM_DEPLOY_DIR/nginx/bootstrap.conf" \
"$WATERMAPS_RUNTIME_DIR/nginx/conf.d/default.conf"
fi
revision="$(wm_env_value "$images_env_file" WATERMAPS_DEPLOY_REVISION)"
requested_app_image="$(wm_env_value "$images_env_file" WATERMAPS_APP_IMAGE)"
requested_route_data_image="$(wm_env_value "$images_env_file" WATERMAPS_ROUTE_DATA_IMAGE)"
wm_log "Container-Images für Commit $revision werden aus den Registries geladen."
wm_compose --profile maintenance pull watermaps nginx certbot route-data
resolved_app_image="$(wm_resolve_image_digest "$requested_app_image")"
resolved_route_data_image="$(wm_resolve_image_digest "$requested_route_data_image")"
resolved_images_file="$(
mktemp "$WATERMAPS_RUNTIME_DIR/deployments/.resolved-images.XXXXXX"
)"
{
printf 'WATERMAPS_DEPLOY_REVISION=%s\n' "$revision"
printf 'WATERMAPS_APP_IMAGE=%s\n' "$resolved_app_image"
printf 'WATERMAPS_ROUTE_DATA_IMAGE=%s\n' "$resolved_route_data_image"
} >"$resolved_images_file"
chmod 0644 "$resolved_images_file"
wm_use_images_env "$resolved_images_file"
wm_log "Commit $revision wird mit unveränderlichen Image-Digests gestartet."
wm_compose up --detach --no-build --remove-orphans watermaps nginx
wm_wait_for_health watermaps 240
wm_wait_for_health nginx 120
route_rebuild_required=false
update_args=()
if [[ "${WATERMAPS_REBUILD_ROUTE_DATA:-false}" == "true" ]]; then
route_rebuild_required=true
update_args+=(--force)
elif ! wm_route_data_ready; then
route_rebuild_required=true
fi
if [[ "$route_rebuild_required" == "true" ]]; then
wm_log "Deutschland- und Niederlande-Routingdaten werden sicher vorbereitet."
WATERMAPS_ROUTE_LOCK_HELD=true \
"$WM_DEPLOY_DIR/scripts/update-route-data.sh" "${update_args[@]}"
fi
wm_assert_route_data
wm_wait_for_health watermaps 240
wm_smoke_test_route
release_images_file="$WATERMAPS_RUNTIME_DIR/deployments/$revision.env"
install -m 0644 "$resolved_images_file" "$release_images_file"
if [[ -n "$rollback_images_file" ]]; then
install -m 0644 \
"$rollback_images_file" \
"$WATERMAPS_RUNTIME_DIR/deployments/previous.env"
fi
install -m 0644 "$resolved_images_file" "$active_images_file"
deployment_complete=true
wm_log "Deployment von Commit $revision ist bereit. Vor dem Livegang liefert Port 80 nur ACME-Challenges und ansonsten HTTP 404."