Variable expansion in rules:exists not working for subdirectories

Workaround

variables:
  SUB: subdir
  TEST: tests

job:
  rules:
    - exists:
        - $SUB/$TEST.py

Summary

This issue introduced a feature for being able to use variables inside rules:exists: #283881 (closed)

This works if the path inside the used variable does not contain any slashes, i.e. points directly to a file on the root of the repository. If the file however is located in a subdirectory, the rule will not match anymore.

We also assume that this is the underlying cause for this bug report: #411344 (closed)

Steps to reproduce

I.e. this works:

variables:
  PATH: "Dockerfile"

build_image:
  image: busybox
  stage: test
  script:
    - echo hello
  rules:
    exists:
      - $PATH

But this does not work:

variables:
  PATH: "subdir/Dockerfile"

build_image:
  image: busybox
  stage: test
  script:
    - echo hello
  rules:
    exists:
      - $PATH

We're currently on GitLab version 15.11.3-ee. We also tested this on 16.1.2-ee with the same results.

Example Project

What is the current bug behavior?

Jobs will not be added to the pipeline if the path of the file referenced in the variable which is used in rules:exists is not in the root of the repository

What is the expected correct behavior?

Jobs will be added to the pipeline if the path of the file referenced in the variable which is used in rules:exists is not in the root of the repository

Relevant logs and/or screenshots

Output of checks

Results of GitLab environment info

Expand for output related to GitLab environment info

(For installations with omnibus-gitlab package run and paste the output of:
`sudo gitlab-rake gitlab:env:info`)

(For installations from source run and paste the output of:
`sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production`)

Results of GitLab application Check

Expand for output related to the GitLab application check

(For installations with omnibus-gitlab package run and paste the output of: sudo gitlab-rake gitlab:check SANITIZE=true)

(For installations from source run and paste the output of: sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true)

(we will only investigate if the tests are passing)

Possible fixes

Edited by Dov Hershkovitch