Running pipeline returns No stages / jobs for this pipeline

I’m trying to run gitlab CI stages based on which files changed in the MR. The changes: rule is not sufficient because for example:
A MR contains changes in two different directories X & Y and two stages that are run based on changes in X or Y.
If you run the pipeline in the MR first time and job X runs and fails the merge can’t happen, but if you run it a second time with changes to Y then the pipeline runs only stage Y and it succeeds. Even though the first pipeline which had errors in directory X failed.
My attempt on this is:

image: docker:latest

  - docker:dind

  stage: build
    DIR_IN_MR: $(git diff --name-status $CI_MERGE_REQUEST_TARGET_BRANCH_NAME..$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME | cut -f2 | cut -d '/' -f 1 | uniq | grep -q dir_x && echo $?)
    - if: $DIR_IN_MR == "0"
    - echo 1

My problem is when I try to run it, it returns No stages / jobs for this pipeline even though dir_x exists in the project. How can this be solved?

Hi @alterEgo123

variables are not evaluated like in Shell, so the value of your DIR_IN_MR variable is actually a string $(git diff --name-status $CI_MERGE_REQUEST_TARGET_BRANCH_NAME..$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME | cut -f2 | cut -d '/' -f 1 | uniq | grep -q dir_x && echo $?)

Variables are only string values, there is no expantion performed.

Thank you for your response,
Is there any workaround for this?

The rules.changes won’t work for you?

It causes faulty results because in a single MR if it takes into consideration directory x which fails the pipeline at first and the merge can’t happen, and afterwards it takes into consideration directory y that makes the pipeline succeed the merge will happen because the latest pipeline is successful.

Nope, it will execute the job if the change happened in previous commits as well.

This is an example
Screenshot from 2022-04-07 11-15-14
The gitlab-ci file contains the rules.changes, but only took it into consideration per commit

Maybe we are both talking about something else? Because I cannot replicate what you are saying, see MR1

2 commits, 2 pipelines. First commit faulty, pipeline failed. Second commit to a different directory, pipeline still failed, because both commits are tested.

EDIT: Created MR2 after 2 commits in a branch already exist = 1 pipeline. Pipeline failed, because changes from both commits are tested

EDIT2: Created MR3 with single job to run if change in any directory. Still fails.