Backend: Extract predefined variables builder from project classes

Description

We want to optimize the workflow of calculating build variables, as per #213560 (closed) and #22520 (closed).

In order to make it easier to optimize calculating build variables, we might want to extract variables builder, to a separate class / module. This will make it easier to profile this code, and find deficiencies.

Proposal (This is for project classes only)

Extract Ci::Variables::Builder class or module. Use dependency injection to manage dependencies on objects that are required to calculate variables for project.

Ensure that we can remove duplicate computations with memoization. For example, we should be able to use the same builder for multiple builds in a pipeline to make the most of memorization.

Project predefined variables still need to be moved into its separate class / module.

Expected improvements:

  • we should have the logic to compute CI variables in one place
  • easy to memoize variables for project across multiple builds
  • easy to profile and make further improvements when needed

Additional Context

Details can be found here

Edited by Mark Nuzzo