Fix "variable definition must be a string or hash" bug for CI text interpolation
Context
Users began to see "variable definition must be a string or hash" errors when trying to run pipelines after we began rolling out ci_text_interpolation
Technical details
In order to avoid indentation errors, text interpolation casts most input types to JSON before inserting them into the YAML content. However, casting strings to JSON adds quotes around, which breaks interpolation when inserting a string into another string. For example, $[[ inputs.job_name ]]_job
becomes "test"_job
instead of test_job
.
To get around this, the first iteration of text interpolation didn't cast strings to JSON. However, this creates another problem where strings that look like other data types (for example, "true"
or "6.6"
) get interpolated as non-strings (true
and 6.6
) when we don't include quotes.
This causes the "variable definition must be a string or hash" error to be raised because our variable value validator only considers strings, symbols, and integers to be valid alphanumeric values.
The bigger problem is that this means that the type of string inputs isn't respected when those strings look like other data types.
Solution
We may have to test a few solutions to find one we're confident in.
Option 1: Check whether we're inserting the string between whitespace. If we are, cast it to JSON so it has surrounding quotes and gets parsed as a string. If we're inserting it between other characters, don't cast it to avoid getting extra quotes inserted into the YAML.
Option 2: ???