Skip to content

Use a minimal cost factor for GitLab forks

Fabio Pitino requested to merge add-custom-cost-factor-for-gitlab-forks into master

What does this MR do and why?

Related to #365447 (closed)

In this MR we identify as Community contribution to GitLab projects those merge request pipelines that run from a fork of any gitlab-com or gitlab-org projects. For these pipelines we would assign a minimal cost factor that yields 300,000 minutes of shared runners usage to spend on GitLab contributions.

The cost factor is calculated dynamically based on the amount of monthly CI minutes that the user has, in order to guarantee 300k minutes of usage to all contributors regardless of their plan.

contributor_cost_factor = monthly_minutes / 300,000

This change allows us to move forward with the plan to enforce CI minutes for all public projects while treating contributions to GitLab as a special case until we have a longer term plan.

How to set up and validate locally

  1. Enable the development FF
    Feature.enable(:ci_minimal_cost_factor_for_gitlab_contributors)
  2. Add a group and project to enable. I added gitlab-org/gitlab-cost-factor
  3. In rails console enable the ops feature flag for the group
    Feature.enable(:ci_minimal_cost_factor_for_gitlab_namespaces, Group.find_by_full_path('gitlab-org'))
  4. Navigate to the pipeline editor http://localhost:3000/gitlab-org/gitlab-cost-factor/-/ci/editor?branch_name=main and add a ci config:
       job1:
         script: sleep 10
         rules:
           - if: $CI_PIPELINE_SOURCE == 'merge_request_event' # must be a true MR trigger pipeline and not a branch pipeline with a push source
  5. Fork the project (ex.root/gitlab-cost-factor-fork) and create an MR pipeline
  6. Give the group it was forked into some minutes
    Namespace.find_by_path('root').update!(shared_runners_minutes_limit: 10000)
  7. Navigate to the pipeline page http://localhost:3000/root/gitlab-cost-factor-fork/-/pipelines/new
  8. Click the run pipeline button and wait for the pipeline to finish (background jobs and shared runners must be functional).
  9. Tail the logs and check for the logging added
     tail -f */*.log
     ...
     ==> log/application.log <==
     2022-07-13T19:38:19.486Z: {:message=>"GitLab contributor cost factor granted", :cost_factor=>0.03333333333333333, :project_path=>"gitlab-org/gitlab-cost-factor", :pipeline_id=>287, :class=>"Gitlab::Ci::Minutes::BuildConsumption"}
    
     ==> log/application_json.log <==
     {"severity":"INFO","time":"2022-07-13T19:38:19.486Z","correlation_id":"01G7WH2X7A561NG9HB6WHVDVWH","message":"GitLab contributor cost factor granted","cost_factor":0.03333333333333333,"project_path":"gitlab-org/gitlab-cost-factor","pipeline_id":287,"class":"Gitlab::Ci::Minutes::BuildConsumption"}
     ...
  10. Run other ci pipelines for other projects and ensure "GitLab contributor cost factor granted" is not rendered in the logs

MR acceptance checklist

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

Edited by Allison Browne

Merge request reports