feat(tools): deploy-lxc.sh for git pull + npm ci + PM2 reload
Node tests / test (push) Has been cancelled

- tools/deploy-lxc.sh: ff-only pull, production npm ci, pm2 startOrReload
  ecosystem.config.cjs --update-env; optional --with-ops-check
- package.json: npm run deploy:lxc
- README: commands table + deploy section note

Made-with: Cursor
This commit is contained in:
David Santos committed 2026-03-25 03:03:09 +00:00
1 parent 7f941e3cf7
commit bd27b446a5
3 files changed
+58

No files matched your search

+3
View File
@@ -26,6 +26,7 @@ node src/index.js write "FC Porto match result"
| `npm run write -- "topic"` | Write article about specific topic |
| `npm run listen` | Start auto RSS listener (cron) |
| `npm run pm2:start` | PM2 via `ecosystem.config.cjs` (requires global `pm2`) |
| `npm run deploy:lxc` | Server deploy: `git pull --ff-only`, `npm ci --omit=dev`, `pm2 startOrReload ecosystem.config.cjs --update-env`; append `-- --with-ops-check` for smoke tests |
| `npm run verify:deploy -- <url>` | Smoke-test `GET /health` (expects `listen` mode); needs `curl` + `node` |
| `npm run wp:test` | `GET /wp-json/wp/v2/users/me` — validates **every** `config/sites/*.json` (excl. example), or `WP_*` env if no site files |
| `npm run rss:test` | Probe each RSS URL in `config/sources.json` (transient HTTP retries via `RSS_HTTP_MAX_ATTEMPTS` / `RSS_RETRY_BASE_MS`); stdout `{ "ok", "feeds" }`; exit 1 if any feed fails |
@@ -181,6 +182,8 @@ pm2 save && pm2 startup
When you start from a **git checkout**, `ecosystem.config.cjs` sets **`DEPLOY_REVISION`** to `git rev-parse --short HEAD` if `DEPLOY_REVISION` / `GIT_REVISION` are not already in the environment — the app exposes this on **`GET /health`** as `deploy_revision`.
**Repeatable deploy on the CT:** from the repo root, `npm run deploy:lxc` (runs `tools/deploy-lxc.sh`: `git pull --ff-only`, `npm ci --omit=dev`, `pm2 startOrReload ecosystem.config.cjs --update-env`). Add `--with-ops-check` for post-restart smoke tests (`npm run deploy:lxc -- --with-ops-check`).
*(Alternative, easier to mistype: `pm2 start src/index.js --name ai-news-writer --cwd /srv/ai-news-writer --time -- listen` — the `--` before `listen` is required.)*
**NPM (CT 102):** Proxy Host `ainews.bitreport.pt` → `http://<lxc-ip>:3200` (forward scheme **http** unless you terminate TLS only at NPM). Quick check from another machine: `curl -sS http://<lxc-ip>:3200/health` — JSON includes `version`, `mode: "listen"`, optional **`deploy_revision`** (git SHA / deploy tag when configured), and **non-secret flags**: `rss_feed_count`, `rss_listener_active` (false if the cron listener failed to start), **`rss_listener_last_tick_at` / `rss_listener_last_tick_ok` / `rss_listener_last_tick_error`** (cron liveness after the first tick), `x_listening` (bearer + `social.json` queries), `wp_credentials_configured`, `openai_api_key_configured`, `ai_model`.
+1
View File
@@ -14,6 +14,7 @@
"rss:test": "node src/index.js rss:test",
"x:test": "node src/index.js x:test",
"ops:check": "bash tools/ops-check.sh",
"deploy:lxc": "bash tools/deploy-lxc.sh",
"test": "node --test $(find src -type f -name '*.test.js' | sort | tr '\\n' ' ')"
},
"dependencies": {
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
# LXC / server deploy: git pull, production npm ci, PM2 reload with updated env.
# Run from the app host (e.g. inside the CT), with .env already present.
#
# Usage:
# ./tools/deploy-lxc.sh
# ./tools/deploy-lxc.sh --with-ops-check # runs npm run ops:check after PM2 (needs WP/RSS config)
#
# Requires: git, npm, pm2 on PATH; repo must be a git checkout (for pull + DEPLOY_REVISION in ecosystem).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
WITH_OPS_CHECK=0
for _arg in "$@"; do
if [[ "${_arg}" == "--with-ops-check" ]]; then
WITH_OPS_CHECK=1
fi
done
if ! command -v git >/dev/null 2>&1; then
echo "error: git is required" >&2
exit 1
fi
if ! command -v npm >/dev/null 2>&1; then
echo "error: npm is required" >&2
exit 1
fi
if ! command -v pm2 >/dev/null 2>&1; then
echo "error: pm2 is required (npm i -g pm2)" >&2
exit 1
fi
echo "=== deploy-lxc: git pull (ff-only) ==="
git pull --ff-only
echo ""
echo "=== deploy-lxc: npm ci (production deps) ==="
npm ci --omit=dev
echo ""
echo "=== deploy-lxc: PM2 startOrReload ecosystem (update env) ==="
# startOrReload: creates process on first run; reloads on subsequent deploys so ecosystem env (e.g. DEPLOY_REVISION) refreshes
pm2 startOrReload ecosystem.config.cjs --update-env
if [[ "${WITH_OPS_CHECK}" -eq 1 ]]; then
echo ""
echo "=== deploy-lxc: ops-check ==="
npm run ops:check --silent
fi
echo ""
echo "deploy-lxc: done. Tip: npm run verify:deploy -- \"http://127.0.0.1:3200\""