docs(ops): add deployment and rollback runbooks for ai-news-writer
- Add docs/runbooks/DEPLOYMENT.md: day-to-day deploy pipeline overview, staging/prod promote flow, env configs, zero-downtime PM2 strategy, manual emergency deploy procedure - Add docs/runbooks/ROLLBACK.md: decision tree (rollback vs hotfix), 5 rollback procedures (RB-1 restart through RB-5 full wipe/redeploy), failure modes table, post-rollback checklist - Update docs/runbooks/README.md: add Deploy & Rollback section linking both new runbooks Closes BIT-14770
This commit is contained in:
1 parent
e868644f6f
commit
c4c44dc3ef
3 files changed
+474
-2
No files matched your search
@@ -0,0 +1,233 @@
|
||||
# ai-news-writer — Deployment Runbook
|
||||
|
||||
**Service:** `ai-news-writer` | **CT:** 223 | **Port:** 3200
|
||||
**CI:** Gitea Actions | **Process manager:** PM2 (`ecosystem.config.cjs`)
|
||||
**External URL:** `https://ainews.bitreport.pt`
|
||||
|
||||
Related issues: BIT-14770 (this runbook), BIT-2708 (CI/ops programme).
|
||||
|
||||
---
|
||||
|
||||
## Pipeline Overview
|
||||
|
||||
```
|
||||
developer push
|
||||
│
|
||||
▼
|
||||
[develop branch]
|
||||
│
|
||||
▼
|
||||
┌──────────────────────────────┐
|
||||
│ Gitea CI (test.yml) │
|
||||
│ lint + audit + test │
|
||||
└──────────┬───────────────────┘
|
||||
│ success
|
||||
▼
|
||||
┌──────────────────────────────┐
|
||||
│ Staging auto-deploy │ ← deploy-staging.yml
|
||||
│ git pull + npm ci + pm2 │
|
||||
│ reload + verify-deploy.sh │
|
||||
└──────────┬───────────────────┘
|
||||
│ passes QA on staging
|
||||
▼
|
||||
PR: develop → main (code review)
|
||||
│ merged
|
||||
▼
|
||||
[main branch]
|
||||
│
|
||||
▼
|
||||
┌──────────────────────────────┐
|
||||
│ Production manual deploy │ ← deploy-production.yml
|
||||
│ Gitea Actions workflow │
|
||||
│ dispatch: "PROMOTE" │
|
||||
└──────────────────────────────┘
|
||||
```
|
||||
|
||||
Staging deploys automatically on every successful CI run against `develop`.
|
||||
Production requires an explicit **manual trigger** with the word `PROMOTE`.
|
||||
|
||||
---
|
||||
|
||||
## Environment Reference
|
||||
|
||||
| Setting | Dev (local) | Staging | Production |
|
||||
|---------|-------------|---------|------------|
|
||||
| Branch | any | `develop` | `main` |
|
||||
| Host/CT | localhost | CTID from `STAGING_HOST` secret | CTID 223 |
|
||||
| Port | 3200 | 3200 | 3200 |
|
||||
| URL | `http://localhost:3200` | `https://ainews-staging.bitreport.pt` | `https://ainews.bitreport.pt` |
|
||||
| PM2 config | `ecosystem.config.cjs` | `ecosystem.config.cjs` | `ecosystem.config.cjs` |
|
||||
| Secrets file | `.env` | `/etc/ai-news-writer/secrets.env` | `/etc/ai-news-writer/secrets.env` |
|
||||
| NODE_ENV | (unset) | `production` | `production` |
|
||||
|
||||
**Gitea secrets required for CI pipelines:**
|
||||
|
||||
| Secret | Used by | Purpose |
|
||||
|--------|---------|---------|
|
||||
| `STAGING_HOST` | deploy-staging.yml | SSH host for staging CT |
|
||||
| `STAGING_USER` | deploy-staging.yml | SSH user |
|
||||
| `STAGING_SSH_KEY` | deploy-staging.yml | Private key |
|
||||
| `STAGING_SSH_KNOWN_HOSTS` | deploy-staging.yml | Host fingerprints |
|
||||
| `PROD_HOST` | deploy-production.yml | SSH host for prod CT |
|
||||
| `PROD_USER` | deploy-production.yml | SSH user |
|
||||
| `PROD_SSH_KEY` | deploy-production.yml | Private key |
|
||||
| `PROD_SSH_KNOWN_HOSTS` | deploy-production.yml | Host fingerprints |
|
||||
| `PROD_URL` | deploy-production.yml | External URL for smoke test |
|
||||
|
||||
Application secrets (OpenAI key, WP credentials, etc.) are injected via `tools/deploy/lxc-secrets-inject.sh`. See [SECRETS_MANAGEMENT.md](../SECRETS_MANAGEMENT.md) and [DEPLOY_LXC.md](./DEPLOY_LXC.md).
|
||||
|
||||
---
|
||||
|
||||
## Staging Deployment
|
||||
|
||||
### Automatic (normal flow)
|
||||
|
||||
Push to `develop` → CI passes → staging deploys automatically.
|
||||
|
||||
Monitor in Gitea: `BitReport.pt/ai-news-writer` → Actions → `ai-news-writer-deploy-staging`.
|
||||
|
||||
### Manual trigger
|
||||
|
||||
1. Go to Gitea Actions → `ai-news-writer-deploy-staging` → **Run workflow**
|
||||
2. Select branch `develop`
|
||||
3. Click **Run**
|
||||
|
||||
The pipeline:
|
||||
1. `git pull --ff-only origin develop`
|
||||
2. `npm ci --omit=dev && npm run lint --quiet`
|
||||
3. `pm2 startOrReload ecosystem.config.cjs --update-env`
|
||||
4. Waits 5 s, runs `tools/verify-deploy.sh http://127.0.0.1:3200`
|
||||
5. Fails → automatic rollback (see [ROLLBACK.md](./ROLLBACK.md))
|
||||
|
||||
---
|
||||
|
||||
## Production Deployment
|
||||
|
||||
### Pre-deployment checklist
|
||||
|
||||
- [ ] Feature has been running on staging without errors for ≥ 1 hour
|
||||
- [ ] PM2 logs clean on staging (`pm2 logs ai-news-writer --lines 50 --nostream`)
|
||||
- [ ] PR reviewed and merged into `main`
|
||||
- [ ] No open critical or high issues blocking the release
|
||||
- [ ] You have a rollback plan (see [ROLLBACK.md](./ROLLBACK.md))
|
||||
|
||||
### Trigger production deploy
|
||||
|
||||
1. Open Gitea Actions → `ai-news-writer-deploy-production` → **Run workflow**
|
||||
2. Branch: `main`
|
||||
3. Confirmation field: type exactly `PROMOTE`
|
||||
4. Deployment notes: enter short description (e.g., commit hash or reason)
|
||||
5. Click **Run workflow**
|
||||
|
||||
The pipeline:
|
||||
1. Validates `PROMOTE` confirmation (exits if wrong)
|
||||
2. Logs pre-deployment state (`pm2 list`, last commit)
|
||||
3. `git pull --ff-only origin main`
|
||||
4. `npm ci --omit=dev && npm run lint --quiet`
|
||||
5. `pm2 startOrReload ecosystem.config.cjs --update-env`
|
||||
6. Waits 10 s, runs `tools/verify-deploy.sh http://127.0.0.1:3200`
|
||||
7. External smoke: `curl $PROD_URL/health` — must return `"ok":true`
|
||||
8. On any failure → automatic rollback (see [ROLLBACK.md](./ROLLBACK.md))
|
||||
|
||||
### Post-deploy verification
|
||||
|
||||
```bash
|
||||
# From a host with SSH access to the prod CT:
|
||||
ssh <prod-user>@<prod-host> "cd /opt/bitreport/ai-news-writer && bash tools/verify-deploy.sh http://127.0.0.1:3200"
|
||||
|
||||
# Or from outside:
|
||||
curl -s https://ainews.bitreport.pt/health | jq '{status: .ok, version: .version, revision: .deploy_revision}'
|
||||
```
|
||||
|
||||
Expected:
|
||||
```json
|
||||
{ "status": true, "version": "x.y.z", "revision": "abc1234" }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Zero-Downtime Strategy
|
||||
|
||||
The service runs as a **single PM2 process** (`instances: 1`). Zero-downtime is achieved via PM2's graceful reload:
|
||||
|
||||
```
|
||||
pm2 startOrReload ecosystem.config.cjs --update-env
|
||||
```
|
||||
|
||||
PM2 reload sequence for a single instance:
|
||||
1. Starts new process with updated code and env
|
||||
2. Waits for the new process to emit `ready` or for the listen timeout
|
||||
3. Sends `SIGTERM` to the old process
|
||||
4. Old process: RSS cron stops, HTTP server drains in-flight requests (up to `kill_timeout: 15 000 ms`)
|
||||
5. PM2 sends `SIGKILL` if still alive after 15 s
|
||||
|
||||
During steps 1–2 (typically < 2 s for this service), incoming requests may queue briefly at the TCP layer. No requests are dropped under normal load.
|
||||
|
||||
**Caveats:**
|
||||
- If the new process crashes on startup, PM2 falls back to the old process automatically
|
||||
- A `git pull` that changes `package.json` requires `npm ci` before reload — covered by the CI pipeline
|
||||
- Cron-triggered RSS runs (every 30 min by default) are not interrupted; the next cron tick after reload uses the new code
|
||||
|
||||
---
|
||||
|
||||
## Manual Deploy (SSH — emergency bypass of CI)
|
||||
|
||||
Use only when CI is unavailable or for hotfix deployments.
|
||||
|
||||
```bash
|
||||
# 1. SSH into production CT
|
||||
ssh <prod-user>@<prod-host>
|
||||
|
||||
# 2. Pull latest main
|
||||
cd /opt/bitreport/ai-news-writer
|
||||
git pull --ff-only origin main
|
||||
|
||||
# 3. Install deps (skip if package.json unchanged)
|
||||
npm ci --omit=dev
|
||||
|
||||
# 4. Reload service
|
||||
pm2 startOrReload ecosystem.config.cjs --update-env
|
||||
|
||||
# 5. Smoke test
|
||||
bash tools/verify-deploy.sh http://127.0.0.1:3200
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Observability After Deploy
|
||||
|
||||
```bash
|
||||
# PM2 process status
|
||||
pm2 list
|
||||
|
||||
# Recent logs (last 50 lines)
|
||||
pm2 logs ai-news-writer --lines 50 --nostream
|
||||
|
||||
# Health endpoint
|
||||
curl -s http://127.0.0.1:3200/health | jq .
|
||||
|
||||
# Metrics (Prometheus format)
|
||||
curl -s http://127.0.0.1:3200/metrics | grep -E '^(engagement|articles).*total'
|
||||
```
|
||||
|
||||
See [OBSERVABILITY_BIT3601.md](./OBSERVABILITY_BIT3601.md) for the full metrics reference.
|
||||
|
||||
---
|
||||
|
||||
## First-Time LXC Container Setup
|
||||
|
||||
For provisioning a new LXC container from scratch (e.g., disaster recovery), see:
|
||||
- [DEPLOY_LXC.md](./DEPLOY_LXC.md) — full container setup and secrets injection
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- [ROLLBACK.md](./ROLLBACK.md) — rollback procedures
|
||||
- [DEPLOY_LXC.md](./DEPLOY_LXC.md) — first-time LXC provisioning
|
||||
- [DISASTER_RECOVERY.md](./DISASTER_RECOVERY.md) — full recovery scenarios
|
||||
- [SECRETS_MANAGEMENT.md](../SECRETS_MANAGEMENT.md) — secrets policy
|
||||
- [OBSERVABILITY_BIT3601.md](./OBSERVABILITY_BIT3601.md) — metrics and alerts
|
||||
- CI pipelines: `.gitea/workflows/deploy-staging.yml`, `.gitea/workflows/deploy-production.yml`
|
||||
- Smoke test: `tools/verify-deploy.sh`
|
||||
- PM2 config: `ecosystem.config.cjs`
|
||||
@@ -7,9 +7,15 @@ Related: Paperclip [BIT-353](/BIT/issues/BIT-353) — CT 223 / ai-news-writer ep
|
||||
|
||||
---
|
||||
|
||||
## Deploy & Start
|
||||
## Deploy & Rollback
|
||||
|
||||
All commands are documented in the root [`README.md`](../../README.md) — quick reference below.
|
||||
| Runbook | Purpose |
|
||||
|---------|---------|
|
||||
| [DEPLOYMENT.md](./DEPLOYMENT.md) (BIT-14770) | Day-to-day deployment: pipeline overview, staging/prod promote flow, zero-downtime strategy, env configs |
|
||||
| [ROLLBACK.md](./ROLLBACK.md) (BIT-14770) | Rollback procedures: decision tree, failure modes, recovery paths (RB-1 through RB-5) |
|
||||
| [DEPLOY_LXC.md](./DEPLOY_LXC.md) | First-time LXC container provisioning and secrets injection |
|
||||
|
||||
**Quick reference** — all commands documented in the root [`README.md`](../../README.md):
|
||||
|
||||
| Action | Command |
|
||||
|--------|---------|
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
# ai-news-writer — Rollback Runbook
|
||||
|
||||
**Service:** `ai-news-writer` | **CT:** 223 | **Port:** 3200
|
||||
**Process manager:** PM2 | **Repo:** `BitReport.pt/ai-news-writer`
|
||||
|
||||
Related: BIT-14770 (this runbook), [DEPLOYMENT.md](./DEPLOYMENT.md).
|
||||
|
||||
---
|
||||
|
||||
## Decision Tree: Rollback vs. Hotfix
|
||||
|
||||
```
|
||||
Is production broken after a deploy?
|
||||
│
|
||||
├── Yes → Is there a known-good previous commit?
|
||||
│ │
|
||||
│ ├── Yes → ROLLBACK (this runbook)
|
||||
│ └── No → HOTFIX (fix forward on a new branch)
|
||||
│
|
||||
└── No → Is the issue pre-existing (not caused by this deploy)?
|
||||
│
|
||||
├── Yes → File a bug, do not rollback
|
||||
└── Maybe → Check git log, check deploy_revision in /health
|
||||
```
|
||||
|
||||
**When to rollback:** service is unhealthy (`/health` returns `ok: false` or 5xx), articles failing to publish, or critical regression introduced by the deploy.
|
||||
|
||||
**When to hotfix:** rollback would revert a security fix, data migration, or secrets rotation that must stay in place.
|
||||
|
||||
---
|
||||
|
||||
## Automatic Rollback (CI pipelines)
|
||||
|
||||
Both staging and production pipelines include an `if: failure()` rollback step. When any deployment step fails:
|
||||
|
||||
1. Pipeline logs the last 2–3 commits
|
||||
2. Runs `git revert HEAD --no-edit && git push origin <branch>`
|
||||
3. Re-pulls, reinstalls deps, reloads PM2
|
||||
4. Exits with code 1 (pipeline marked as failed)
|
||||
|
||||
**No manual action needed** if the CI pipeline triggered the rollback. Verify the revert commit appeared in Gitea and that `/health` is green.
|
||||
|
||||
---
|
||||
|
||||
## Manual Rollback Procedures
|
||||
|
||||
### RB-1: Restart current version (service crash — no code change needed)
|
||||
|
||||
Use when PM2 process died but the code is fine.
|
||||
|
||||
```bash
|
||||
ssh <prod-user>@<prod-host>
|
||||
cd /opt/bitreport/ai-news-writer
|
||||
|
||||
pm2 restart ai-news-writer
|
||||
sleep 5
|
||||
bash tools/verify-deploy.sh http://127.0.0.1:3200
|
||||
```
|
||||
|
||||
If the process keeps crashing, look at logs first:
|
||||
```bash
|
||||
pm2 logs ai-news-writer --lines 100 --nostream
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### RB-2: Git rollback to previous commit
|
||||
|
||||
Use when the current code on disk is broken and needs to be reverted.
|
||||
|
||||
```bash
|
||||
ssh <prod-user>@<prod-host>
|
||||
cd /opt/bitreport/ai-news-writer
|
||||
|
||||
# 1. Check current and recent commits
|
||||
git log --oneline -5
|
||||
|
||||
# 2. Find the last known-good SHA (e.g., abc1234)
|
||||
# You can cross-reference with the deploy_revision in /health before the broken deploy
|
||||
|
||||
# 3. Revert (creates a new commit — preferred, keeps history clean)
|
||||
git revert HEAD --no-edit
|
||||
git push origin main
|
||||
|
||||
# 4. Or hard reset if the commit cannot be reverted cleanly (last resort)
|
||||
# git reset --hard <good-sha>
|
||||
# git push --force-with-lease origin main
|
||||
# WARNING: force-push rewrites history — notify the team
|
||||
|
||||
# 5. Reload service
|
||||
pm2 startOrReload ecosystem.config.cjs --update-env
|
||||
|
||||
# 6. Verify
|
||||
bash tools/verify-deploy.sh http://127.0.0.1:3200
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### RB-3: npm dependency rollback
|
||||
|
||||
Use when a dependency update broke the service.
|
||||
|
||||
```bash
|
||||
ssh <prod-user>@<prod-host>
|
||||
cd /opt/bitreport/ai-news-writer
|
||||
|
||||
# 1. Restore previous package-lock.json via git
|
||||
git checkout HEAD~1 -- package-lock.json
|
||||
|
||||
# 2. Reinstall locked deps
|
||||
npm ci --omit=dev
|
||||
|
||||
# 3. Reload service
|
||||
pm2 startOrReload ecosystem.config.cjs --update-env
|
||||
|
||||
# 4. Verify
|
||||
bash tools/verify-deploy.sh http://127.0.0.1:3200
|
||||
|
||||
# 5. Commit the restored lock file
|
||||
git add package-lock.json
|
||||
git commit -m "chore: revert package-lock.json to pre-rollback state"
|
||||
git push origin main
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### RB-4: Secrets rollback (wrong secret injected)
|
||||
|
||||
Use when `secrets.env` has incorrect values causing auth failures.
|
||||
|
||||
```bash
|
||||
# On the Proxmox host (or machine with Gitea token):
|
||||
cd /opt/bitreport/ai-news-writer
|
||||
|
||||
# 1. Update the secret value in Gitea UI:
|
||||
# Settings → Secrets → Edit <SECRET_NAME>
|
||||
|
||||
# 2. Re-inject (overwrites secrets.env on the CT)
|
||||
export GITEA_API_TOKEN="ghp_xxxxxxxxxxxx"
|
||||
./tools/deploy/lxc-secrets-inject.sh \
|
||||
--lxc-id 223 \
|
||||
--secrets OPENAI_API_KEY # or whichever secret changed
|
||||
|
||||
# 3. Restart service to pick up new secrets
|
||||
lxc exec 223 -- pm2 restart ai-news-writer
|
||||
|
||||
# 4. Verify
|
||||
lxc exec 223 -- bash tools/verify-deploy.sh http://127.0.0.1:3200
|
||||
```
|
||||
|
||||
See [SECRETS_MANAGEMENT.md](../SECRETS_MANAGEMENT.md) for the full secrets rotation procedure.
|
||||
|
||||
---
|
||||
|
||||
### RB-5: Full service wipe and redeploy
|
||||
|
||||
Use when the CT is corrupted or the filesystem is inconsistent.
|
||||
|
||||
```bash
|
||||
lxc exec 223 -- bash -c '
|
||||
pm2 delete ai-news-writer 2>/dev/null || true
|
||||
rm -rf /opt/bitreport/ai-news-writer
|
||||
'
|
||||
```
|
||||
|
||||
Then follow [DEPLOY_LXC.md](./DEPLOY_LXC.md) from Phase 2 (Code Deployment) onwards.
|
||||
|
||||
---
|
||||
|
||||
## Failure Modes and Recovery Paths
|
||||
|
||||
| Symptom | Likely cause | Recovery |
|
||||
|---------|-------------|---------|
|
||||
| `/health` returns `{"ok":false}` after deploy | Code regression | RB-2: git revert |
|
||||
| PM2 process in `errored` state | Startup crash (missing module, syntax error) | Check `pm2 logs`, then RB-2 or RB-3 |
|
||||
| `curl: (7) Failed to connect` on port 3200 | PM2 not running | RB-1: `pm2 restart` |
|
||||
| WordPress publish failures after deploy | WP credentials changed or code broke WP client | Check `/health` → `wordpress.ok`, then RB-4 or RB-2 |
|
||||
| RSS cron stops firing | Cron expression change or node-cron API change | Check `pm2 logs`, review last commit diff |
|
||||
| `Cannot find module` error in logs | `npm ci` not run after package.json change | `npm ci --omit=dev && pm2 reload` |
|
||||
| `ENOENT /etc/ai-news-writer/secrets.env` | Secrets file missing | RB-4: re-inject secrets |
|
||||
| High memory (`max_memory_restart` trigger) | Memory leak in new code | RB-2: git revert |
|
||||
| Health check passes but articles not generating | AI provider key expired or quota hit | RB-4: rotate key — not a code rollback |
|
||||
|
||||
---
|
||||
|
||||
## Identifying the Broken Commit
|
||||
|
||||
```bash
|
||||
# Check what's running now
|
||||
curl -s http://127.0.0.1:3200/health | jq '.deploy_revision'
|
||||
# Returns short SHA of currently running code
|
||||
|
||||
# Compare to what was running before the deploy
|
||||
git log --oneline -5
|
||||
|
||||
# View diff introduced by the suspect commit
|
||||
git show <sha>
|
||||
|
||||
# Check if the issue is reproducible on staging
|
||||
ssh <staging-user>@<staging-host> "cd /opt/bitreport/ai-news-writer && bash tools/verify-deploy.sh http://127.0.0.1:3200"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Post-Rollback Checklist
|
||||
|
||||
- [ ] `/health` returns `{"ok":true}` and correct `deploy_revision`
|
||||
- [ ] `pm2 list` shows `ai-news-writer` as `online`
|
||||
- [ ] No new errors in `pm2 logs ai-news-writer --lines 50 --nostream`
|
||||
- [ ] RSS cron active (check `rss_listener_active` or `rss_listener_last_tick_at` in `/health`)
|
||||
- [ ] Article pipeline resumes (monitor for 30–60 min)
|
||||
- [ ] Root cause identified and documented in a new issue or comment on the failing deploy issue
|
||||
- [ ] Broken commit reverted or hotfix PR opened
|
||||
|
||||
---
|
||||
|
||||
## Escalation
|
||||
|
||||
If a rollback cannot stabilize the service within 30 minutes:
|
||||
|
||||
1. Check [DISASTER_RECOVERY.md](./DISASTER_RECOVERY.md) for full container recovery scenarios
|
||||
2. Escalate to the on-call operator or DevOps
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- [DEPLOYMENT.md](./DEPLOYMENT.md) — deployment runbook
|
||||
- [DEPLOY_LXC.md](./DEPLOY_LXC.md) — first-time CT provisioning
|
||||
- [DISASTER_RECOVERY.md](./DISASTER_RECOVERY.md) — full recovery scenarios
|
||||
- [SECRETS_MANAGEMENT.md](../SECRETS_MANAGEMENT.md) — secrets rotation
|
||||
- CI pipelines with auto-rollback: `.gitea/workflows/deploy-staging.yml`, `.gitea/workflows/deploy-production.yml`
|
||||
- Smoke test: `tools/verify-deploy.sh`
|
||||
Reference in new issue
Block a user