Add CI_UPSTREAM_PROJECT_ID to pipeline predefined variables

What does this MR do and why?

This MR introduces CI_UPSTREAM_PROJECT_ID as a new pre-pipeline predefined variable for multi-project pipelines.
It lets a downstream pipeline know which upstream project triggered it, without having to manually pass a custom variable.

This MR:

  • Adds a downstream_predefined_variables method to Gitlab::Ci::Variables::Downstream::Generator
    • When bridge.triggers_cross_project_pipeline? is true, stores the upstream project ID in CI_UPSTREAM_PROJECT_ID.

References

Related to #377694

How to set up and validate locally

  1. In your GDK instance, create two projects (or reuse existing ones):

    • an upstream project
    • a downstream project
  2. In the downstream project, create a .gitlab-ci.yml like:

    stages:
      - test
    
    print_upstream_vars:
      stage: test
      script:
        - echo "CI_UPSTREAM_PROJECT_ID=$CI_UPSTREAM_PROJECT_ID"
        - echo "CI_UPSTREAM_PIPELINE_ID=$CI_UPSTREAM_PIPELINE_ID"
        - echo "CI_UPSTREAM_JOB_ID=$CI_UPSTREAM_JOB_ID"
  3. In the upstream project, create a .gitlab-ci.yml that triggers the downstream project via a bridge job, for example:

    stages:
      - trigger
    
    trigger_downstream:
      stage: trigger
      trigger:
        project: <your-namespace>/<downstream-project-path>
        strategy: depend
  4. Run a pipeline in the upstream project (for example, by pushing a commit or using Run pipeline in the UI).

  5. When the downstream pipeline is created and the print_upstream_vars job runs, open the job log.

Expected result

  • CI_UPSTREAM_PROJECT_ID is set to the ID of the upstream project.
  • CI_UPSTREAM_PIPELINE_ID is set to the ID of the upstream pipeline that triggered the bridge.
  • CI_UPSTREAM_JOB_ID is set to the ID of the bridge job in the upstream pipeline.
  • All three variables are available as predefined variables in the downstream job’s environment.

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.

Edited by Kouta Kumagai

Merge request reports

Loading