Nested variable not evaluated before transfer to child pipeline when passed with another name

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

Summary

Nested variables (e.g. MY_VAR='${CI_PROJECT_NAME}-${CI_PIPELINE_IID}' are not evaluated before being sent to a child pipeline when passed with another name (e.g. TRIGGER_VAR='${MY_VAR}')

Steps to reproduce

# parent project
---
build1:
  stage: build
  variables:
    VERSION: "${CI_PROJECT_NAME}-${CI_PIPELINE_IID}"
  script:
    - echo "VERSION='${VERSION}'"

deploy1:
  stage: deploy
  variables:
    VERSION: "${CI_PROJECT_NAME}-${CI_PIPELINE_IID}"
    APP_VERSION: ${VERSION}
  trigger:
    strategy: depend
    project: child-project
# child project
---
deploy1:
  stage: deploy
  script:
    - echo "APP_VERSION='${APP_VERSION}'"

Example Project

What is the current bug behavior?

Output on parent project (as expected):

$ echo "VERSION='${VERSION}'"
VERSION='parent-project-2'

Output on child project:

$ echo "APP_VERSION='${APP_VERSION}'"
APP_VERSION='child-project-3'

When looking at the pipeline variables through the API, we can see that the value for APP_VERSION is the composed string, not the value of VERSION:

// curl -s -H 'PRIVATE-TOKEN: ...' "https://gitlab.com/api/v4/projects/<project_id>/pipelines/<pipeline_id>/variables"
[
  {
    "variable_type": "env_var",
    "key": "APP_VERSION",
    "value": "${CI_PROJECT_NAME}-${CI_PIPELINE_IID}"
  },
  {
    "variable_type": "env_var",
    "key": "VERSION",
    "value": "parent-project-2"
  }
]

What is the expected correct behavior?

Output on child project:

$ echo "APP_VERSION='${APP_VERSION}'"
APP_VERSION='parent-project-2'
// curl -s -H 'PRIVATE-TOKEN: ...' "https://gitlab.com/api/v4/projects/<project_id>/pipelines/<pipeline_id>/variables"
[
  {
    "variable_type": "env_var",
    "key": "APP_VERSION",
    "value": "parent-project-2"
  },
  {
    "variable_type": "env_var",
    "key": "VERSION",
    "value": "parent-project-2"
  }
]

Relevant logs and/or screenshots

N/A

Output of checks

This bug happens on GitLab.com

Results of GitLab environment info

N/A

Possible fixes

Looks like the equivalent of !48627 (merged) for triggers.

A fix would be to use sort_and_expand_all(keep_undefined: true) in https://gitlab.com/gitlab-org/gitlab/-/blob/02f5614e3791768ceb29f5f846dd76be9225a350/app/models/ci/bridge.rb#L204

Edited by 🤖 GitLab Bot 🤖