name: DH V2 CI on: pull_request: branches: [main] push: branches: [main] permissions: contents: read concurrency: group: dhv2-ci-${{ github.ref }} cancel-in-progress: true jobs: api: name: API · typecheck, tests, build runs-on: ubuntu-latest defaults: run: working-directory: api-v3 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '24' cache: npm cache-dependency-path: api-v3/package-lock.json - run: npm ci - run: npm run typecheck - run: npm test - run: npm run build web: name: WEB · typecheck, build runs-on: ubuntu-latest defaults: run: working-directory: web-v2 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '24' cache: npm cache-dependency-path: web-v2/package-lock.json - run: npm ci - run: npm run typecheck - name: F3.1 WEB contract run: bash ../scripts/check-f3-1-web-contract.sh - run: npm run build contract: name: Docker / scripts contract runs-on: ubuntu-latest needs: [api, web] steps: - uses: actions/checkout@v4 - name: Validate shell scripts run: | while IFS= read -r -d '' script; do bash -n "$script" done < <(find scripts -type f -name '*.sh' -print0) - name: Validate deploy preflight parity run: | grep -Fq -- '$STAGE/docker-compose.yml:/docker-compose.yml:ro' scripts/deploy-github.sh grep -Fq -- '$STAGE/web-v2:/web-v2:ro' scripts/deploy-github.sh grep -Fq -- '$STAGE/android-app:/android-app:ro' scripts/deploy-github.sh - name: Validate Compose run: docker compose --env-file .env.example config >/dev/null - name: Rehearse migrations on clean PostGIS run: | set -Eeuo pipefail cleanup() { docker compose --env-file .env.example --profile tools down -v --remove-orphans >/dev/null 2>&1 || true } trap cleanup EXIT cleanup docker compose --env-file .env.example up -d db # Historical production reset is a one-shot migration that expects the # production admin. Prove the clean chain reaches that exact guard, # mark only that historical reset as applied, then continue the chain. bootstrap_log="$(mktemp)" set +e docker compose --env-file .env.example --profile tools run --build --rm migrate 2>&1 | tee "$bootstrap_log" bootstrap_status=${PIPESTATUS[0]} set -e if [ "$bootstrap_status" -eq 0 ]; then echo "ERROR: clean migration rehearsal unexpectedly passed the historical production reset." >&2 exit 1 fi grep -Fq 'Production reset aborted: expected exactly one username admin, found 0' "$bootstrap_log" || { echo "ERROR: migration rehearsal failed before the expected historical production-reset guard." >&2 exit 1 } rm -f "$bootstrap_log" docker compose --env-file .env.example exec -T db \ psql -v ON_ERROR_STOP=1 -U dhv2_owner -d dhv2 <<'SQL' DO $$ DECLARE reset_rows integer; BEGIN SELECT COUNT(*) INTO reset_rows FROM typeorm_migrations WHERE name = 'ResetProductionOperationalData1788652800000'; IF reset_rows <> 0 THEN RAISE EXCEPTION 'CI one-shot bypass expected reset migration to be pending, found % rows', reset_rows; END IF; INSERT INTO typeorm_migrations ("timestamp", name) VALUES (1788652800000, 'ResetProductionOperationalData1788652800000'); END $$; SQL docker compose --env-file .env.example --profile tools run --rm migrate # F5.1 intentionally ends with zero operational/domain instances. The # technical family and finding masters remain, but territory preload, # imports, applicability links and old audits are deliberately gone. docker compose --env-file .env.example exec -T db \ psql -v ON_ERROR_STOP=1 -U dhv2_owner -d dhv2 <<'SQL' DO $$ DECLARE f5_migrations integer; domain_assets integer; audits integer; applicability integer; territory_sources integer; source_installations integer; source_subinstallations integer; source_findings integer; department_types integer; area_department_rules integer; BEGIN SELECT COUNT(*) INTO f5_migrations FROM typeorm_migrations WHERE name IN ( 'F5InventoryPhysicalInstance1790087100000', 'F5CanonicalInventoryHierarchy1790087150000', 'F5AuthoritativeTerritory1790087200000', 'F5OperationalContextCompatibility1790087250000', 'F5AuthoritativeInventoryCatalog1790087300000', 'F51CleanManualInventory1790087400000' ); IF f5_migrations <> 6 THEN RAISE EXCEPTION 'Expected 6 F5/F5.1 migrations, got %', f5_migrations; END IF; SELECT COUNT(*) INTO domain_assets FROM assets; IF domain_assets <> 0 THEN RAISE EXCEPTION 'F5.1 clean start must contain 0 Assets, got %', domain_assets; END IF; SELECT COUNT(*) INTO audits FROM audit_events; IF audits <> 0 THEN RAISE EXCEPTION 'F5.1 clean start must contain 0 audit events, got %', audits; END IF; SELECT COUNT(*) INTO applicability FROM finding_catalog_item_inventory_families; IF applicability <> 0 THEN RAISE EXCEPTION 'F5.1 clean start must contain 0 finding applicability links, got %', applicability; END IF; SELECT COUNT(*) INTO territory_sources FROM source_documents WHERE document_number='DH-F5-TERRITORY'; IF territory_sources <> 0 THEN RAISE EXCEPTION 'F5.1 must remove the old territory source preload, got % rows', territory_sources; END IF; SELECT COUNT(*) FILTER (WHERE level='INSTALLATION'), COUNT(*) FILTER (WHERE level='SUBINSTALLATION') INTO source_installations,source_subinstallations FROM inventory_families WHERE is_active=true AND source_reference LIKE 'F5:final_modelov2.xlsx%'; IF source_installations <> 14 OR source_subinstallations <> 109 THEN RAISE EXCEPTION 'F5.1 must preserve technical family masters: installations %, subinstallations %', source_installations,source_subinstallations; END IF; SELECT COUNT(*) INTO source_findings FROM finding_catalog_items item JOIN finding_categories category ON category.id=item.category_id WHERE lower(category.code)='f5model' AND item.is_active=true; IF source_findings <> 177 THEN RAISE EXCEPTION 'F5.1 must preserve finding master catalog, got %', source_findings; END IF; SELECT COUNT(*) INTO department_types FROM asset_types WHERE lower(code)='departamento' AND can_be_root=true AND is_active=true; IF department_types <> 1 THEN RAISE EXCEPTION 'Expected one active root Departamento type, got %', department_types; END IF; SELECT COUNT(*) INTO area_department_rules FROM asset_type_parent_rules rule JOIN asset_types child ON child.id=rule.child_type_id JOIN asset_types parent ON parent.id=rule.parent_type_id WHERE lower(child.code)='area' AND lower(parent.code)='departamento'; IF area_department_rules <> 1 THEN RAISE EXCEPTION 'Expected Area → Departamento canonical rule, got %', area_department_rules; END IF; END $$; SQL # F5.1 is intentionally one-way: production rollback is the PRE database # backup, not migration:revert. Prove instead that the completed chain is # idempotent and has no pending migration on a second run. rerun_log="$(mktemp)" docker compose --env-file .env.example --profile tools run --rm migrate 2>&1 | tee "$rerun_log" grep -Eq 'No pending migrations|Applied migrations: 0' "$rerun_log" || { echo "ERROR: F5.1 migration chain is not idempotent." >&2 cat "$rerun_log" >&2 exit 1 } rm -f "$rerun_log" - name: VPS-equivalent isolated API preflight run: | set -Eeuo pipefail image="dhv2-api:ci-vps-preflight-${GITHUB_SHA::12}" docker build --target builder -t "$image" api-v3 docker run --rm \ -v "$PWD/api-v3/test:/app/test:ro" \ -v "$PWD/api-v3/tsconfig.test.json:/app/tsconfig.test.json:ro" \ -v "$PWD/docker-compose.yml:/docker-compose.yml:ro" \ -v "$PWD/web-v2:/web-v2:ro" \ -v "$PWD/android-app:/android-app:ro" \ "$image" npm test docker image rm "$image" >/dev/null 2>&1 || true - name: Build production images run: docker compose --env-file .env.example build api migrate web