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]>
This commit is contained in:
David SantosandClaude Sonnet 4.6 committed 2026-04-27 10:26:23 +00:00
1 parent da57d2a444
commit 9a4939a2ba
3 files changed
+395 -142

No files matched your search

+174 -134
View File
@@ -2,155 +2,195 @@
## Overview
This document describes the Gitea Actions CI/CD pipeline for the `errors.bitreport.pt` project. The pipeline automatically tests, validates, and builds all services on every commit to the `master` branch and on pull requests.
The CI/CD pipeline for `errors.bitreport.pt` runs on Gitea Actions in two stages:
## Pipeline Structure
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
The CI pipeline runs four independent validation jobs in parallel:
## CI Pipeline
### 1. **Test ingest-api** (`test-ingest-api`)
### Jobs (run in parallel)
Tests the Node.js ingest API service using Vitest.
| 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 |
- **Service:** `ingest-api/`
- **Steps:**
- Node.js 20 setup with npm cache
- `npm ci` — install dependencies with exact versions
- `npm test` — runs Vitest unit tests + Python validation of error reporter context contract
- **Purpose:** Validates ingest API logic and contract with client SDKs
### Triggers
### 2. **Test api** (`test-api`)
- Push to `master`
- Pull requests targeting `master`
Tests the Node.js admin API service using Vitest.
### Python dependency (ingest-api)
- **Service:** `api/`
- **Steps:**
- Node.js 20 setup with npm cache
- `npm ci` — install dependencies
- `npm test` — runs Vitest unit tests
- **Purpose:** Validates admin API logic and type safety
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.
### 3. **Lint and Typecheck admin-ui** (`lint-admin-ui`)
### NPM caching
Validates frontend code quality and type safety.
- **Service:** `admin-ui/`
- **Steps:**
- Node.js 20 setup with npm cache
- `npm ci` — install dependencies
- `npm run lint` — Next.js ESLint validation
- `npm run typecheck` — TypeScript type checking
- **Purpose:** Ensures admin UI code quality before runtime testing
### 4. **Validate Docker Build** (`docker-build`)
Builds all Docker images defined in `docker-compose.yml`.
- **Services:** ingest-api, collector-worker, admin-ui
- **Steps:**
- Docker Buildx setup for multi-platform builds
- `docker compose build` — validates all Dockerfiles and layers
- Environment variables provided for build-time configuration
- **Purpose:** Ensures production container images build cleanly
## Trigger Events
The pipeline runs automatically on:
1. **Push to master** — every commit merged to main branch
2. **Pull requests** — on creation and on new pushes to PR branches
## Dependencies and Runtime
- **Node.js version:** 20 (as specified in `ingest-api/package.json` and `api/package.json`)
- **Runtime:** Ubuntu latest
- **Parallel execution:** All four jobs run in parallel; no job waits for another
- **Total runtime:** ~5–10 minutes (depending on dependency installation and test suite size)
## Caching
NPM package caches are built per service based on their `package-lock.json` files:
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`
This accelerates dependency installation on subsequent runs.
### Failure notifications
## Configuration
### Workflow file
Location: `.gitea/workflows/ci.yml`
Modify this file to:
- Add or remove services
- Change trigger branches
- Adjust Node.js version
- Add new jobs (e.g., integration tests, security scanning)
### Environment Variables (Docker Build)
The `docker-build` job provides standard dev environment values:
```yaml
ER_MYSQL_ROOT_PASSWORD: devrootchange
ER_MYSQL_USER: er_app
ER_MYSQL_PASSWORD: er_app_secret
ER_MYSQL_DATABASE: error_reporting
```
To change defaults (e.g., for staging builds), update these in the workflow file.
## Failure Handling
If any job fails, the entire pipeline is marked as failed. Pull requests will show a **red check** and cannot be merged until all jobs pass.
### Common Failure Causes
| Job | Cause | Resolution |
|-----|-------|-----------|
| `test-ingest-api` | Failed Vitest or Python validation | Check ingest API tests and client SDK contract |
| `test-api` | Failed Vitest | Check API tests and type safety |
| `lint-admin-ui` | Lint errors or type errors | Fix ESLint violations and TypeScript errors |
| `docker-build` | Layer build failure, missing Dockerfile | Check Dockerfile syntax and base image availability |
## Local Testing
To replicate the CI pipeline locally:
```bash
# Test ingest-api
cd ingest-api
npm ci && npm test
cd ..
# Test api
cd api
npm ci && npm test
cd ..
# Test admin-ui
cd admin-ui
npm ci && npm run lint && npm run typecheck
cd ..
# Validate Docker build
docker compose build
```
## Future Enhancements
Potential pipeline extensions:
1. **Integration Tests** — Run full stack with docker-compose (requires longer runtime)
2. **Performance Benchmarks** — Track API response times over time
3. **Security Scanning** — SAST, dependency vulnerability checks
4. **Coverage Reports** — Publish code coverage metrics
5. **Artifact Publishing** — Push built images to registry on master merges
6. **Staged Deployments** — Auto-deploy to staging/production environments
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.
---
**Last updated:** 2026-04-12
**Maintained by:** Planner Agent (Patrícia Santos)
**Related issues:** [BIT-2481](/BIT/issues/BIT-2481), [BIT-2498](/BIT/issues/BIT-2498)
## 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)