diff --git a/.env.example b/.env.example index c9b06a0..f808507 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ # ---- image coordinates ---- -REGISTRY=gitea.example.com/vigilcare +# Must match CD default / vars.REGISTRY and docker-compose.prod.yml image prefixes. +REGISTRY=git.vectur45.com/Trent/vigilcare-clinical IMAGE_TAG=v1.0.0 # ---- exposed ports on the production host ---- diff --git a/.gitea/workflows/cd.yml b/.gitea/workflows/cd.yml index 47c18cd..3c62a9f 100644 --- a/.gitea/workflows/cd.yml +++ b/.gitea/workflows/cd.yml @@ -1,10 +1,11 @@ -# Phase 36 Step 12 — build images, migrate, deploy on version tags. -# Requires act_runner with docker, curl, ssh, scp, bash and label ubuntu-latest. +# Build images, migrate, deploy on version tags. +# Cap buildx + one bake target at a time (docs/CICD). GITEA_TOKEN cannot push — +# REGISTRY_TOKEN must be a PAT. # # Secrets: REGISTRY_USERNAME, REGISTRY_TOKEN, PG_CONNECTION_DDL, # DEPLOY_HOST, DEPLOY_USER, DEPLOY_SSH_KEY -# Variables: PROD_API_URL (e.g. https://api.vigilcare-clinical.vectur45.com), -# REGISTRY (optional; defaults below) +# Variables: REGISTRY (optional), PROD_API_URL (baked into dashboard), +# MIGRATE_DOCKER_NETWORK (default shared-services) name: CD on: @@ -17,7 +18,7 @@ on: required: false env: - REGISTRY: gitea.example.com/vigilcare + REGISTRY: git.vectur45.com/Trent/vigilcare-clinical jobs: build-and-push: @@ -48,58 +49,110 @@ jobs: | docker login "${REGISTRY%%/*}" \ -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin - - name: Build and push clinical-api - # Context MUST be repo root — ClinicalContracts is a sibling ProjectReference. + - name: Bake and push images + env: + TAG: ${{ steps.meta.outputs.tag }} + VITE_API_URL: ${{ vars.PROD_API_URL }} + VITE_SHOW_OPS: "false" run: | - docker build -f VigilCareClinicalAPI/Dockerfile \ - -t "${REGISTRY}/clinical-api:${{ steps.meta.outputs.tag }}" \ - -t "${REGISTRY}/clinical-api:latest" . - docker push "${REGISTRY}/clinical-api:${{ steps.meta.outputs.tag }}" - docker push "${REGISTRY}/clinical-api:latest" - - - name: Build and push ward-gateway - # Same repo-root context as docker-compose.yml's ward-gateway-api service. - run: | - docker build -f VigilCare.WardGateway/Dockerfile \ - -t "${REGISTRY}/ward-gateway:${{ steps.meta.outputs.tag }}" \ - -t "${REGISTRY}/ward-gateway:latest" . - docker push "${REGISTRY}/ward-gateway:${{ steps.meta.outputs.tag }}" - docker push "${REGISTRY}/ward-gateway:latest" - - - name: Build and push dashboard - # Context is vigilcare-dashboard/ — package.json and nginx.conf live there. - run: | - docker build -f vigilcare-dashboard/Dockerfile \ - --build-arg VITE_API_URL="${{ vars.PROD_API_URL }}" \ - -t "${REGISTRY}/dashboard:${{ steps.meta.outputs.tag }}" \ - -t "${REGISTRY}/dashboard:latest" vigilcare-dashboard - docker push "${REGISTRY}/dashboard:${{ steps.meta.outputs.tag }}" - docker push "${REGISTRY}/dashboard:latest" - - migrate: - needs: build-and-push - runs-on: ubuntu-latest - # Checkout must run on the job host (Node). Do not use job-level container:. - # Build the EF bundle via Dockerfile --target migrate (context upload), not - # docker run -v — under act_runner bind mounts resolve on the Docker host. - steps: - - uses: actions/checkout@v4 - - - name: Build migration bundle - run: | - docker build -f VigilCareClinicalAPI/Dockerfile --target migrate \ - -t vigilcare-migrate-bundle:local . - cid=$(docker create vigilcare-migrate-bundle:local) + bash scripts/cap-buildx-builder.sh vigilcare-builder + bake_one() { + local target="$1" + shift + echo "=== bake ${target} ===" + BUILDX_NO_DEFAULT_ATTESTATIONS=1 docker buildx bake \ + --progress=plain \ + "$target" \ + "$@" \ + -f docker-bake.hcl + } + bake_one api --push --set api.output=type=image,push=true + sleep 8 + bake_one gateway --push --set gateway.output=type=image,push=true + sleep 8 + if [ -n "${VITE_API_URL}" ]; then + bake_one dashboard --push --set dashboard.output=type=image,push=true + sleep 8 + else + echo "PROD_API_URL unset — skipping dashboard bake" >&2 + exit 1 + fi + bake_one migrate --set migrate.output=type=docker + cid=$(docker create vigilcare-migrate-bundle:local /out/migrate-api) docker cp "$cid:/out/migrate-api" ./migrate-api docker rm "$cid" chmod +x ./migrate-api - # Runs while the previous release is still serving traffic, so every - # migration must be backwards-compatible with the outgoing image. - # See Step 6 — expand-then-contract. - # Self-contained linux-x64 binary — runs on the job host. + - name: Upload migration bundle + uses: actions/upload-artifact@v4 + with: + name: migrate-api + path: migrate-api + if-no-files-found: error + + migrate: + needs: build-and-push + runs-on: ubuntu-latest + steps: + - name: Download migration bundle + uses: actions/download-artifact@v4 + with: + name: migrate-api + path: . + + - name: Configure SSH + run: | + mkdir -p ~/.ssh + echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + ssh-keyscan -H "${{ secrets.DEPLOY_HOST }}" >> ~/.ssh/known_hosts + chmod +x ./migrate-api + + # Expand-then-contract: migrate while the previous release still serves. - name: Apply migrations - run: ./migrate-api --connection "${{ secrets.PG_CONNECTION_DDL }}" + env: + PG_CONNECTION_DDL: ${{ secrets.PG_CONNECTION_DDL }} + MIGRATE_DOCKER_NETWORK: ${{ vars.MIGRATE_DOCKER_NETWORK }} + run: | + REMOTE="${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" + NETWORK="${MIGRATE_DOCKER_NETWORK:-shared-services}" + scp -i ~/.ssh/id_ed25519 ./migrate-api \ + "$REMOTE:/tmp/vigilcare-migrate-api" + umask 077 + printf '%s' "$PG_CONNECTION_DDL" > /tmp/vigilcare-pg-ddl + scp -i ~/.ssh/id_ed25519 /tmp/vigilcare-pg-ddl \ + "$REMOTE:/tmp/vigilcare-pg-ddl" + rm -f /tmp/vigilcare-pg-ddl + ssh -i ~/.ssh/id_ed25519 "$REMOTE" \ + NETWORK="$NETWORK" \ + bash -euo pipefail <<'EOF' + chmod +x /tmp/vigilcare-migrate-api + chmod 600 /tmp/vigilcare-pg-ddl + + PG_CONNECTION_DDL="$(tr -d '\r\n' < /tmp/vigilcare-pg-ddl)" + PG_CONNECTION_DDL="${PG_CONNECTION_DDL#\"}" + PG_CONNECTION_DDL="${PG_CONNECTION_DDL%\"}" + PG_CONNECTION_DDL="${PG_CONNECTION_DDL#\'}" + PG_CONNECTION_DDL="${PG_CONNECTION_DDL%\'}" + + if [[ "$PG_CONNECTION_DDL" == postgresql://* || "$PG_CONNECTION_DDL" == postgres://* ]]; then + echo "PG_CONNECTION_DDL must be Npgsql key=value form, not a URI." >&2 + exit 1 + fi + if [[ "$PG_CONNECTION_DDL" != Host=* && "$PG_CONNECTION_DDL" != host=* ]]; then + PG_CONNECTION_DDL="Host=${PG_CONNECTION_DDL}" + fi + printf '%s' "$PG_CONNECTION_DDL" > /tmp/vigilcare-pg-ddl + + docker run --rm \ + --network "${NETWORK}" \ + -v /tmp/vigilcare-migrate-api:/migrate-api:ro \ + -v /tmp/vigilcare-pg-ddl:/run/pg-ddl:ro \ + --entrypoint sh \ + mcr.microsoft.com/dotnet/runtime-deps:8.0 \ + -c 'exec /migrate-api --connection "$(tr -d "\r\n" < /run/pg-ddl)"' + rm -f /tmp/vigilcare-migrate-api /tmp/vigilcare-pg-ddl + EOF deploy: needs: [build-and-push, migrate] @@ -122,15 +175,44 @@ jobs: - name: Deploy env: IMAGE_TAG: ${{ needs.build-and-push.outputs.tag }} + REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }} + REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} run: | + if [ -n "${{ vars.REGISTRY }}" ]; then + REGISTRY="${{ vars.REGISTRY }}" + else + REGISTRY="git.vectur45.com/Trent/vigilcare-clinical" + fi + REGISTRY_HOST="${REGISTRY%%/*}" ssh -i ~/.ssh/id_ed25519 \ "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" \ - IMAGE_TAG="$IMAGE_TAG" bash -euo pipefail <<'EOF' + IMAGE_TAG="$IMAGE_TAG" \ + REGISTRY_HOST="$REGISTRY_HOST" \ + REGISTRY_USERNAME="$REGISTRY_USERNAME" \ + REGISTRY_TOKEN="$REGISTRY_TOKEN" \ + bash -euo pipefail <<'EOF' cd /opt/vigilcare - # Record the currently deployed tag so a rollback has a target. - grep '^IMAGE_TAG=' .env > .env.previous || true + echo "${REGISTRY_TOKEN}" | docker login "${REGISTRY_HOST}" \ + -u "${REGISTRY_USERNAME}" --password-stdin + env_val() { sed -n "s/^${1}=//p" .env | tail -n1 | tr -d '\r'; } + strip_quotes() { + local v="$1" + v="${v#\"}"; v="${v%\"}"; v="${v#\'}"; v="${v%\'}" + printf '%s' "$v" + } + PG_CONNECTION="$(strip_quotes "$(env_val PG_CONNECTION)")" + if [[ -z "$PG_CONNECTION" ]]; then + echo "PG_CONNECTION is empty in /opt/vigilcare/.env" >&2 + exit 1 + fi + if [[ "$PG_CONNECTION" != Host=* && "$PG_CONNECTION" != host=* ]]; then + echo "PG_CONNECTION must start with Host= (Npgsql key=value form)." >&2 + exit 1 + fi + + grep '^IMAGE_TAG=' .env > .env.previous || true if grep -q '^IMAGE_TAG=' .env; then sed -i "s|^IMAGE_TAG=.*|IMAGE_TAG=${IMAGE_TAG}|" .env else @@ -140,6 +222,7 @@ jobs: docker compose -f docker-compose.prod.yml --env-file .env pull docker compose -f docker-compose.prod.yml --env-file .env up -d --remove-orphans docker image prune -f + docker compose -f docker-compose.prod.yml --env-file .env ps -a EOF - name: Smoke test @@ -147,24 +230,31 @@ jobs: ssh -i ~/.ssh/id_ed25519 \ "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" bash -euo pipefail <<'EOF' cd /opt/vigilcare - # Do not `source` .env — compose env files are not bash (semicolons, - # spaces in "SSL Mode=...", CRLF). Read only the host ports we need. env_val() { sed -n "s/^${1}=//p" .env | tail -n1 | tr -d '\r'; } API_PORT="$(env_val API_PORT)"; API_PORT="${API_PORT:-5270}" GATEWAY_PORT="$(env_val GATEWAY_PORT)"; GATEWAY_PORT="${GATEWAY_PORT:-5081}" DASHBOARD_PORT="$(env_val DASHBOARD_PORT)"; DASHBOARD_PORT="${DASHBOARD_PORT:-8080}" for i in $(seq 1 30); do - if curl -fsS "http://localhost:${API_PORT}/health/ready" >/dev/null; then - echo "Ready check passed." + set +e + http_code=$(curl -s -o /dev/null -w '%{http_code}' --connect-timeout 2 \ + "http://localhost:${API_PORT}/health/ready" 2>/dev/null) + curl_exit=$? + set -e + if [ "$curl_exit" -ne 0 ] || [ -z "$http_code" ]; then + http_code="000" + fi + if [ "$http_code" = "200" ]; then + echo "Ready check passed (attempt ${i})." curl -fsS "http://localhost:${GATEWAY_PORT}/health/live" >/dev/null && echo "Gateway live." curl -fsS "http://localhost:${DASHBOARD_PORT}/" >/dev/null && echo "Dashboard serving." exit 0 fi + echo "Attempt ${i}/30: HTTP ${http_code}" sleep 5 done - echo "Ready check never passed — dumping API logs:" - docker compose -f docker-compose.prod.yml --env-file .env logs --tail 100 api + echo "Ready check never passed" + docker compose -f docker-compose.prod.yml --env-file .env logs --tail 150 api || true exit 1 EOF @@ -174,8 +264,8 @@ jobs: ssh -i ~/.ssh/id_ed25519 \ "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" bash -euo pipefail <<'EOF' cd /opt/vigilcare - # Restores the previous image tag only. Schema changes are NOT - # reverted — this is why migrations must be backwards-compatible. + # Restores the previous image tag only. Schema is NOT reverted — + # migrations must be backwards-compatible (expand-then-contract). if [ -f .env.previous ]; then PREV=$(cut -d= -f2 .env.previous) sed -i "s|^IMAGE_TAG=.*|IMAGE_TAG=${PREV}|" .env diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index f36ecac..1e351f3 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -1,93 +1,100 @@ -# Phase 36 Step 11 — build and test on every push/PR. -# Fixtures read ConnectionStrings__* / Redis__* / RabbitMq__* / Kafka__* from the -# environment (see ApiFixture / GatewayApiFixture). Dependencies come from -# docker-compose.yml so Kafka, ES, and MinIO match local integration tests — -# ApiFixture starts hosted services that require those brokers. +# Backend stays local (onboarding §6 escape hatch): fixtures need RabbitMQ, +# Kafka, Elasticsearch, MinIO, and a second logical DB — beyond stock +# dotnet8-ci (Postgres + Redis services only). Frontend uses shared node-ci. name: CI on: push: branches: [master] + paths: + - "VigilCareClinicalAPI/**" + - "VigilCare.WardGateway/**" + - "VigilCare.ClinicalContracts/**" + - "VigilCare.ClinicalContracts.Tests/**" + - "VigilCareClinicalAPI.Tests/**" + - "VigilCare.WardGateway.Tests/**" + - "VigilCare.Simulation.Core/**" + - "VigilCare.Simulator/**" + - "vigilcare-dashboard/**" + - "VigilCareClinical.sln" + - "scripts/ci-*.sh" + - ".gitea/workflows/**" pull_request: branches: [master] + paths: + - "VigilCareClinicalAPI/**" + - "VigilCare.WardGateway/**" + - "VigilCare.ClinicalContracts/**" + - "VigilCare.ClinicalContracts.Tests/**" + - "VigilCareClinicalAPI.Tests/**" + - "VigilCare.WardGateway.Tests/**" + - "VigilCare.Simulation.Core/**" + - "VigilCare.Simulator/**" + - "vigilcare-dashboard/**" + - "VigilCareClinical.sln" + - "scripts/ci-*.sh" + - ".gitea/workflows/**" + workflow_dispatch: jobs: - backend: + changes: runs-on: ubuntu-latest - env: - # Kafka advertises this host on its EXTERNAL listener. The test process runs in - # a job container, so "localhost" (the compose default, correct for a dev box) - # would point the client at itself after bootstrap. - KAFKA_EXTERNAL_HOST: host.docker.internal + outputs: + backend: ${{ steps.filter.outputs.backend }} + frontend: ${{ steps.filter.outputs.frontend }} steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - - name: Start dependency stack + - id: filter + name: Detect stack changes run: | - docker compose up -d postgres redis rabbitmq kafka elasticsearch minio - docker compose --profile ward-gateway up -d ward-gateway-db ward-gateway-redis ward-gateway-rabbitmq - - - name: Wait for Postgres (API + gateway) - run: | - for i in $(seq 1 60); do - docker compose exec -T postgres pg_isready -U postgres && break - sleep 2 - done - docker compose exec -T postgres pg_isready -U postgres - docker compose exec -T ward-gateway-db pg_isready -U postgres - - - name: Wait for Kafka - run: | - for i in $(seq 1 90); do - # Probe the INTERNAL listener: the EXTERNAL one advertises - # host.docker.internal, which does not resolve inside the broker container. - if docker compose exec -T kafka \ - /opt/kafka/bin/kafka-broker-api-versions.sh \ - --bootstrap-server kafka:29092 >/dev/null 2>&1; then - echo "Kafka is ready" - exit 0 + set -euo pipefail + backend=false + frontend=false + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then + backend=true + frontend=true + else + if [ "${{ github.event_name }}" = "pull_request" ]; then + base="${{ github.event.pull_request.base.sha }}" + else + base="${{ github.event.before }}" fi - echo "waiting for Kafka ($i)..." - sleep 2 - done - echo "Kafka failed to become ready" >&2 - docker compose logs --tail=100 kafka - exit 1 + if [ -z "${base:-}" ] || [ "$base" = "0000000000000000000000000000000000000000" ]; then + backend=true + frontend=true + else + files="$(git diff --name-only "$base" "${{ github.sha }}")" + printf '%s\n' "$files" + if printf '%s\n' "$files" | grep -qE '^(VigilCareClinicalAPI/|VigilCare\.WardGateway/|VigilCare\.ClinicalContracts|VigilCareClinicalAPI\.Tests/|VigilCare\.WardGateway\.Tests/|VigilCare\.Simulation\.Core/|VigilCare\.Simulator/|VigilCareClinical\.sln|scripts/ci-|\.gitea/workflows/)'; then + backend=true + fi + if printf '%s\n' "$files" | grep -qE '^(vigilcare-dashboard/|\.gitea/workflows/)'; then + frontend=true + fi + fi + fi + echo "backend=${backend}" >> "$GITHUB_OUTPUT" + echo "frontend=${frontend}" >> "$GITHUB_OUTPUT" - - name: Wait for Redis / RabbitMQ / Elasticsearch - # --user rabbitmq is required: docker exec defaults to root, and Erlang tooling - # creates $HOME/.erlang.cookie mode 400 when it is missing. Probing a broker - # that is still booting would leave a root-owned cookie the server itself - # cannot read, crashing it with "reading .erlang.cookie: eacces". - run: | - for i in $(seq 1 60); do - docker compose exec -T redis redis-cli ping 2>/dev/null | grep -q PONG \ - && docker compose exec -T ward-gateway-redis redis-cli ping 2>/dev/null | grep -q PONG \ - && docker compose exec -T --user rabbitmq rabbitmq rabbitmq-diagnostics -q ping \ - && docker compose exec -T --user rabbitmq ward-gateway-rabbitmq rabbitmq-diagnostics -q ping \ - && docker compose exec -T elasticsearch curl -sf http://localhost:9200/_cluster/health >/dev/null \ - && echo "Redis, RabbitMQ, and Elasticsearch are ready" && exit 0 - echo "waiting for brokers ($i)..." - sleep 2 - done - echo "Brokers failed to become ready" >&2 - docker compose ps - docker compose logs --tail=50 redis rabbitmq elasticsearch ward-gateway-redis ward-gateway-rabbitmq - exit 1 + backend: + needs: changes + if: needs.changes.outputs.backend == 'true' + runs-on: ubuntu-latest + steps: + - uses: builtin:checkout - - name: Create test databases - run: | - docker compose exec -T postgres psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname='vigilcare_test'" \ - | grep -q 1 \ - || docker compose exec -T postgres psql -U postgres -c "CREATE DATABASE vigilcare_test" - docker compose exec -T ward-gateway-db psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname='vigilcare_ward_test'" \ - | grep -q 1 \ - || docker compose exec -T ward-gateway-db psql -U postgres -c "CREATE DATABASE vigilcare_ward_test" + - name: Start dependency sidecars + run: bash scripts/ci-start-sidecars.sh - name: Setup .NET 8 uses: actions/setup-dotnet@v4 with: dotnet-version: "8.0.x" + cache: true + cache-dependency-path: "**/*.csproj" - name: Restore run: dotnet restore VigilCareClinical.sln @@ -95,55 +102,84 @@ jobs: - name: Build run: dotnet build VigilCareClinical.sln -c Release --no-restore - - name: Test + - name: Test (isolated — no service database) env: - # act_runner runs in its own container; Compose publishes ports on the VM. - # host.docker.internal reaches those published ports (requires extra_hosts - # host-gateway on the runner). RabbitMQ guest remote access is enabled in - # infra/rabbitmq/rabbitmq.conf for this topology. - ConnectionStrings__DefaultConnection: "Host=host.docker.internal;Port=5436;Database=vigilcare_test;Username=postgres;Password=password" - ConnectionStrings__GatewayDb: "Host=host.docker.internal;Port=5437;Database=vigilcare_ward_test;Username=postgres;Password=password" - Redis__ConnectionString: "host.docker.internal:6382,defaultDatabase=1,allowAdmin=true" - Gateway__Redis__ConnectionString: "host.docker.internal:6383,defaultDatabase=2,allowAdmin=true" - RabbitMq__Host: "host.docker.internal" - RabbitMq__Port: "5674" - Gateway__RabbitMq__Host: "host.docker.internal" - Gateway__RabbitMq__Port: "5675" - Kafka__BootstrapServers: "host.docker.internal:9092" + ConnectionStrings__DefaultConnection: "" + ConnectionStrings__GatewayDb: "" + Redis__ConnectionString: "" + Gateway__Redis__ConnectionString: "" + RabbitMq__Host: "" + DOTNET_PROCESSOR_COUNT: "1" + DOTNET_gcServer: "0" + DOTNET_GCHeapCount: "1" + GITEA_TOKEN: "" + ASPNETCORE_ENVIRONMENT: Testing + run: | + set -euo pipefail + dotnet test VigilCare.ClinicalContracts.Tests/VigilCare.ClinicalContracts.Tests.csproj \ + -c Release --no-build \ + --blame-hang-timeout 2m \ + --logger "trx;LogFileName=ClinicalContracts.Tests.trx" \ + --results-directory ./TestResults \ + -- xUnit.MaxParallelThreads=1 + + - name: Test (against sidecars) + env: + ConnectionStrings__DefaultConnection: "Host=ci-pg;Port=5432;Database=vigilcare_test;Username=postgres;Password=postgres;SSL Mode=Disable" + ConnectionStrings__GatewayDb: "Host=ci-pg;Port=5432;Database=vigilcare_ward_test;Username=postgres;Password=postgres;SSL Mode=Disable" + Redis__ConnectionString: "ci-redis:6379,defaultDatabase=1,allowAdmin=true" + Gateway__Redis__ConnectionString: "ci-redis:6379,defaultDatabase=2,allowAdmin=true" + RabbitMq__Host: "ci-rmq" + RabbitMq__Port: "5672" + RabbitMq__Username: "ciuser" + RabbitMq__Password: "cipass" + Gateway__RabbitMq__Host: "ci-rmq" + Gateway__RabbitMq__Port: "5672" + Kafka__BootstrapServers: "ci-kafka:9092" Kafka__ReplicationFactor: "1" Kafka__SecurityProtocol: "Plaintext" - Elasticsearch__Uri: "http://host.docker.internal:9200" - Minio__Endpoint: "host.docker.internal:9005" + Elasticsearch__Uri: "http://ci-es:9200" + Minio__Endpoint: "ci-minio:9000" Minio__UseSSL: "false" - ASPNETCORE_ENVIRONMENT: "Testing" + ASPNETCORE_ENVIRONMENT: Testing + DOTNET_PROCESSOR_COUNT: "1" + DOTNET_gcServer: "0" + DOTNET_GCHeapCount: "1" + GITEA_TOKEN: "" run: | - dotnet test VigilCareClinical.sln -c Release --no-build \ - --logger "trx;LogFileName=test-results.trx" \ - --results-directory ./TestResults + set -euo pipefail + for proj in \ + VigilCareClinicalAPI.Tests/VigilCareClinicalAPI.Tests.csproj \ + VigilCare.WardGateway.Tests/VigilCare.WardGateway.Tests.csproj + do + name="$(basename "$proj" .csproj)" + echo "::group::$name" + dotnet test "$proj" -c Release --no-build \ + --blame-hang-timeout 2m \ + --logger "trx;LogFileName=${name}.trx" \ + --results-directory ./TestResults \ + -- xUnit.MaxParallelThreads=1 + echo "::endgroup::" + done - name: Publish test results if: always() - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: test-results path: ./TestResults - - name: Tear down stack + - name: Tear down sidecars if: always() - run: docker compose --profile ward-gateway down -v + run: bash scripts/ci-stop-sidecars.sh frontend: - runs-on: ubuntu-latest - container: - image: node:22-alpine - steps: - - uses: actions/checkout@v4 - - name: Install - working-directory: vigilcare-dashboard - run: npm ci - - name: Test - working-directory: vigilcare-dashboard - run: npm run test - - name: Build - working-directory: vigilcare-dashboard - run: npm run build + needs: changes + if: needs.changes.outputs.frontend == 'true' + uses: infra/ci-workflows/.gitea/workflows/node-ci.yml@v1 + with: + working_directory: vigilcare-dashboard + node_version: "22" + lint_command: "true" + extra_check_command: npm run test + build_command: npm run build diff --git a/VigilCare.WardGateway/Dockerfile b/VigilCare.WardGateway/Dockerfile index c8fcfef..4933045 100644 --- a/VigilCare.WardGateway/Dockerfile +++ b/VigilCare.WardGateway/Dockerfile @@ -17,7 +17,7 @@ RUN sed -i \ -e 's/"Gateway": "dev-[^"]*"/"Gateway": ""/' \ /app/publish/appsettings.json -FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final WORKDIR /app RUN apt-get update \ diff --git a/VigilCareClinicalAPI/Dockerfile b/VigilCareClinicalAPI/Dockerfile index 5e36026..beb28bf 100644 --- a/VigilCareClinicalAPI/Dockerfile +++ b/VigilCareClinicalAPI/Dockerfile @@ -1,6 +1,6 @@ # Multi-stage API image + EF migration bundle. -# docker build -f VigilCareClinicalAPI/Dockerfile . → runtime (default) -# docker build -f VigilCareClinicalAPI/Dockerfile --target migrate . → /out/migrate-api +# docker build -f VigilCareClinicalAPI/Dockerfile . → final (default) +# docker build -f VigilCareClinicalAPI/Dockerfile --target migrate-bundle . → scratch + /out/migrate-api FROM mcr.microsoft.com/dotnet/sdk:8.0 AS src WORKDIR /src @@ -34,9 +34,7 @@ RUN sed -i \ -e 's/"RunnerPassword": "[^"]*"/"RunnerPassword": ""/' \ /app/publish/appsettings.json -# Self-contained EF bundle for CD (docker build --target migrate). -# Prefer docker build over docker run -v on act_runner — bind mounts resolve -# on the Docker host, not the job workspace. +# Self-contained EF bundle for CD bake target migrate. FROM src AS migrate RUN dotnet tool install --global dotnet-ef --version 8.0.4 \ && export PATH="$PATH:/root/.dotnet/tools" \ @@ -49,7 +47,12 @@ RUN dotnet tool install --global dotnet-ef --version 8.0.4 \ --output /out/migrate-api \ --force -FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime +# Scratch extract stage — CMD required for bare `docker create` after bake. +FROM scratch AS migrate-bundle +COPY --from=migrate /out/migrate-api /out/migrate-api +CMD ["/out/migrate-api"] + +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final WORKDIR /app # curl is required by the container healthcheck; the aspnet image does not ship it. diff --git a/docker-bake.hcl b/docker-bake.hcl new file mode 100644 index 0000000..d9ca958 --- /dev/null +++ b/docker-bake.hcl @@ -0,0 +1,58 @@ +# Bake from repo root. CD caps the builder (scripts/cap-buildx-builder.sh) +# and bakes one target at a time so Gitea on the shared host stays up. +# +# API / gateway contexts are the repo root (sibling ProjectReferences). +# Dashboard context is vigilcare-dashboard/. + +variable "REGISTRY" {} +variable "TAG" {} +variable "VITE_API_URL" { + default = "" +} +variable "VITE_SHOW_OPS" { + default = "false" +} + +group "default" { + targets = ["api", "gateway", "dashboard", "migrate"] +} + +target "api" { + context = "cwd://" + dockerfile = "VigilCareClinicalAPI/Dockerfile" + target = "final" + tags = ["${REGISTRY}/clinical-api:${TAG}", "${REGISTRY}/clinical-api:latest"] + cache-from = ["type=registry,ref=${REGISTRY}/clinical-api:buildcache"] + cache-to = ["type=registry,ref=${REGISTRY}/clinical-api:buildcache,mode=max"] +} + +target "gateway" { + context = "cwd://" + dockerfile = "VigilCare.WardGateway/Dockerfile" + target = "final" + tags = ["${REGISTRY}/ward-gateway:${TAG}", "${REGISTRY}/ward-gateway:latest"] + cache-from = ["type=registry,ref=${REGISTRY}/ward-gateway:buildcache"] + cache-to = ["type=registry,ref=${REGISTRY}/ward-gateway:buildcache,mode=max"] +} + +target "dashboard" { + context = "cwd://vigilcare-dashboard" + dockerfile = "Dockerfile" + target = "final" + args = { + VITE_API_URL = VITE_API_URL + VITE_SHOW_OPS = VITE_SHOW_OPS + } + tags = ["${REGISTRY}/dashboard:${TAG}", "${REGISTRY}/dashboard:latest"] + cache-from = ["type=registry,ref=${REGISTRY}/dashboard:buildcache"] + cache-to = ["type=registry,ref=${REGISTRY}/dashboard:buildcache,mode=max"] +} + +target "migrate" { + context = "cwd://" + dockerfile = "VigilCareClinicalAPI/Dockerfile" + target = "migrate-bundle" + tags = ["vigilcare-migrate-bundle:local"] + cache-from = ["type=registry,ref=${REGISTRY}/clinical-api:buildcache"] + # no cache-to — api already exports this Dockerfile at mode=max +} diff --git a/scripts/cap-buildx-builder.sh b/scripts/cap-buildx-builder.sh new file mode 100755 index 0000000..4ef5af7 --- /dev/null +++ b/scripts/cap-buildx-builder.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# Cap a named docker-container buildx builder so it cannot take both vCPUs +# or swap-thrash Gitea on the e2-standard-2 (2 vCPU / 8 GB). +# +# ./CICD/scripts/cap-buildx-builder.sh growth-os-builder +# +# BUILDKIT_STEP_MAX_PARALLELISM=1 only serializes steps. A single +# `dotnet publish` / vite / gzip still uses every core unless the +# buildkitd container itself has a cgroup quota. This script: +# - reuses the builder when the driver is already docker-container +# (wiping it would drop the local BuildKit cache) +# - creates it with parallelism=1 and GOMAXPROCS=1 when missing +# - always `docker update --cpus=1 --memory=3g --memory-swap=3g` +# +# Override with BUILDX_HOST_CPUS / BUILDX_HOST_MEMORY. +# The CD job must still bake one target at a time. +set -euo pipefail + +builder="${1:?builder name}" +cpus="${BUILDX_HOST_CPUS:-1.0}" +memory="${BUILDX_HOST_MEMORY:-3g}" + +if docker buildx inspect "$builder" >/dev/null 2>&1; then + driver="$(docker buildx inspect "$builder" | awk '/^Driver:/{print $2; exit}')" + if [ "$driver" = "docker-container" ]; then + docker buildx use "$builder" + else + docker buildx rm -f "$builder" + docker buildx create --name "$builder" --driver docker-container \ + --driver-opt env.BUILDKIT_STEP_MAX_PARALLELISM=1 \ + --driver-opt env.GOMAXPROCS=1 \ + --use + fi +else + docker buildx create --name "$builder" --driver docker-container \ + --driver-opt env.BUILDKIT_STEP_MAX_PARALLELISM=1 \ + --driver-opt env.GOMAXPROCS=1 \ + --use +fi + +docker buildx inspect --bootstrap + +ids="$(docker ps --filter "name=buildx_buildkit_${builder}" --format '{{.ID}}')" +cid="$(printf '%s\n' "$ids" | awk 'NR==1')" +if [ -z "$cid" ]; then + echo "cap-buildx-builder: no running container matching buildx_buildkit_${builder}" >&2 + docker ps --filter "name=buildx_buildkit" >&2 || true + exit 1 +fi + +docker update \ + --cpus="$cpus" \ + --memory="$memory" \ + --memory-swap="$memory" \ + --cpu-shares=512 \ + "$cid" +echo "cap-buildx-builder: ${builder} -> ${cid} cpus=${cpus} memory=${memory} (swap capped)" diff --git a/scripts/ci-start-sidecars.sh b/scripts/ci-start-sidecars.sh new file mode 100755 index 0000000..73e69bc --- /dev/null +++ b/scripts/ci-start-sidecars.sh @@ -0,0 +1,234 @@ +#!/usr/bin/env bash +# Start VigilCare CI dependencies on the current Gitea Actions job network. +# No published ports — fixtures reach brokers by Docker DNS aliases (ci-pg, …). +# RabbitMQ cannot be a workflow services: entry on this runner; see docs/CICD. +# +# Requires: Docker socket, docker CLI, bash. +# Exports container names + fixture-oriented hosts into $GITHUB_ENV when set. +set -euo pipefail + +PG_IMAGE="${CI_PG_IMAGE:-postgres:16-alpine}" +RMQ_IMAGE="${CI_RMQ_IMAGE:-rabbitmq:3.13-management-alpine}" +REDIS_IMAGE="${CI_REDIS_IMAGE:-redis:7-alpine}" +KAFKA_IMAGE="${CI_KAFKA_IMAGE:-apache/kafka:3.7.0}" +ES_IMAGE="${CI_ES_IMAGE:-docker.elastic.co/elasticsearch/elasticsearch:8.13.0}" +MINIO_IMAGE="${CI_MINIO_IMAGE:-minio/minio:RELEASE.2024-07-04T14-25-45Z}" + +PG_USER="${CI_PG_USER:-postgres}" +PG_PASSWORD="${CI_PG_PASSWORD:-postgres}" +RMQ_USER="${CI_RMQ_USER:-ciuser}" +RMQ_PASS="${CI_RMQ_PASS:-cipass}" + +find_job_cid() { + local cid + cid="$(grep -oE '[0-9a-f]{64}' /proc/self/cgroup 2>/dev/null | head -1 || true)" + if [ -n "${cid}" ] && docker inspect "${cid}" >/dev/null 2>&1; then + printf '%s\n' "${cid}" + return 0 + fi + if docker inspect "$(hostname)" >/dev/null 2>&1; then + hostname + return 0 + fi + cid="$(grep -oE 'containers/[0-9a-f]{64}' /proc/self/mountinfo 2>/dev/null | head -1 | cut -d/ -f2 || true)" + if [ -n "${cid}" ] && docker inspect "${cid}" >/dev/null 2>&1; then + printf '%s\n' "${cid}" + return 0 + fi + return 1 +} + +CID="$(find_job_cid)" || { + echo "ci-start-sidecars: cannot resolve job container id (is /var/run/docker.sock mounted?)" >&2 + exit 1 +} +NETWORK="$(docker inspect -f '{{range $k, $_ := .NetworkSettings.Networks}}{{println $k}}{{end}}' "${CID}" | awk 'NF { print; exit }')" +if [ -z "${NETWORK}" ]; then + echo "ci-start-sidecars: job container ${CID} has no Docker network" >&2 + exit 1 +fi +if [ "${NETWORK}" = "host" ]; then + echo "ci-start-sidecars: host network mode is unsupported for this project CI" >&2 + exit 1 +fi +echo "ci-start-sidecars: job=${CID} network=${NETWORK}" + +SUFFIX="${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-1}" +PG_NAME="ci-pg-${SUFFIX}" +RMQ_NAME="ci-rmq-${SUFFIX}" +REDIS_NAME="ci-redis-${SUFFIX}" +KAFKA_NAME="ci-kafka-${SUFFIX}" +ES_NAME="ci-es-${SUFFIX}" +MINIO_NAME="ci-minio-${SUFFIX}" + +if [ -n "${GITHUB_ENV:-}" ]; then + { + echo "CI_PG_NAME=${PG_NAME}" + echo "CI_RMQ_NAME=${RMQ_NAME}" + echo "CI_REDIS_NAME=${REDIS_NAME}" + echo "CI_KAFKA_NAME=${KAFKA_NAME}" + echo "CI_ES_NAME=${ES_NAME}" + echo "CI_MINIO_NAME=${MINIO_NAME}" + } >> "${GITHUB_ENV}" +fi + +docker rm -f "${PG_NAME}" "${RMQ_NAME}" "${REDIS_NAME}" "${KAFKA_NAME}" "${ES_NAME}" "${MINIO_NAME}" 2>/dev/null || true + +docker run -d --name "${PG_NAME}" \ + --network "${NETWORK}" --network-alias ci-pg \ + -e POSTGRES_USER="${PG_USER}" \ + -e POSTGRES_PASSWORD="${PG_PASSWORD}" \ + -e POSTGRES_DB=vigilcare_test \ + "${PG_IMAGE}" + +# Non-guest user: guest is loopback-only; do not bind-mount rabbitmq.conf — +# under act_runner bind mounts resolve on the Docker host, not the job workspace. +docker run -d --name "${RMQ_NAME}" \ + --network "${NETWORK}" --network-alias ci-rmq \ + --hostname ci-rmq \ + -e RABBITMQ_NODENAME="rabbit@ci-rmq" \ + -e RABBITMQ_DEFAULT_USER="${RMQ_USER}" \ + -e RABBITMQ_DEFAULT_PASS="${RMQ_PASS}" \ + "${RMQ_IMAGE}" + +docker run -d --name "${REDIS_NAME}" \ + --network "${NETWORK}" --network-alias ci-redis \ + "${REDIS_IMAGE}" + +# Single listener advertised as Docker DNS name so the job container can reconnect. +docker run -d --name "${KAFKA_NAME}" \ + --network "${NETWORK}" --network-alias ci-kafka \ + -e KAFKA_NODE_ID=1 \ + -e KAFKA_PROCESS_ROLES=broker,controller \ + -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093 \ + -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://ci-kafka:9092 \ + -e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXT \ + -e KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT \ + -e KAFKA_CONTROLLER_QUORUM_VOTERS=1@ci-kafka:9093 \ + -e KAFKA_CONTROLLER_LISTENER_NAMES=CONTROLLER \ + -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \ + -e KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=1 \ + -e KAFKA_TRANSACTION_STATE_LOG_MIN_ISR=1 \ + -e KAFKA_AUTO_CREATE_TOPICS_ENABLE=false \ + -e CLUSTER_ID=MkU3OEVBNTcwNTJENDM2Qk \ + "${KAFKA_IMAGE}" + +docker run -d --name "${ES_NAME}" \ + --network "${NETWORK}" --network-alias ci-es \ + -e discovery.type=single-node \ + -e xpack.security.enabled=false \ + -e "ES_JAVA_OPTS=-Xms256m -Xmx256m" \ + -e cluster.name=vigilcare-ci \ + "${ES_IMAGE}" + +docker run -d --name "${MINIO_NAME}" \ + --network "${NETWORK}" --network-alias ci-minio \ + -e MINIO_ROOT_USER=minioadmin \ + -e MINIO_ROOT_PASSWORD=minioadmin \ + "${MINIO_IMAGE}" \ + server /data --console-address ":9001" + +echo "Waiting for Postgres…" +for i in $(seq 1 60); do + if docker exec "${PG_NAME}" pg_isready -U "${PG_USER}" -d vigilcare_test >/dev/null 2>&1; then + echo "Postgres ready" + break + fi + if [ "${i}" -eq 60 ]; then + docker logs "${PG_NAME}" || true + exit 1 + fi + sleep 1 +done + +# Second DB for ward-gateway tests on the same instance. +docker exec "${PG_NAME}" psql -U "${PG_USER}" -tc "SELECT 1 FROM pg_database WHERE datname='vigilcare_ward_test'" \ + | grep -q 1 \ + || docker exec "${PG_NAME}" psql -U "${PG_USER}" -c "CREATE DATABASE vigilcare_ward_test" + +echo "Waiting for RabbitMQ…" +for i in $(seq 1 90); do + if docker exec --user rabbitmq "${RMQ_NAME}" rabbitmq-diagnostics -q ping >/dev/null 2>&1 \ + && docker exec --user rabbitmq "${RMQ_NAME}" rabbitmq-diagnostics -q check_port_connectivity >/dev/null 2>&1; then + echo "RabbitMQ ready" + break + fi + if [ "${i}" -eq 90 ]; then + docker logs "${RMQ_NAME}" || true + exit 1 + fi + sleep 1 +done + +echo "Waiting for Redis…" +for i in $(seq 1 30); do + if docker exec "${REDIS_NAME}" redis-cli ping 2>/dev/null | grep -q PONG; then + echo "Redis ready" + break + fi + if [ "${i}" -eq 30 ]; then + docker logs "${REDIS_NAME}" || true + exit 1 + fi + sleep 1 +done + +echo "Waiting for Kafka…" +for i in $(seq 1 90); do + if docker exec "${KAFKA_NAME}" \ + /opt/kafka/bin/kafka-broker-api-versions.sh \ + --bootstrap-server ci-kafka:9092 >/dev/null 2>&1; then + echo "Kafka ready" + break + fi + if [ "${i}" -eq 90 ]; then + docker logs "${KAFKA_NAME}" || true + exit 1 + fi + sleep 2 +done + +echo "Waiting for Elasticsearch…" +for i in $(seq 1 90); do + if docker exec "${ES_NAME}" curl -sf http://localhost:9200/_cluster/health >/dev/null 2>&1; then + echo "Elasticsearch ready" + break + fi + if [ "${i}" -eq 90 ]; then + docker logs "${ES_NAME}" || true + exit 1 + fi + sleep 2 +done + +echo "Waiting for MinIO…" +# Official minio image has no curl; settle briefly and confirm the process is alive. +for i in $(seq 1 30); do + if docker exec "${MINIO_NAME}" true >/dev/null 2>&1; then + sleep 3 + echo "MinIO ready" + break + fi + if [ "${i}" -eq 30 ]; then + docker logs "${MINIO_NAME}" || true + exit 1 + fi + sleep 1 +done + +if [ -n "${GITHUB_ENV:-}" ]; then + { + echo "CI_PG_HOST=ci-pg" + echo "CI_RMQ_HOST=ci-rmq" + echo "CI_REDIS_HOST=ci-redis" + echo "CI_KAFKA_HOST=ci-kafka" + echo "CI_ES_HOST=ci-es" + echo "CI_MINIO_HOST=ci-minio" + echo "CI_PG_USER=${PG_USER}" + echo "CI_PG_PASSWORD=${PG_PASSWORD}" + echo "CI_RMQ_USER=${RMQ_USER}" + echo "CI_RMQ_PASS=${RMQ_PASS}" + } >> "${GITHUB_ENV}" +fi + +echo "ci-start-sidecars: ready (ci-pg / ci-rmq / ci-redis / ci-kafka / ci-es / ci-minio)" diff --git a/scripts/ci-stop-sidecars.sh b/scripts/ci-stop-sidecars.sh new file mode 100755 index 0000000..30d37f4 --- /dev/null +++ b/scripts/ci-stop-sidecars.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Tear down containers started by scripts/ci-start-sidecars.sh. +set -euo pipefail + +names=( + "${CI_PG_NAME:-}" + "${CI_RMQ_NAME:-}" + "${CI_REDIS_NAME:-}" + "${CI_KAFKA_NAME:-}" + "${CI_ES_NAME:-}" + "${CI_MINIO_NAME:-}" +) + +to_rm=() +for n in "${names[@]}"; do + [ -n "$n" ] && to_rm+=( "$n" ) +done + +if [ "${#to_rm[@]}" -eq 0 ]; then + echo "ci-stop-sidecars: no CI_*_NAME vars set — nothing to remove" + exit 0 +fi + +docker rm -f "${to_rm[@]}" 2>/dev/null || true +echo "ci-stop-sidecars: removed ${to_rm[*]}" diff --git a/vigilcare-dashboard/Dockerfile b/vigilcare-dashboard/Dockerfile index d84b025..5a9ec22 100644 --- a/vigilcare-dashboard/Dockerfile +++ b/vigilcare-dashboard/Dockerfile @@ -12,7 +12,7 @@ ENV VITE_API_URL=$VITE_API_URL ENV VITE_SHOW_OPS=$VITE_SHOW_OPS RUN npm run build -FROM nginx:1.27-alpine AS runtime +FROM nginx:1.27-alpine AS final COPY --from=build /src/dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80