Staging deploy previously chained only off develop CI and hard-coded git pull develop. Remote develop was missing or protected while PR #18 merged to main. Watch develop and main; set STAGING_GIT_BRANCH from workflow_run or dispatch ref; align rollback push and pull with the same branch. Docs: DEPLOYMENT runbook diagram and manual-dispatch notes. Co-authored-by: Cursor <[email protected]>
8.3 KiB
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 or main branch]
│
▼
┌──────────────────────────────┐
│ Gitea CI (test.yml) │
│ lint + audit + test │
└──────────┬───────────────────┘
│ success (same branch)
▼
┌──────────────────────────────┐
│ Staging auto-deploy │ ← deploy-staging.yml
│ git pull that branch + npm │
│ ci + pm2 reload + verify │
└──────────┬───────────────────┘
│ 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 or main (the workflow pulls the same branch that passed CI — see .gitea/workflows/deploy-staging.yml).
Production requires an explicit manual trigger with the word PROMOTE.
Environment Reference
| Setting | Dev (local) | Staging | Production |
|---|---|---|---|
| Branch | any | develop or main (matches CI branch) |
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 and 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
- Go to Gitea Actions →
ai-news-writer-deploy-staging→ Run workflow - Select branch
developormain(must match the branch you want on the staging host) - Click Run
The pipeline:
git fetch origin && git pull --ff-only origin <branch>where<branch>is the branch that passed CI (developormain) or the branch you chose for Run workflownpm ci --omit=dev && npm run lint --quietpm2 startOrReload ecosystem.config.cjs --update-env- Waits 5 s, runs
tools/verify-deploy.sh http://127.0.0.1:3200 - Fails → automatic rollback (see 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)
Trigger production deploy
- Open Gitea Actions →
ai-news-writer-deploy-production→ Run workflow - Branch:
main - Confirmation field: type exactly
PROMOTE - Deployment notes: enter short description (e.g., commit hash or reason)
- Click Run workflow
The pipeline:
- Validates
PROMOTEconfirmation (exits if wrong) - Logs pre-deployment state (
pm2 list, last commit) git pull --ff-only origin mainnpm ci --omit=dev && npm run lint --quietpm2 startOrReload ecosystem.config.cjs --update-env- Waits 10 s, runs
tools/verify-deploy.sh http://127.0.0.1:3200 - External smoke:
curl $PROD_URL/health— must return"ok":true - On any failure → automatic rollback (see ROLLBACK.md)
Post-deploy verification
# 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:
{ "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:
- Starts new process with updated code and env
- Waits for the new process to emit
readyor for the listen timeout - Sends
SIGTERMto the old process - Old process: RSS cron stops, HTTP server drains in-flight requests (up to
kill_timeout: 15 000 ms) - PM2 sends
SIGKILLif 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 pullthat changespackage.jsonrequiresnpm cibefore 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.
# 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
# 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 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 — full container setup and secrets injection
References
- ROLLBACK.md — rollback procedures
- DEPLOY_LXC.md — first-time LXC provisioning
- DISASTER_RECOVERY.md — full recovery scenarios
- SECRETS_MANAGEMENT.md — secrets policy
- 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