'How to escape colon with following space in a GItLab CI/CD YAML value?

I have the following line in GitLab CI/CD:

  script:
    - echo "Backend image: $BACKEND_IMAGE"

But YAML interpreters treat this as an object. Then I googled this issue and tried this:

  script:
    - echo "Backend image:: $BACKEND_IMAGE"

But it still doesn't work and GitLab job fails with the following:

jobs:deploy review:script config should be a string or a nested array of strings up to 10 levels deep

If I remove colons at all it works fine. How to make a string value with colon and following space in GitLab CI/CD?



Solution 1:[1]

It should work if you surround your string with single quotes:

script:
    - 'echo "Backend image: $BACKEND_IMAGE"'

Solution 2:[2]

Maybe you can try and put your string in a variable first, then echo that variable:

- ECHO_STRING=$(echo "Backend image:: $BACKEND_IMAGE")
- echo $ECHO_STRING

If not, try:

- ECHO_STRING=$(echo "Backend image:\ $BACKEND_IMAGE" | tr -d '\')
- echo $ECHO_STRING>>

(Replace ECHO_STRING by a more meaning variable name)

Solution 3:[3]

After going through several options, I found this below simple solution Use like this,

Image="Backend image:"
export RESULT="$(echo "$Image" $BACKEND_IMAGE)" 
echo "Result is " $RESULT

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 slauth
Solution 2 VonC
Solution 3