Assigning an undefined variable to itself causes it to be populated with the name of the variable

Summary

Within a .gitlab-ci.yml Job:Variables block, if you assign the empty variable $FOO to BAR, $BAR will be undefined in scripts as expected.

If you instead assign empty variable $BAR to BAR, there is a bugged behaviour where $BAR has a string as its content.

This occurs in both regular jobs and trigger: jobs. It is a huge nuisance for trigger jobs where you would like the downstream pipeline to have the same variable values as the upstream (even if undefined). However I only have included reproduction steps for regular jobs because those are easier.

Steps to reproduce

Create a .gitlab-ci.yml like the below:

Test_Different_name:
    variables:
        MY_VARIABLE: $UNDEFINED_VARIABLE
    script:
        - echo $UNDEFINED_VARIABLE # empty output
        - echo $MY_VARIABLE # empty output

Test_Same_Name:
    variables:
        UNDEFINED_VARIABLE: $UNDEFINED_VARIABLE
    script:
        - echo $UNDEFINED_VARIABLE # output is "$UNDEFINED_VARIABLE"

Observe that in the Test_Different_name job, both $UNDEFINED_VARIABLE and $MY_VARIABLE are empty Conversely, in the Test_Same_Name, $UNDEFINED_VARIABLE has the value "$UNDEFINED_VARIABLE" as a string

Example Project

https://gitlab.com/jadentom/demonstrate-undefined-variable-assignment-bug/

What is the current bug behavior?

Assigning an undefined variable to a variable with the same name makes it defined with a string name.

What is the expected correct behavior?

The variable should be empty.

Relevant logs and/or screenshots

Test_Different_Name output https://gitlab.com/jadentom/demonstrate-undefined-variable-assignment-bug/-/jobs/1994119915

$ echo $UNDEFINED_VARIABLE
$ echo $MY_VARIABLE

Test_Same_Name output https://gitlab.com/jadentom/demonstrate-undefined-variable-assignment-bug/-/jobs/1994119916

$ echo $UNDEFINED_VARIABLE
$UNDEFINED_VARIABLE

Output of checks

This bug happens on GitLab.com

Results of GitLab environment info

Running with gitlab-runner 14.7.0~beta.58.gfa48f33b (fa48f33b)

GitLab Enterprise Edition 14.7.0-pre dcddbf83

Results of GitLab application Check

Not relevant

Possible fixes

Unknown

Edited by Jaden Tom