Limit the number of project and group variables
What does this MR do and why?
Adds a count limit for each plan to project and group CI/CD variables, behind the ci_project_variable_limits and ci_group_variable_limits flags from !267953 (merged).
The existing project_ci_variables (8,000) and group_ci_variables (30,000) limits have no switch. Lowering them on GitLab.com would apply on deploy, and rolling back would need another migration. This MR leaves them as they are and adds two plan_limits columns:
ci_project_variables_countci_group_variables_count
Both default to 0, which enforces nothing. Administrators set them in Admin > Settings > CI/CD > CI/CD limits or with the plan limits API.
Ci::CountLimitedVariable checks the new limit when a variable is created:
- It reads the flag only when the owner already holds the limit. The flag actor is the root namespace.
- With the flag off, the variable is saved and the check is only logged.
ci_variable_count_limit_overridelifts the check for a root namespace. A percentage rollout cannot leave one namespace out, so this is how owners already over the limit are exempted. The size override,ci_pipeline_variable_limits_override, does not lift it.- The
Limitablelimits apply either way.
Every check that finds the owner over the limit logs one line to application_json.log, with the same shape as the size check:
{"message":"CI variable count exceeds the plan limit","class_name":"Ci::GroupVariable","gl_root_namespace_id":1,"extra":{"owner_type":"Group","owner_id":2,"count":1,"limit":1,"override":false,"over_limit":true,"rejected":true}}A seed migration sets the GitLab.com values for both projects and groups. It covers every GitLab.com plan except default and early_adopter.
The seed runs only on GitLab.com and skips JiHu, so other instances keep 0.
The seed runs outside a transaction. On GitLab.com the plan limit helper also upserts the plans row, so each call writes two tables. Each upsert is idempotent, so a partial run can be repeated.
Rollout condition
The count check uses the same flags as the size check in !267953 (merged). If the size rollout is at 100% when this MR deploys, the seeded count limits apply at once. Before this MR merges, one of these must hold on GitLab.com:
ci_variable_count_limit_overrideis on for every root namespace over the project or group count limit. The exemption lists are on the rollout issues.- The flags are still off.
Known gap
Settings > CI/CD > Variables disables Add variable at the Limitable limit. Types::Ci::ProjectVariableConnectionType#limit, Types::Ci::GroupVariableConnectionType#limit and both settings controllers read project_ci_variables and group_ci_variables from the default plan. When the new limit applies, the button stays enabled and saving returns the limit error. Tracked in #644515.
Ci::ChangeVariablesService saves several variables in one request through nested attributes. Each new variable is checked against the rows already stored, so one request can add several variables past the limit. The settings page sends one variable per request. Limitable has the same gap.
References
- Project variables: https://gitlab.com/gitlab-org/gitlab/-/work_items/613420
- Group variables: https://gitlab.com/gitlab-org/gitlab/-/work_items/613419
- Epic: https://gitlab.com/groups/gitlab-org/-/work_items/23230
- Size check and flag definitions: !267953 (merged)
- Rollout issues: #639852 (projects), #639853 (groups)
Screenshots or screen recordings
Two number fields are added under Admin > Settings > CI/CD > CI/CD limits, after Maximum number of job variables in a single job. They match the fields around them. The help page at /help/instance_configuration gets two matching rows.
How to set up and validate locally
-
In the Rails console, set a limit of 1 and create one variable on a group:
group = Group.find(<group_id>) Plan.default.actual_limits.update!(ci_group_variables_count: 1) group.variables.create!(key: 'FIRST', value: 'x') -
POST /groups/:id/variableswith a second key returns400withThe number of CI/CD variables exceeds the limit of 1. -
log/application_json.loghas oneCI variable count exceeds the plan limitline with"rejected":true. -
Disable the flag. The same request now succeeds and logs
"rejected":false:Feature.disable(:ci_group_variable_limits) -
Enable the flag again and enable the override for the root namespace. The request succeeds and logs
"override":true:Feature.enable(:ci_group_variable_limits) Feature.enable(:ci_variable_count_limit_override, group.root_ancestor) -
Repeat with
ci_project_variables_count,ci_project_variable_limitsandPOST /projects/:id/variablesfor projects. -
To check the seed migration, run it again with GitLab.com simulated:
GITLAB_SIMULATE_SAAS=1 bin/rails db:migrate:redo:main VERSION=20261203113200Every plan except
defaultandearly_adopternow hasci_project_variables_countandci_group_variables_countset to the seeded value. WithoutGITLAB_SIMULATE_SAAS=1, the migration changes nothing.
Database
Both migrations touch plan_limits only, plus plans on GitLab.com. Output is from a local GDK database. The seed ran with GITLAB_SIMULATE_SAAS=1. Its output is trimmed to the free plan, with the quote lines removed and the seeded value shown as <limit>. The same statements repeat for the other 9 plans.
20261203113100 up and down
main: == 20261203113100 AddCiProjectAndGroupVariablesCountToPlanLimits: migrating ===
main: -- add_column(:plan_limits, :ci_project_variables_count, :integer, {default: 0, null: false})
main: -> 0.0277s
main: -- add_column(:plan_limits, :ci_group_variables_count, :integer, {default: 0, null: false})
main: -> 0.0026s
main: == 20261203113100 AddCiProjectAndGroupVariablesCountToPlanLimits: migrated (0.0439s)main: == 20261203113100 AddCiProjectAndGroupVariablesCountToPlanLimits: reverting ===
main: -- remove_column(:plan_limits, :ci_group_variables_count, :integer, {default: 0, null: false})
main: -> 0.0337s
main: -- remove_column(:plan_limits, :ci_project_variables_count, :integer, {default: 0, null: false})
main: -> 0.0019s
main: == 20261203113100 AddCiProjectAndGroupVariablesCountToPlanLimits: reverted (0.0478s)20261203113200 up and down
main: == 20261203113200 SeedCiProjectAndGroupVariablesCountLimits: migrating ========
main: -- execute("INSERT INTO plans (name, title, plan_name_uid, created_at, updated_at)\nVALUES ('free', 'Free', '2', NOW(), NOW())\nON CONFLICT DO NOTHING;\n")
main: -> 0.0016s
main: -- execute("INSERT INTO plan_limits (plan_id, plan_name_uid, \"ci_project_variables_count\")\nSELECT id, '2', '<limit>' FROM plans WHERE name = 'free' LIMIT 1\nON CONFLICT (plan_id) DO UPDATE SET \"ci_project_variables_count\" = EXCLUDED.\"ci_project_variables_count\", plan_name_uid = EXCLUDED.plan_name_uid;\n")
main: -> 0.0021s
main: -- execute("INSERT INTO plans (name, title, plan_name_uid, created_at, updated_at)\nVALUES ('free', 'Free', '2', NOW(), NOW())\nON CONFLICT DO NOTHING;\n")
main: -> 0.0004s
main: -- execute("INSERT INTO plan_limits (plan_id, plan_name_uid, \"ci_group_variables_count\")\nSELECT id, '2', '<limit>' FROM plans WHERE name = 'free' LIMIT 1\nON CONFLICT (plan_id) DO UPDATE SET \"ci_group_variables_count\" = EXCLUDED.\"ci_group_variables_count\", plan_name_uid = EXCLUDED.plan_name_uid;\n")
main: -> 0.0007s
...
main: == 20261203113200 SeedCiProjectAndGroupVariablesCountLimits: migrated (0.0423s)main: == 20261203113200 SeedCiProjectAndGroupVariablesCountLimits: reverting ========
main: -- execute("INSERT INTO plans (name, title, plan_name_uid, created_at, updated_at)\nVALUES ('free', 'Free', '2', NOW(), NOW())\nON CONFLICT DO NOTHING;\n")
main: -> 0.0050s
main: -- execute("INSERT INTO plan_limits (plan_id, plan_name_uid, \"ci_project_variables_count\")\nSELECT id, '2', '0' FROM plans WHERE name = 'free' LIMIT 1\nON CONFLICT (plan_id) DO UPDATE SET \"ci_project_variables_count\" = EXCLUDED.\"ci_project_variables_count\", plan_name_uid = EXCLUDED.plan_name_uid;\n")
main: -> 0.0021s
main: -- execute("INSERT INTO plans (name, title, plan_name_uid, created_at, updated_at)\nVALUES ('free', 'Free', '2', NOW(), NOW())\nON CONFLICT DO NOTHING;\n")
main: -> 0.0004s
main: -- execute("INSERT INTO plan_limits (plan_id, plan_name_uid, \"ci_group_variables_count\")\nSELECT id, '2', '0' FROM plans WHERE name = 'free' LIMIT 1\nON CONFLICT (plan_id) DO UPDATE SET \"ci_group_variables_count\" = EXCLUDED.\"ci_group_variables_count\", plan_name_uid = EXCLUDED.plan_name_uid;\n")
main: -> 0.0005s
...
main: == 20261203113200 SeedCiProjectAndGroupVariablesCountLimits: reverted (0.0409s)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.