Skip to content

Create deployments for ready-to-run jobs only

What does this MR do and why?

This change is part of the epic for Reduce main database load by optimizing `deploy... (&10169)

This ensures that deployment records are only created for deploy jobs that are ready to run. This means that deploy jobs that are still at the created state will not have deployment records upfront, helping to reduce the load on the main database.

For further details, please check the Expected behavior section related issue, and the illustration of the expected behavior in the Screenshots below.

Screenshots or screen recordings

GitLab CI configuration example

Given the following GitLab CI configuration with jobs in different stages:

  • [stage-0] test job
  • 4 deploy jobs
    • [stage-1] review runs automatically after test
    • [stage-2] staging is set as when: manual and allow_failure: false, meaning it must succeed before jobs in the next stages can run
    • [stage-3] canary is set as when: manual and by default allow_failure: true
    • [stage-4] production-a is set as when: manual (and by default allow_failure: true)
    • [stage-4] production-b is set as when: manual and needs: [canary] (and by default allow_failure: true)
=== EXPAND THIS SECTION FOR THE GITLAB CI CONFIG ===
stages: [stage-0, stage-1, stage-2, stage-3, stage-4]

test:
  stage: stage-0
  script: sleep 120

review:
  stage: stage-1
  environment:
    name: review
    action: start
  script: sleep 60

staging:
  stage: stage-2
  environment:
    name: staging
    action: start
  script: sleep 60
  when: manual
  allow_failure: false

canary:
  stage: stage-3
  environment:
    name: canary
    action: start
  script: exit 0
  when: manual

production-a:
  stage: stage-4
  environment:
    name: production-a
    action: start
  script: exit 0
  when: manual

production-b:
  stage: stage-4
  environment:
    name: production-b
    action: start
  script: exit 0
  when: manual
  needs: [canary]

Current behavior

This is the behavior before the change in this MR is applied / the Feature Flag is disabled

=== EXPAND THIS SECTION FOR THE EXAMPLE TABLE ===
Pipeline Deployments created Screenshot
test job is running Deployment records of all deploy jobs are already created FF_disabled
review job is running Deployment record for review deploy job is already created see above
staging job is played manually Deployment record for staging deploy job is already created see above
production-a job is played manually Deployment record for production-a deploy job is already created see above
production-b job is played manually Deployment record for production-a deploy job is already created see above

Expected behavior

This is the expected behavior after the change / the Feature Flag is enabled

=== EXPAND THIS SECTION FOR THE EXAMPLE TABLE ===
Pipeline Deployments created Screenshot
test job is running No deployment records created yet FF_enabled_1_test_running
test job done; review deploy job is running Deployment record created for review job FF_enabled_2_test_done_and_review_running
review job done; staging deploy job is played Deployment record created for staging job

Since staging is set as allow_failure: false, the subsequent deployment jobs are not yet "ready" because they need staging to succeed.
FF_enabled_3_review_done_and_staging_running
staging job done Deployment records for canary, production-a, and production-b are created

At this point, canary is ready to run since staging has succeeded.
Since canary is set by default as allow_failure: true, the subsequent production-a and production-b jobs are also considered ready to run.
FF_enabled_4_staging_done
canary job is running Deployment for canary is already created FF_enabled_6_canary_running
production-a and production-b jobs are played and about to be picked up Deployment records for production-a and production-b are already created FF_enabled_7_canary_done_and_production_pending

Note: since production-b is set as needs: [canary], its corresponding deployment should ideally not be created until canary succeeds. This will be addressed in a follow-up issue.

How to set up and validate locally

  1. Create a project or use an existing project for testing

  2. Enable the create_deployment_only_for_processable_jobs Feature Flag in the rails console

    Feature.enable(:create_deployment_only_for_processable_jobs)
  3. Use the example above for the configuration in your .gitlab-ci.yml

  4. Run the pipeline / observe the running pipeline and verify that it follows the Expected behavior in the example above

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #391291 (closed)

Edited by Pam Artiaga

Merge request reports