autodeploy implemented
Some checks failed
Deploy SingleChat / deploy (push) Failing after 1s

This commit is contained in:
Torsten Schulz (local)
2026-06-15 16:17:20 +02:00
parent 5bb9db2aad
commit 8d7c7d6f2a
4 changed files with 245 additions and 2 deletions

149
scripts/actualize-singlechat.sh Executable file
View File

@@ -0,0 +1,149 @@
#!/usr/bin/env bash
set -euo pipefail
APP_DIR="${APP_DIR:-/opt/ypchat}"
REPO_URL="${REPO_URL:-ssh://git@tsschulz.de:2222/torsten/singlechat}"
BRANCH="${BRANCH:-main}"
SERVICE_NAME="${SERVICE_NAME:-ypchat}"
RUN_USER="${RUN_USER:-www-data}"
RUN_GROUP="${RUN_GROUP:-www-data}"
LOCK_FILE="${LOCK_FILE:-/tmp/actualize-singlechat.lock}"
NPM_CACHE_DIR="${NPM_CACHE_DIR:-$APP_DIR/.npm-cache}"
log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
}
run_as_app_user() {
if [ "$(id -u)" -eq 0 ]; then
sudo -u "$RUN_USER" env HOME="$APP_DIR" npm_config_cache="$NPM_CACHE_DIR" "$@"
else
env HOME="$APP_DIR" npm_config_cache="$NPM_CACHE_DIR" "$@"
fi
}
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "FEHLER: $1 ist nicht installiert." >&2
exit 1
fi
}
need_cmd git
need_cmd npm
need_cmd node
need_cmd rsync
need_cmd flock
need_cmd openssl
if [ "$(id -u)" -ne 0 ]; then
need_cmd sudo
fi
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "Deployment laeuft bereits: $LOCK_FILE" >&2
exit 1
fi
log "Starte SingleChat Rollout"
log "APP_DIR=$APP_DIR"
log "REPO_URL=$REPO_URL"
log "BRANCH=$BRANCH"
log "SERVICE_NAME=$SERVICE_NAME"
mkdir -p "$APP_DIR" "$NPM_CACHE_DIR"
if [ ! -d "$APP_DIR/.git" ]; then
log "Erstelle initialen Git-Checkout fuer $APP_DIR"
tmp_checkout="$(mktemp -d)"
git clone --branch "$BRANCH" --single-branch "$REPO_URL" "$tmp_checkout"
rsync -a --delete \
--exclude '.env' \
--exclude '.npm-cache/' \
--exclude 'logs/' \
--exclude 'tmp/' \
"$tmp_checkout/" "$APP_DIR/"
rm -rf "$tmp_checkout"
fi
if [ "$(id -u)" -eq 0 ]; then
chown -R "$RUN_USER:$RUN_GROUP" "$APP_DIR" "$NPM_CACHE_DIR"
fi
cd "$APP_DIR"
if ! git remote get-url origin >/dev/null 2>&1; then
git remote add origin "$REPO_URL"
fi
current_origin="$(git remote get-url origin)"
if [ "$current_origin" != "$REPO_URL" ]; then
log "Setze Git-Origin von $current_origin auf $REPO_URL"
git remote set-url origin "$REPO_URL"
fi
log "Hole neuesten Stand"
git fetch --prune origin "$BRANCH"
git reset --hard "origin/$BRANCH"
git clean -fd \
-e .env \
-e .npm-cache/ \
-e logs/ \
-e tmp/ \
-e docroot/dist/
if [ "$(id -u)" -eq 0 ]; then
chown -R "$RUN_USER:$RUN_GROUP" "$APP_DIR" "$NPM_CACHE_DIR"
fi
log "Installiere Root-Dependencies"
run_as_app_user npm ci
log "Installiere Client-Dependencies"
run_as_app_user npm --prefix client ci
if [ ! -f "$APP_DIR/.env" ]; then
log "Erstelle .env"
session_secret="$(openssl rand -hex 32)"
cat > "$APP_DIR/.env" <<EOF
NODE_ENV=production
PORT=4000
SESSION_SECRET=$session_secret
EOF
fi
if [ "$(id -u)" -eq 0 ]; then
chown "$RUN_USER:$RUN_GROUP" "$APP_DIR/.env"
fi
log "Baue Client"
run_as_app_user npm run build
log "Aktualisiere docroot/dist"
rm -rf "$APP_DIR/docroot/dist"
cp -r "$APP_DIR/client/dist" "$APP_DIR/docroot/dist"
mkdir -p "$APP_DIR/logs" "$APP_DIR/tmp"
if [ "$(id -u)" -eq 0 ]; then
chown -R "$RUN_USER:$RUN_GROUP" "$APP_DIR/docroot/dist" "$APP_DIR/logs" "$APP_DIR/tmp"
fi
log "Pruefe Server-Syntax"
node --check "$APP_DIR/server/index.js"
node --check "$APP_DIR/server/routes-seo.js"
if command -v systemctl >/dev/null 2>&1; then
log "Starte Service neu: $SERVICE_NAME"
if [ "$(id -u)" -eq 0 ]; then
systemctl restart "$SERVICE_NAME"
systemctl --no-pager --lines=20 status "$SERVICE_NAME"
else
sudo systemctl restart "$SERVICE_NAME"
sudo systemctl --no-pager --lines=20 status "$SERVICE_NAME"
fi
else
log "systemctl nicht gefunden, Service-Neustart uebersprungen"
fi
log "SingleChat Rollout abgeschlossen"