Rules:changes ignored in detached pipeline

rules:changes are being ignored when running in detached pipeline

Hello I recently started using Gitlab CI/CD to run a few unit tests.

What I’m trying to do:

  • only run jobs on merge requests to master which are not set as Draft or WIP.
  • run specific jobs only when previous condition and a certain file is changed.

What I’m seeing

  • Jobs that depend on specific file changes are being triggered when any push is made.
    For example, if I change Readme.md, the jobs are triggered.

GitLab Versions:

  • I am using Gitlab.com
  • Running jobs on a docker container that is running gitlab-runner 14.2, on my machine.

Here’s a similar.gitlab-ci.yml (removed much of script/services/docker variables to save you the trouble):

stages:
  - build_environment
  - test_environment
  - deploy_environment
  - build
  - test
  - deploy

update_requirements_txt:
  stage: build_environment
  rules:
    - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" && $CI_MERGE_REQUEST_TITLE !~ /^WIP.*/ && $CI_MERGE_REQUEST_TITLE !~ /^Draft.*/) 
      changes:
        - scripts/dockerfile #do not trigger here because the other job will perform a full install
      when: never
    - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" && $CI_MERGE_REQUEST_TITLE !~ /^WIP.*/ && $CI_MERGE_REQUEST_TITLE !~ /^Draft.*/) 
      changes:
        - scripts/requirements.txt
      when: always
    - when: never 
  before_script:
    - echo 'before_script'
  script:
    - echo 'script'

update_dockerfile:
  stage: build_environment
  rules:
    - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" && $CI_MERGE_REQUEST_TITLE !~ /^WIP.*/ && $CI_MERGE_REQUEST_TITLE !~ /^Draft.*/) 
      changes:
        - scripts/dockerfile
      when: always
    - when: never 
    before_script:
    - echo 'before_script'
  script:
    - echo 'script'

test_container:
  stage: test_environment
  rules:
    - !reference [.default_container_rules]
   before_script:
    - echo 'before_script'
  script:
    - echo 'script'

release_container:
  stage: deploy_environment
  rules:
    - !reference [.default_container_rules]
   before_script:
    - echo 'before_script'
  script:
    - echo 'script'
    
run_unit_tests:
  stage: test
  rules:
    - !reference [.default_rules]
   before_script:
    - echo 'before_script'
  script:
    - echo 'script'

.default_rules:
  #only run on merge requests to master which are not marked as draft or WIP
  - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" && $CI_MERGE_REQUEST_TITLE !~ /^WIP.*/ && $CI_MERGE_REQUEST_TITLE !~ /^Draft.*/) 
    when: always
  - when: never 

.default_container_rules:
  #only run on merge requests to master which are not marked as draft or WIP
  #and only when there are changes in bone or the other file
  - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" && $CI_MERGE_REQUEST_TITLE !~ /^WIP.*/ && $CI_MERGE_REQUEST_TITLE !~ /^Draft.*/) 
    changes:
      - scripts/requirements.txt
    when: always
  - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" && $CI_MERGE_REQUEST_TITLE !~ /^WIP.*/ && $CI_MERGE_REQUEST_TITLE !~ /^Draft.*/) 
    changes:
      - scripts/dockerfile
    when: always
  - when: never 

When I have a merge request from branch A to master and push a change to a readme.md file, for example, to branch A.

Instead of running only:
run_unit_tests

It runs:
update_dockerfiletest_containerrelease_containerrun_unit_tests

If you think adding the specific services and images used will be useful let me know.

Troubleshooting steps:

I’ve tried running the same configuration but without the restrictions
$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
and it creates both a branch pipeline and a detached pipeline.

In the regular pipeline the correct jobs are executed but in the detached pipeline, the changes seem to be ignored.

What is exactly a ‘detached pipeline’? The docs say it is the same as any other pipeline but without access to the protected variables. I don’t understand how that can cause such differences.

If anyone has any suggestions on how to proceed or maybe corrections to my configs, please let me know.

Thank you