Bug Report: CI_OPEN_MERGE_REQUESTS variable not available in workflow:rules unless referenced elsewhere

Summary

The $CI_OPEN_MERGE_REQUESTS predefined variable is not properly available during workflow:rules evaluation. The variable only works in workflow rules if it's explicitly referenced elsewhere in the pipeline configuration (e.g., in a job), requiring users to add workaround "debug code" to achieve the intended behavior.

Steps to reproduce

  1. Create a GitLab project with merge request pipelines enabled
  2. Configure .gitlab-ci.yml with the documented workflow rules (without workaround)
  3. Push to a branch, then create a merge request
  4. Observe that both branch and merge request pipelines are created
  5. Add a job that references $CI_OPEN_MERGE_REQUESTS
  6. Repeat steps 3-4 and observe that duplicate pipelines are now prevented

Example Project

A minimal example project demonstrating this issue can be created with the following .gitlab-ci.yml:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH

What is the current bug behavior?

  • The $CI_OPEN_MERGE_REQUESTS variable is not available during workflow rules evaluation
  • Duplicate pipelines are created (both branch and merge request pipelines)
  • The when: never rule doesn't trigger when it should
  • Users must implement workarounds by referencing the variable elsewhere in the pipeline

What is the expected correct behavior?

The $CI_OPEN_MERGE_REQUESTS variable should be consistently available in workflow:rules to prevent duplicate pipelines, as documented in the GitLab CI/CD workflow documentation, without requiring additional code or workarounds.

Relevant logs and/or screenshots

# Current workaround needed:
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH

# Workaround job required:
debug_job:
  script:
    - echo "Open MRs: $CI_OPEN_MERGE_REQUESTS"  # Forces variable evaluation

Possible fixes

The issue appears to be in:

  • lib/gitlab/ci/variables/builder/pipeline.rb
  • lib/gitlab/ci/pipeline/chain/evaluate_workflow_rules.rb

Potential solutions:

  1. Move variable evaluation earlier in the pipeline creation sequence
  2. Ensure all required pipeline associations are loaded before workflow rules evaluation
  3. Modify the variable evaluation logic to be more robust during early pipeline creation phases

Description was generated using AI

Edited by 🤖 GitLab Bot 🤖