Record CD service health when a deployment finishes

What does this MR do and why?

The CD environment detail page (the "Running Apps" tile and the deployed applications list) reads from the GraphQL fields applicationsCount, servicesCount and applications on CdEnvironment. These, along with the cdEnvironments(applicationId:) filter and deployedVersions, are all derived from the cd_service_environment_healths table, which holds one row per (service, environment).

Nothing writes that table yet. Applications will eventually report their own health, but this functionality doesn't exist yet.

As a stopgap, Cd::Deployment gains an after_transition any => [:healthy, :degraded] state machine hook that calls a new Cd::ServiceEnvironmentHealth.record_deployment!(deployment). This upserts the (service_id, environment_id) row with health mirroring the deployment state, observed_at set to the deployment's finished_at, and organization_id set explicitly since upsert bypasses the sharding-key callback. Last write wins, so real health reports will simply overwrite these rows once that feature ships. Failed and cancelled deployments write nothing.

Health-derived fields such as healthStatus, CdApplication.health, and the status filters will read healthy right after a deployment finishes. This is accepted for now because the frontend does not surface health status until applications report it themselves.

References

Resolves https://gitlab.com/gitlab-org/gitlab/-/work_items/627243

How to set up and validate locally

  1. Have an organization with a CD application, a service, and an environment, with the ai_native_deploy feature flag enabled.
  2. Create a rollout to the environment and let its deployment reach healthy (or, in a Rails console, find a deployment in the deploying state and call mark_healthy! on it).
  3. Open the environment detail page at /o/<organization>/-/deploy/environments/<id>. The Running Apps tile shows the application count and the deployed applications table lists the application with its services.
  4. Optional: to seed rows for deployments that already finished before this change, run in a Rails console:
    Cd::Deployment.where(state: %i[healthy degraded]).find_each { |d| Cd::ServiceEnvironmentHealth.record_deployment!(d) }

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Tiger Watson

Merge request reports

Loading
Loading