diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 171ec4f..7b7e962 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -15,6 +15,11 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Set up Python 3 + uses: actions/setup-python@v5 + with: + python-version: '3.x' + - name: Set up Node.js uses: actions/setup-node@v4 with: @@ -26,10 +31,43 @@ jobs: working-directory: ingest-api run: npm ci + - name: Lint and typecheck + working-directory: ingest-api + run: | + npm run lint --if-present + npm run typecheck --if-present + - name: Run tests working-directory: ingest-api run: npm test + test-collector-worker: + name: Test collector-worker + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + cache: 'npm' + cache-dependency-path: 'collector-worker/package-lock.json' + + - name: Install dependencies + working-directory: collector-worker + run: npm ci + + - name: Lint and typecheck + working-directory: collector-worker + run: | + npm run lint --if-present + npm run typecheck --if-present + + - name: Run tests + working-directory: collector-worker + run: npm test + test-api: name: Test api runs-on: ubuntu-latest @@ -47,6 +85,12 @@ jobs: working-directory: api run: npm ci + - name: Lint and typecheck + working-directory: api + run: | + npm run lint --if-present + npm run typecheck --if-present + - name: Run tests working-directory: api run: npm test @@ -77,7 +121,7 @@ jobs: run: npm run typecheck test-web: - name: Test web + name: Test and lint web runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -93,6 +137,12 @@ jobs: working-directory: web run: npm ci + - name: Lint and typecheck + working-directory: web + run: | + npm run lint --if-present + npm run typecheck --if-present + - name: Run tests working-directory: web run: npm test @@ -102,7 +152,7 @@ jobs: run: npm run test:coverage docker-build: - name: Validate Docker build + name: Validate Docker builds runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -110,10 +160,49 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Build Docker images - run: docker compose build + - name: Build ingest-api image + uses: docker/build-push-action@v5 + with: + context: ./ingest-api + push: false + tags: errors-bitreport/ingest-api:ci + + - name: Build collector-worker image + uses: docker/build-push-action@v5 + with: + context: ./collector-worker + push: false + tags: errors-bitreport/collector-worker:ci + + - name: Build admin-ui image + uses: docker/build-push-action@v5 + with: + context: ./admin-ui + push: false + tags: errors-bitreport/admin-ui:ci + + notify-failure: + name: Notify on CI failure + runs-on: ubuntu-latest + needs: + - test-ingest-api + - test-collector-worker + - test-api + - lint-admin-ui + - test-web + - docker-build + if: failure() + steps: + - name: Send failure webhook env: - ER_MYSQL_ROOT_PASSWORD: devrootchange - ER_MYSQL_USER: er_app - ER_MYSQL_PASSWORD: er_app_secret - ER_MYSQL_DATABASE: error_reporting + WEBHOOK_URL: ${{ secrets.NOTIFY_WEBHOOK_URL }} + REPO: ${{ github.repository }} + REF: ${{ github.ref_name }} + SHA: ${{ github.sha }} + ACTOR: ${{ github.actor }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + [ -n "$WEBHOOK_URL" ] && curl -fsSL -X POST "$WEBHOOK_URL" \ + -H "Content-Type: application/json" \ + -d "{\"text\":\"❌ CI failed on *${REPO}*\\nBranch: \`${REF}\`\\nCommit: \`${SHA}\`\\nActor: ${ACTOR}\\nRun: ${RUN_URL}\"}" \ + || true diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..4ace1c1 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,124 @@ +name: Deploy + +on: + workflow_run: + workflows: + - CI + types: + - completed + branches: + - master + +jobs: + deploy-staging: + name: Deploy to staging + runs-on: ubuntu-latest + environment: staging + if: ${{ github.event.workflow_run.conclusion == 'success' }} + steps: + - uses: actions/checkout@v4 + + - name: Setup SSH key + run: | + mkdir -p ~/.ssh + echo "${{ secrets.STAGING_SSH_KEY }}" > ~/.ssh/deploy_key + chmod 600 ~/.ssh/deploy_key + ssh-keyscan -p "${{ secrets.STAGING_PORT || '22' }}" "${{ secrets.STAGING_HOST }}" >> ~/.ssh/known_hosts + + - name: Deploy to staging + env: + HOST: ${{ secrets.STAGING_HOST }} + PORT: ${{ secrets.STAGING_PORT || '22' }} + USER: ${{ secrets.STAGING_USER }} + run: | + ssh -i ~/.ssh/deploy_key -p "$PORT" "$USER@$HOST" bash -s << 'REMOTE' + set -e + 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 + echo "Staging deploy complete: $(date -u +%Y-%m-%dT%H:%M:%SZ)" + REMOTE + + - name: Notify staging deploy success + if: success() + env: + WEBHOOK_URL: ${{ secrets.NOTIFY_WEBHOOK_URL }} + SHA: ${{ github.sha }} + REPO: ${{ github.repository }} + run: | + [ -n "$WEBHOOK_URL" ] && curl -fsSL -X POST "$WEBHOOK_URL" \ + -H "Content-Type: application/json" \ + -d "{\"text\":\"βœ… Staging deploy succeeded\\nRepo: *${REPO}*\\nCommit: \`${SHA}\`\"}" \ + || true + + - name: Notify staging deploy failure + if: failure() + env: + WEBHOOK_URL: ${{ secrets.NOTIFY_WEBHOOK_URL }} + SHA: ${{ github.sha }} + REPO: ${{ github.repository }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + [ -n "$WEBHOOK_URL" ] && curl -fsSL -X POST "$WEBHOOK_URL" \ + -H "Content-Type: application/json" \ + -d "{\"text\":\"❌ Staging deploy FAILED\\nRepo: *${REPO}*\\nCommit: \`${SHA}\`\\nRun: ${RUN_URL}\"}" \ + || true + + deploy-production: + name: Deploy to production + runs-on: ubuntu-latest + environment: production + needs: deploy-staging + if: ${{ github.event.workflow_run.conclusion == 'success' }} + steps: + - uses: actions/checkout@v4 + + - name: Setup SSH key + run: | + mkdir -p ~/.ssh + echo "${{ secrets.PROD_SSH_KEY }}" > ~/.ssh/deploy_key + chmod 600 ~/.ssh/deploy_key + ssh-keyscan -p "${{ secrets.PROD_PORT || '22' }}" "${{ secrets.PROD_HOST }}" >> ~/.ssh/known_hosts + + - name: Deploy to production + env: + HOST: ${{ secrets.PROD_HOST }} + PORT: ${{ secrets.PROD_PORT || '22' }} + USER: ${{ secrets.PROD_USER }} + run: | + ssh -i ~/.ssh/deploy_key -p "$PORT" "$USER@$HOST" bash -s << 'REMOTE' + set -e + 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 + echo "Production deploy complete: $(date -u +%Y-%m-%dT%H:%M:%SZ)" + REMOTE + + - name: Notify production deploy success + if: success() + env: + WEBHOOK_URL: ${{ secrets.NOTIFY_WEBHOOK_URL }} + SHA: ${{ github.sha }} + REPO: ${{ github.repository }} + run: | + [ -n "$WEBHOOK_URL" ] && curl -fsSL -X POST "$WEBHOOK_URL" \ + -H "Content-Type: application/json" \ + -d "{\"text\":\"πŸš€ Production deploy succeeded\\nRepo: *${REPO}*\\nCommit: \`${SHA}\`\"}" \ + || true + + - name: Notify production deploy failure + if: failure() + env: + WEBHOOK_URL: ${{ secrets.NOTIFY_WEBHOOK_URL }} + SHA: ${{ github.sha }} + REPO: ${{ github.repository }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + [ -n "$WEBHOOK_URL" ] && curl -fsSL -X POST "$WEBHOOK_URL" \ + -H "Content-Type: application/json" \ + -d "{\"text\":\"πŸ”₯ Production deploy FAILED\\nRepo: *${REPO}*\\nCommit: \`${SHA}\`\\nRun: ${RUN_URL}\"}" \ + || true diff --git a/docs/CI_PIPELINE.md b/docs/CI_PIPELINE.md index 13abc1f..a23bd72 100644 --- a/docs/CI_PIPELINE.md +++ b/docs/CI_PIPELINE.md @@ -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)