diff --git a/deploy.sh b/deploy.sh index 29bcf8b0..7d895b0c 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/run/current-system/sw/bin/bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -16,6 +16,27 @@ DEST_HOST="${DEPLOY_DEST_HOST:?DEPLOY_DEST_HOST manquant}" DEST_DIR="/var/lib/www/richard-dern.fr/" HUGO_ENV="production" TARGET_OWNER="caddy:caddy" +CHOWN_BIN="/run/current-system/sw/bin/chown" +SETFACL_BIN="/run/current-system/sw/bin/setfacl" + +is_local_host() { + local target="$1" + local hostnames + + hostnames="$(hostname) $(hostname -f) $(hostname -s) localhost 127.0.0.1 ::1" + for name in $hostnames; do + if [ "$target" = "$name" ]; then + return 0 + fi + done + + return 1 +} + +LOCAL_DEPLOY=false +if is_local_host "$DEST_HOST"; then + LOCAL_DEPLOY=true +fi echo "==> Vérification des liens externes" node "$SCRIPT_DIR/tools/check_external_links.js" @@ -38,17 +59,42 @@ npm run stats:generate echo "==> Génération du site Hugo pour l'environnement $HUGO_ENV (avec nettoyage de destination)" hugo --environment "$HUGO_ENV" --cleanDestinationDir -echo "==> Synchronisation du site vers le serveur $DEST_HOST" -rsync -avz --delete \ - --exclude='data/***' \ - --no-owner --no-group \ - --checksum \ - public/ "$DEST_USER@$DEST_HOST:$DEST_DIR" +if [ "$LOCAL_DEPLOY" = true ]; then + if [ ! -d "$DEST_DIR" ]; then + echo "Dossier de destination introuvable: $DEST_DIR" >&2 + exit 1 + fi -CMD="find \"$DEST_DIR\" -type d -name data -exec rm -rf {} +" -ssh "$DEST_USER@$DEST_HOST" "$CMD" + echo "==> Vérification/pose des ACL sur $DEST_DIR" + sudo "$SETFACL_BIN" -R -m u:"$USER":rwx -m m:rwx "$DEST_DIR" + sudo "$SETFACL_BIN" -dR -m u:"$USER":rwx -m m:rwx "$DEST_DIR" +fi -echo "==> Correction des droits sur le serveur" -ssh "$DEST_USER@$DEST_HOST" "chown -R $TARGET_OWNER '$DEST_DIR'" +if [ "$LOCAL_DEPLOY" = true ]; then + echo "==> Synchronisation locale du site vers $DEST_DIR" + rsync -rlvz --delete \ + --exclude='data/***' \ + --no-owner --no-group \ + --no-perms --omit-dir-times --no-times \ + --checksum \ + public/ "$DEST_DIR" +else + echo "==> Synchronisation du site vers le serveur $DEST_HOST" + rsync -rlvz --delete \ + --exclude='data/***' \ + --no-owner --no-group \ + --no-perms --omit-dir-times --no-times \ + --checksum \ + public/ "$DEST_USER@$DEST_HOST:$DEST_DIR" +fi + +if [ "$LOCAL_DEPLOY" = true ]; then + find "$DEST_DIR" -type d -name data -exec rm -rf {} + + sudo "$CHOWN_BIN" -R "$TARGET_OWNER" "$DEST_DIR" +else + CMD="find \"$DEST_DIR\" -type d -name data -exec rm -rf {} +" + ssh "$DEST_USER@$DEST_HOST" "$CMD" + ssh "$DEST_USER@$DEST_HOST" "$CHOWN_BIN -R $TARGET_OWNER '$DEST_DIR'" +fi echo "==> Déploiement terminé avec succès."