Files
ai-news-writer/docs/runbooks/DEPLOYMENT.md
T
XikaandCursor ca8bec5ca3
ai-news-writer-ci / test (pull_request) Successful in 32s
ci(staging): deploy after CI on main; pull triggering branch (BIT-21911)
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]>
2026-05-12 12:05:48 +00:00

8.3 KiB
Raw Blame History

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

  1. Go to Gitea Actions → ai-news-writer-deploy-staging → Run workflow
  2. Select branch develop or main (must match the branch you want on the staging host)
  3. Click Run

The pipeline:

  1. git fetch origin && git pull --ff-only origin <branch> where <branch> is the branch that passed CI (develop or main) or the branch you chose for Run workflow
  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)

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

  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)

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:

  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.

# 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:


References