Add CI_JOB_RETRY_COUNT predefined CI/CD variable

What does this MR do and why?

Adds CI_JOB_RETRY_COUNT, a predefined CI/CD variable containing the number of times the current job has been retried within its pipeline. It is 0 on the first run and increases by one with each retry.

Performance

This adds one COUNT query per job variable resolution, in practice, once when a runner picks up a job.

It is not an N+1. persisted_variables has exactly one caller, Ci::Build#base_variables, which is strong_memoize_attr, and #variables is memoized separately, so the query runs once per build instance rather than once per variable lookup.

How to set up and validate locally

  1. Add a job to .gitlab-ci.yml that prints the variable:

    retry-check:
      script:
        - echo "attempt $CI_JOB_RETRY_COUNT"
        - if [ "$CI_JOB_RETRY_COUNT" -eq 0 ]; then echo "first attempt"; fi
        - exit 1
    
  2. Run the pipeline and let the job fail. The log shows attempt 0.

  3. Retry the job. The log shows attempt 1, and so on for each retry.

Related to #27589 (closed)

Merge request reports

Loading
Loading