Skip to main content

Manage GitLab project housekeeping from the default branch. The component cleans up obsolete terminal pipelines, applies a default container registry cleanup policy, stops stale environments, and schedules deletion for old stopped review app environments.

The token in GITLAB_TOKEN requires project Owner access to update pipeline-retention settings and delete pipelines. By default, old pipelines are eligible for deletion after 7 days, the container registry cleanup policy is enabled with (?:[0-9]+\.[0-9]+\.[0-9]+|main|latest) kept, and stopped review app environments older than 30 days are scheduled for deletion.

Configure GITLAB_TOKEN with a group service account and grant it Owner access to the managed project.

Usage

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/housekeeping@stable

The job only runs on the default branch unless you override rules.

Guarded pipeline cleanup​

pipeline-retention-seconds is the minimum age for cleanup (default: seven days, or 604800 seconds). The job processes all eligible pipelines without a per-run deletion limit. Both creation and last-update timestamps must be older than the cutoff, so a recently retried old pipeline is retained.

The job retains all active/manual/unknown-status pipelines, its own pipeline, the latest pipeline and latest successful pipeline for every ref, and pipelines for the latest ref SHA. Tag and child pipelines are retained. Candidates are inventoried with pagination before deletion, then their state and latest ref are rechecked. API errors, incomplete age/SHA data, or a latest-pipeline response for a different ref fail the job before that candidate is deleted.

The component disables GitLab's age-only background cleanup before guarded cleanup. GitLab documents that the background policy can delete pipelines regardless of status, including the latest pipeline for a branch or tag. The job verifies that it is disabled; an insufficient token or unsupported response fails before pipeline deletion.

Only terminal success, failed, canceled, and skipped pipelines can be deleted. Pipeline deletion permanently removes associated logs and artifacts. Do not run concurrent manual retries or independent deletion jobs during cleanup: GitLab has no atomic compare-and-delete API, so state rechecks cannot lock a pipeline against an external change between the final read and delete.

Inputs​

NameDescriptionDefaultType
------------
nameThe name of the housekeeping job.projectstring
needsThe jobs that this job depends on.[]array
pipeline-retention-secondsMinimum age for guarded pipeline cleanup. Active and current pipelines are retained.604800number
registry-cleanup-cadenceCadence for the GitLab container registry cleanup policy.7dstring
registry-cleanup-keep-nMinimum number of container tags to keep.100number
registry-cleanup-name-regexContainer tag regex selected by the cleanup policy..*string
registry-cleanup-name-regex-keepContainer tag regex always kept by the cleanup policy.(?:[0-9]+\.[0-9]+\.[0-9]+|main|latest)string
registry-cleanup-older-thanDelete container tags older than this value.14dstring
rulesRules controlling when the housekeeping job runs.[{"if":"$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH","when":"on_success"},{"when":"never"}]array
stageThe stage where the housekeeping job runs..poststring
stale-environment-daysStop environments last modified before this many days. GitLab requires at least 7 days.30number
stopped-review-app-daysSchedule deletion for stopped review app environments older than this many days.30number
stopped-review-app-limitMaximum number of stopped review app environments to schedule for deletion in one run.100number
tagsThe runners that this job depends on.[]array
token-variableName of the CI/CD variable containing a GitLab token allowed to manage project settings and environments.GITLAB_TOKENstring