From 7f941e3cf74cc59a4f82a1448337964360ae1506 Mon Sep 17 00:00:00 2001 From: David Santos Date: Wed, 25 Mar 2026 02:50:38 +0000 Subject: [PATCH] chore(pm2): auto DEPLOY_REVISION from git in ecosystem When DEPLOY_REVISION/GIT_REVISION are unset, ecosystem.config.cjs runs git rev-parse --short HEAD at pm2 start so /health deploy_revision matches the checkout. Documented in README and .env.example. Made-with: Cursor --- .env.example | 1 + README.md | 4 +++- ecosystem.config.cjs | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index e412604..ce6cd9a 100644 --- a/.env.example +++ b/.env.example @@ -40,6 +40,7 @@ SCRAPER_RETRY_BASE_MS=500 HOST=0.0.0.0 PORT=3200 # Optional: set at deploy (CI webhook) — surfaced in GET /health as deploy_revision +# PM2 ecosystem.config.cjs also injects DEPLOY_REVISION from `git rev-parse --short HEAD` when unset. # DEPLOY_REVISION= # GIT_REVISION= # Inbound socket timeouts (ms). headers must stay above request (enforced in code if misconfigured). diff --git a/README.md b/README.md index ca4f846..b70769a 100644 --- a/README.md +++ b/README.md @@ -179,9 +179,11 @@ pm2 start ecosystem.config.cjs 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`. + *(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://:3200` (forward scheme **http** unless you terminate TLS only at NPM). Quick check from another machine: `curl -sS http://:3200/health` — JSON includes `version`, `mode: "listen"`, 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`. +**NPM (CT 102):** Proxy Host `ainews.bitreport.pt` → `http://:3200` (forward scheme **http** unless you terminate TLS only at NPM). Quick check from another machine: `curl -sS http://: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`. **Structured check (exit code 0/1):** from a machine with `curl` + `node` (e.g. the CT or your laptop): diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs index 8639861..be8acf4 100644 --- a/ecosystem.config.cjs +++ b/ecosystem.config.cjs @@ -1,5 +1,29 @@ 'use strict'; +const { execSync } = require('child_process'); + +/** + * For GET /health `deploy_revision`: honour existing env, else short git SHA when `.git` exists. + */ +function resolveDeployRevisionForPm2() { + const fromEnv = (process.env.DEPLOY_REVISION || process.env.GIT_REVISION || '').trim(); + if (fromEnv) { + return fromEnv; + } + try { + return execSync('git rev-parse --short HEAD', { + cwd: __dirname, + encoding: 'utf8', + stdio: ['ignore', 'pipe', 'ignore'], + maxBuffer: 64, + }).trim(); + } catch (_err) { + return ''; + } +} + +const _deploy_revision = resolveDeployRevisionForPm2(); + /** * PM2 process definition — always runs `listen` (HTTP + RSS cron). * Prefer this over `pm2 start src/index.js ... -- listen` to avoid footguns. @@ -26,6 +50,7 @@ module.exports = { NODE_ENV: 'production', HOST: '0.0.0.0', PORT: '3200', + ...(_deploy_revision ? { DEPLOY_REVISION: _deploy_revision } : {}), }, }, ],