1
Files
2025/deploy.sh

118 lines
3.2 KiB
Bash
Executable File

#!/run/current-system/sw/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "$SCRIPT_DIR/.env" ]; then
# Export all variables from .env for the duration of the script
set -a
. "$SCRIPT_DIR/.env"
set +a
fi
# Configuration
DEST_USER="${DEPLOY_DEST_USER:?DEPLOY_DEST_USER manquant}"
DEST_HOST="${DEPLOY_DEST_HOST:?DEPLOY_DEST_HOST manquant}"
SECOND_DEST_HOST="${DEPLOY_SECOND_DEST_HOST:?DEPLOY_SECOND_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="$(realpath /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
}
DEST_HOSTS=("$DEST_HOST")
if [ "$SECOND_DEST_HOST" != "$DEST_HOST" ]; then
DEST_HOSTS+=("$SECOND_DEST_HOST")
fi
deploy_to_host() {
local host="$1"
local local_deploy=false
local cmd
if is_local_host "$host"; then
local_deploy=true
fi
if [ "$local_deploy" = true ]; then
if [ ! -d "$DEST_DIR" ]; then
echo "Dossier de destination introuvable: $DEST_DIR" >&2
exit 1
fi
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"
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"
find "$DEST_DIR" -type d -name data -exec rm -rf {} +
sudo "$CHOWN_BIN" -R "$TARGET_OWNER" "$DEST_DIR"
return
fi
echo "==> Synchronisation du site vers le serveur $host"
rsync -rlvz --delete \
--exclude='data/***' \
--no-owner --no-group \
--no-perms --omit-dir-times --no-times \
--checksum \
public/ "$DEST_USER@$host:$DEST_DIR"
cmd="find \"$DEST_DIR\" -type d -name data -exec rm -rf {} +"
ssh "$DEST_USER@$host" "$cmd"
ssh "$DEST_USER@$host" "$CHOWN_BIN -R $TARGET_OWNER '$DEST_DIR'"
}
echo "==> Vérification des liens externes"
node "$SCRIPT_DIR/tools/check_external_links.js"
echo "==> Vérification des liens internes"
node "$SCRIPT_DIR/tools/check_internal_links.js"
echo "==> Enrichissement météo des articles"
node "$SCRIPT_DIR/tools/add_weather.js"
echo "==> Synchronisation des commentaires Lemmy"
node "$SCRIPT_DIR/tools/sync_lemmy_comments.js"
echo "==> Génération des statistiques"
npm run stats:generate
# echo "==> Application des taxonomies et mots-clés"
# node "$SCRIPT_DIR/tools/link_taxonomy_terms.js"
# echo "==> Ajout des liens vers les mots-clés du frontmatter"
# node "$SCRIPT_DIR/tools/link_frontmatter_keywords.js"
echo "==> Génération du site Hugo pour l'environnement $HUGO_ENV (avec nettoyage de destination)"
hugo --environment "$HUGO_ENV" --cleanDestinationDir
for host in "${DEST_HOSTS[@]}"; do
deploy_to_host "$host"
done
node tools/update_lemmy_post_dates.js
echo "==> Déploiement terminé avec succès."