I write a GitLab CI YAML.
This:
test:
script:
- echo 'hello: world'
Fails with the error: "This GitLab CI configuration is invalid: jobs:test:script config should be a string or a nested array of strings up to 10 levels deep."
I remove the space after the colon:
test:
script:
- echo 'hello:world'
And it validates.
I am able to work around this with using a block scalar:
test:
script:
- >-
echo 'hello: world'
But I would like to understand what's happening, and if there is a way to get the echo to work without using a block scalar.
what's happening
I copy the yaml into "online yaml parser" and get:
{
"test": {
"script": [
{
"echo 'hello": "world'"
}
]
}
}
It is interpreting echo 'hello
as a key, and world'
as a value of a dictionary. The quotes '
"
are only special if the whole thing is inside quotes. Otherwise, the :
starts a dictionary in YAML.
To fix, you can do this:
test:
script:
- "echo 'hello: world'"
or this, which is exactly the same thing. YAML is not shell, double ''
inside '
are interpreted as a single '
.
test:
script:
- 'echo ''hello: world'''
But I usually just write it like this. You could use |-
or >-
, but the trailing newline just does not matter and I find |
easy to remember.
test:
script:
- |
echo 'hello: world'
You can test it in one of "online yaml parsers" to see what json happens out of them.
You can check YAML specification https://yaml.org/spec/1.2.2/ for exact parsing rules.