diff --git a/deploy.sh b/deploy.sh index 4b54587..3478e54 100755 --- a/deploy.sh +++ b/deploy.sh @@ -29,11 +29,20 @@ say "Date: $(date -u '+%Y-%m-%dT%H:%M:%SZ')" cd "$PROJECT_DIR" # --------------------------------------------------------- -# 1) Pull latest code from origin/main +# 0) Stop service before touching files (prevents file locks) +# --------------------------------------------------------- +if command -v systemctl >/dev/null 2>&1; then + say "Step 0/4 — Stopping ${APP_NAME} service…" + systemctl stop "${APP_NAME}.service" || true +fi + +# --------------------------------------------------------- +# 1) Pull latest code from origin/main (hard reset — no merge conflicts) # --------------------------------------------------------- say "Step 1/4 — Fetching latest code from origin/main…" git fetch --all git reset --hard origin/main +git clean -fd say "Now at: $(git rev-parse --short HEAD)" # --------------------------------------------------------- diff --git a/index.js b/index.js index 3667184..d70b14f 100644 --- a/index.js +++ b/index.js @@ -2526,11 +2526,17 @@ bot.command('deploy', async (ctx) => { .editMessageText(ctx.chat.id, status.message_id, null, text, { parse_mode: 'Markdown' }) .catch(() => {}); - // ── Step 1: git pull ────────────────────────────────────────── - const gitResult = await runCmd('git', ['pull', 'origin', 'main'], PROJECT_DIR, 30000); + // ── Step 1: fetch + hard reset (avoids hang on dirty tree / merge conflicts) ── + const fetchResult = await runCmd('git', ['fetch', '--all'], PROJECT_DIR, 30000); + const gitResult = fetchResult.ok + ? await runCmd('git', ['reset', '--hard', 'origin/main'], PROJECT_DIR, 15000) + : fetchResult; + if (gitResult.ok) { + await runCmd('git', ['clean', '-fd'], PROJECT_DIR, 10000).catch(() => {}); + } const gitLine = gitResult.ok - ? `✅ *git pull:* \`${sanitizeCmdOutput(gitResult.out.split('\n')[0])}\`` - : `⚠️ *git pull failed:* \`${sanitizeCmdOutput(gitResult.err)}\``; + ? `✅ *git update:* \`${sanitizeCmdOutput(gitResult.out.split('\n')[0])}\`` + : `⚠️ *git update failed:* \`${sanitizeCmdOutput(gitResult.err || fetchResult.err)}\``; await edit( `🚀 *Deployment in progress* — ${ts()}\n\n` diff --git a/prod-run.sh b/prod-run.sh index 2a6586f..6f29904 100755 --- a/prod-run.sh +++ b/prod-run.sh @@ -38,13 +38,15 @@ say "Project directory: $PROJECT_DIR" say "Running as: $(id -un) (uid=$(id -u) gid=$(id -g))" # --------------------------------------------------------- -# 1) Pull latest code from origin main [FIRST — before anything] +# 1) Fetch + hard reset to origin/main [FIRST — before anything] +# Using fetch+reset instead of pull avoids hangs on dirty trees / conflicts. # --------------------------------------------------------- -say "Pulling latest code from origin main..." -if git -C "$PROJECT_DIR" pull origin main 2>&1; then - say "Git pull successful" +say "Fetching latest code from origin main..." +if git -C "$PROJECT_DIR" fetch --all 2>&1 && git -C "$PROJECT_DIR" reset --hard origin/main 2>&1; then + git -C "$PROJECT_DIR" clean -fd 2>&1 || true + say "Git fetch + reset successful: $(git -C "$PROJECT_DIR" rev-parse --short HEAD)" else - warn "Git pull failed — continuing with local copy" + warn "Git fetch/reset failed — continuing with local copy" fi # ---------------------------------------------------------