Job's rules aren't evaluated

Dear community,
I have a GitLab pipeline, looking like this (the most part is omitted)

variables:
  X_CI_BUILD_KIND: 'develop'

.....

ENV_PREPARE:
  stage: prepare
  script:
    - |
      if [[ "${CI_PIPELINE_SOURCE}" == 'merge_request_event' ]]; then
         if [[ "${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}" =~ ^(stable|main).*$ ]]; then
             X_CI_BUILD_KIND="release"
         fi
      fi
      echo X_CI_BUILD_KIND=${X_CI_BUILD_KIND} >> $CI_PROJECT_DIR/build.env
  artifacts:
    expire_in: 1 week
    reports:
      dotenv: env.env


.....

PUBLISH:
  stage: publish
  script:
    - *x_storage_publish
  rules:
    - if: '$X_CI_BUILD_KIND == "release"'

When I’m creating an MR and my $X_CI_BUILD_KIND becoming equal to "release", I still can’t see "PUBLISH" job in the pipeline.

I assume this is because of the magic of the rules (they can be calculated a long time before $X_CI_BUILD_KIND changed to "release".

Could you please prove my assumption and more interesting question - how to workaround in that case? I truly need my $X_CI_BUILD_KIND variable :slight_smile: