How to properly use yaml anchors for rules to include files?

Problem to solve

I want to include a file based on rules specified through yaml anchors. The goal of doing this is to organize workflows with reusable rules to improve readability and maintainability.

  • What are you seeing, and how does that differ from what you expect to see?

The rules with yaml anchors works correctly when $CI_PIPELINE_SOURCE is push, but not when it is merge_request_event.

Configuration

Add the CI/CD configuration from .gitlab-ci.yml and other configuration if relevant (e.g. docker-compose.yml). Alternatively, create a public GitLab.com example project that provides all necessary files to reproduce the question.

Created a public repository with MVCE (minimum verifiable complete example) here: Aravind Pai / honeycomb · GitLab

Here is the main.yml file for convenient reference:

.push_to_default_branch: &push_to_default_branch
  - if: $CI_PIPELINE_SOURCE == "push"

.merge_request_to_default_branch: &merge_request_to_default_branch
  - if: $CI_PIPELINE_SOURCE == "merge_request_event"

print-env:
  stage: demo
  rules:
    - when: always
  script:
    - export

include:
  # note: commenting out the push-job below also doesn't
  # lead to the `merge-request-job` being included
  - local: .gitlab-ci/include-with-rules/push-job.yml
    rules:
      - *push_to_default_branch
  # end note
  - local: .gitlab-ci/include-with-rules/merge-request-job.yml
    rules:
      - *merge_request_to_default_branch

I cannot for the life of me figure out why the include with rules behaves differently between push and merge_request_event.

What have I missed here?

Versions

Please select whether options apply, and add the version information.

  • Self-managed
  • GitLab.com SaaS
  • Self-hosted Runners

Versions