rpg-westreiche/tools/deploy_web.sh

66 lines
2.1 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Lädt den gebauten Webauftritt (web/) per explizitem FTPS nach public_html/.
# Voraussetzung: `python3 tools/build_site.py` wurde ausgeführt.
#
# Aufruf: ./tools/deploy_web.sh [--dry-run]
set -euo pipefail
cd "$(dirname "$0")/.."
[ -f credentials.txt ] || { echo "credentials.txt fehlt" >&2; exit 1; }
[ -d web ] || { echo "web/ fehlt – erst tools/build_site.py ausführen" >&2; exit 1; }
# credentials.txt zeilenweise einlesen (KEY=VALUE, optional in Anführungs-
# zeichen) – nicht per `source`, damit Passwörter mit Sonderzeichen wie
# ( ) ; ? $ nicht die Shell sprengen.
load_credentials() {
local file="$1" line key value
while IFS= read -r line || [[ -n "${line}" ]]; do
line="${line%$'\r'}"
[[ -z "${line//[[:space:]]/}" || "${line}" == \#* ]] && continue
[[ "${line}" != *=* ]] && continue
key="${line%%=*}"
value="${line#*=}"
key="${key//[[:space:]]/}"
[[ "${key}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue
if [[ ${#value} -ge 2 && ( "${value}" == \"*\" || "${value}" == \'*\' ) ]]; then
value="${value:1:${#value}-2}"
fi
value="${value%"${value##*[![:space:]]}"}"
export "${key}=${value}"
done < "${file}"
}
load_credentials ./credentials.txt
DRY=0
[ "${1:-}" = "--dry-run" ] && DRY=1
TARGET="public_html"
upload() {
local src="$1" dst="$2"
if [ "$DRY" = 1 ]; then
echo "would upload $src -> ftp://$FTP_URL/$dst"
return
fi
curl -s -S --ssl-reqd --ftp-create-dirs --max-time 120 --user "$FTP_USER:$FTP_PASSWORD" \
-T "$src" "ftp://$FTP_URL/$dst" || { echo "FEHLER: $src" >&2; return 1; }
echo "ok $dst"
}
# Einzeldateien (nur was gebaut wurde – weltenauge.php fehlt, solange das
# Projekt ruht; siehe SHOW_WELTENAUGE in tools/build_site.py)
for f in index.php weltenauge.php; do
[ -f "web/$f" ] || continue
upload "web/$f" "$TARGET/$f"
done
# assets/ und westreiche/ rekursiv
while IFS= read -r -d '' file; do
rel="${file#web/}"
upload "$file" "$TARGET/$rel"
done < <(find web/assets web/westreiche -type f -print0 | sort -z)
echo "Deploy abgeschlossen ($([ "$DRY" = 1 ] && echo "dry-run" || echo "live"))."