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
- Create a GitLab project with merge request pipelines enabled
- Configure
.gitlab-ci.ymlwith the documented workflow rules (without workaround) - Push to a branch, then create a merge request
- Observe that both branch and merge request pipelines are created
- Add a job that references
$CI_OPEN_MERGE_REQUESTS - 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_REQUESTSvariable is not available during workflow rules evaluation - Duplicate pipelines are created (both branch and merge request pipelines)
- The
when: neverrule 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.rblib/gitlab/ci/pipeline/chain/evaluate_workflow_rules.rb
Potential solutions:
- Move variable evaluation earlier in the pipeline creation sequence
- Ensure all required pipeline associations are loaded before workflow rules evaluation
- Modify the variable evaluation logic to be more robust during early pipeline creation phases
Description was generated using AI
Edited by 🤖 GitLab Bot 🤖