Variables used in include:rules are not expanded

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Summary

Variables used in include:rules are not expanded, which prevents the feature from being practically used.

Steps to reproduce

  1. Specify an include file directive in any Gitlab CI yml file.
  2. Use the rules directive for conditionally including the file.
  3. Use a variable in this rule.
  4. Be amazed that Gitlab CI does not support this obvious functionality, and how Gitlab regularly releases features without usability testing.

Example Project

What is the current bug behavior?

Variable used in an include:rules is ignored. As a result, the rule fails and the file is not included. This makes the entire conditional include feature practically useless for several real life scenarios.

This behavior is also poor UX since it violates internal consistency of the system and principle of least surprise, because variables in rules are supported in other contexts such as workflow:rules and trigger:rules.

What is the expected correct behavior?

The variables used in include:rules should be expanded and the rule should be properly evaluated to determine if the file is included in the pipeline.

Relevant logs and/or screenshots

Workflow

variables:
  TARGET_BRANCH: include-rules-with-ci-variable

include:
  - local: .gitlab-ci/include-rules-with-ci-variable/on-push-to-branch.yml
    rules:
      - if: >
          $CI_PIPELINE_SOURCE == "push"
          && $CI_COMMIT_BRANCH == "include-rules-with-ci-variable"    # note: this file gets included

  - local: .gitlab-ci/include-rules-with-ci-variable/on-push-to-branch-as-variable.yml
    rules:
      - if: >
          $CI_PIPELINE_SOURCE == "push"
          && $CI_COMMIT_BRANCH == $TARGET_BRANCH    # note: this file does not get included

Pipeline behaviour

The file gets included when the string is directly compared in the rule, but not when that same string is assigned to a variable.

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 🤖 GitLab Bot 🤖