Files
errors.bitreport.pt/docs/CI_PIPELINE.md
David SantosandClaude Sonnet 4.6 9a4939a2ba feat(ci): implement full CI/CD pipeline with Gitea Actions (BIT-14249)
- Enhance ci.yml: add collector-worker tests, Python 3 for ingest-api
  contract checker, lint/typecheck for all services, per-service Docker
  Buildx builds, and failure webhook notification job
- Add deploy.yml: workflow_run trigger on CI pass; staging auto-deploy
  via SSH; production deploy gated by manual environment approval;
  success/failure notifications for both environments
- Update docs/CI_PIPELINE.md: document full pipeline, required secrets,
  Gitea environment setup, manual deploy procedure, and failure handling

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-04-27 10:26:23 +00:00

197 lines
7.0 KiB
Markdown

# CI/CD Pipeline — BitReport Error Reporting
## Overview
The CI/CD pipeline for `errors.bitreport.pt` runs on Gitea Actions in two stages:
1. **CI** (`.gitea/workflows/ci.yml`) — runs on every push to `master` and on every pull request
2. **Deploy** (`.gitea/workflows/deploy.yml`) — triggers automatically after CI passes on `master`; deploys to staging automatically, and to production after manual approval
## CI Pipeline
### Jobs (run in parallel)
| Job | Service | What it does |
|-----|---------|--------------|
| `test-ingest-api` | `ingest-api/` | Node 20 + Python 3, `npm ci`, lint, typecheck, `npm test` (Vitest + Python contract check) |
| `test-collector-worker` | `collector-worker/` | Node 20, `npm ci`, lint, typecheck, `npm test` (Vitest) |
| `test-api` | `api/` | Node 20, `npm ci`, lint, typecheck, `npm test` (Vitest) |
| `lint-admin-ui` | `admin-ui/` | Node 20, `npm ci`, `npm run lint`, `npm run typecheck` |
| `test-web` | `web/` | Node 20, `npm ci`, lint, typecheck, `npm test`, `npm run test:coverage` |
| `docker-build` | ingest-api, collector-worker, admin-ui | Builds each Docker image separately via Buildx |
| `notify-failure` | — | Sends webhook notification if any job fails |
### Triggers
- Push to `master`
- Pull requests targeting `master`
### Python dependency (ingest-api)
The `test-ingest-api` job installs Python 3 because `npm test` in `ingest-api/` invokes `tools/ci/check_error_reporter_context_contract.py` to validate the client SDK contract.
### NPM caching
Each service caches its `node_modules` keyed on its own `package-lock.json`:
- `ingest-api/package-lock.json`
- `collector-worker/package-lock.json`
- `api/package-lock.json`
- `admin-ui/package-lock.json`
- `web/package-lock.json`
### Failure notifications
The `notify-failure` job runs only when any CI job fails. It sends a JSON POST to `secrets.NOTIFY_WEBHOOK_URL` (Slack/Teams/etc.). If the secret is unset it silently skips.
---
## Deploy Pipeline
File: `.gitea/workflows/deploy.yml`
### Flow
```
CI passes on master
│
▼
deploy-staging (automatic, environment: staging)
│ success
▼
deploy-production (requires manual approval in Gitea, environment: production)
```
If CI fails, neither deploy job runs.
### Staging deploy
- **Trigger:** Automatic after CI workflow completes successfully on `master`
- **Environment:** `staging` (configure in Gitea → Repository → Environments)
- **Mechanism:** SSH into the staging host, `git reset --hard origin/master`, `docker compose up -d --build`
- **Notification:** Webhook on success and failure
### Production deploy
- **Trigger:** Automatic after `deploy-staging` succeeds, but **gated by manual approval**
- **Environment:** `production` (configure in Gitea → Repository → Environments → protection rules)
- **Mechanism:** Same SSH deploy procedure as staging against the production host
- **Notification:** Webhook on success and failure
---
## Required Secrets
Configure these in Gitea → Repository → Settings → Secrets:
| Secret | Used in | Purpose |
|--------|---------|---------|
| `STAGING_HOST` | deploy.yml | Hostname or IP of the staging server |
| `STAGING_PORT` | deploy.yml | SSH port (default: 22) |
| `STAGING_USER` | deploy.yml | SSH username on the staging server |
| `STAGING_SSH_KEY` | deploy.yml | Private SSH key (PEM format) for staging access |
| `PROD_HOST` | deploy.yml | Hostname or IP of the production server |
| `PROD_PORT` | deploy.yml | SSH port (default: 22) |
| `PROD_USER` | deploy.yml | SSH username on the production server |
| `PROD_SSH_KEY` | deploy.yml | Private SSH key (PEM format) for production access |
| `NOTIFY_WEBHOOK_URL` | ci.yml, deploy.yml | Webhook URL for failure/success notifications (Slack/Teams) |
---
## Gitea Environment Setup
### Creating environments
1. Go to **Repository → Settings → Environments**
2. Create `staging` with no protection rules (auto-deploy)
3. Create `production` with:
- **Required reviewers:** add the team leads who must approve production deploys
- **Wait timer:** optional (e.g., 10 minutes after staging deploy)
### Adding secrets to environments
Environment-scoped secrets (e.g., `STAGING_SSH_KEY`) can be placed at repository level. If staging and production keys differ, use environment-scoped secrets under each environment instead.
---
## Deploy Procedure (Manual)
If the pipeline cannot run (e.g., Gitea Actions runner is down), deploy manually:
```bash
# On the target server (staging or production)
cd /opt/bitreport/errors.bitreport.pt
git fetch origin master
git reset --hard origin/master
docker compose up -d --build ingest-api collector-worker admin-ui
docker compose ps
```
Before deploying manually, ensure:
- All CI checks are green on the commit you are deploying
- The database migration container has run for any schema changes (`docker compose run --rm migrate`)
---
## Failure Handling
### CI failure
1. Check the failing job in Gitea Actions
2. Fix the issue in a branch and open a PR
3. CI re-runs automatically on push to the PR branch
4. Merge only after all checks are green
### Deploy failure
1. Check the deploy job logs in Gitea Actions
2. SSH into the affected host to investigate (check `docker compose logs`)
3. If staging failed, fix and push — the pipeline will retry from CI
4. If production failed but staging succeeded, re-run the production job from the Gitea Actions UI after investigating
### Common failures
| Job | Cause | Resolution |
|-----|-------|-----------|
| `test-ingest-api` | Vitest failure or Python contract mismatch | Check `ingest-api/` tests and `tools/ci/` contract file |
| `test-collector-worker` | Vitest failure | Check `collector-worker/` tests |
| `test-api` | Vitest failure | Check `api/` tests |
| `lint-admin-ui` | ESLint or TypeScript errors | Run `npm run lint && npm run typecheck` locally in `admin-ui/` |
| `test-web` | Test failure or coverage threshold | Run `npm test` locally in `web/` |
| `docker-build` | Dockerfile syntax error or missing layer | Run `docker build ./ingest-api` locally to reproduce |
| `deploy-staging` | SSH key rejected or host unreachable | Verify `STAGING_HOST`, `STAGING_USER`, `STAGING_SSH_KEY` secrets |
| `deploy-production` | Same as staging | Verify `PROD_*` secrets |
---
## Local Testing
Replicate CI locally before pushing:
```bash
# ingest-api (requires Python 3)
cd ingest-api && npm ci && npm test && cd ..
# collector-worker
cd collector-worker && npm ci && npm test && cd ..
# api
cd api && npm ci && npm test && cd ..
# admin-ui
cd admin-ui && npm ci && npm run lint && npm run typecheck && cd ..
# web
cd web && npm ci && npm test && npm run test:coverage && cd ..
# Docker builds
docker build ./ingest-api -t errors-bitreport/ingest-api:local
docker build ./collector-worker -t errors-bitreport/collector-worker:local
docker build ./admin-ui -t errors-bitreport/admin-ui:local
```
---
**Last updated:** 2026-04-27
**Maintained by:** Maria Papoila
**Related issues:** [BIT-14249](/BIT/issues/BIT-14249), [BIT-2477](/BIT/issues/BIT-2477), [BIT-2481](/BIT/issues/BIT-2481)