Search code examples
gitlabcontinuous-integrationcicd

Gitlab CI/CD .gitlab-ci.yml file invalid on spaces in strings?


This is pretty bizarre.

I opened file .gitlab-ci.yml in Gitlab's Pipeline Editor.

When I have double-quoted string without spaces in the file it's OK:

enter image description here

Note no space between PRIVATE-TOKEN: and $EXAMPLE_TOKEN.

Pipeline Editor says " Pipeline syntax is correct." and all is fine, I can run the pipeline on commit etc.

However, when I enter a space there the editor complains:

enter image description here

At the top the editor displays message:

"This GitLab CI configuration is invalid: jobs:build_appliance:after_script config should be a string or a nested array of strings up to 10 levels deep."

Trying to run the pipeline on commit fails with messages about invalid YAML. The strange thing is I checked this string in an online YAML validator and it says it's fine.


Solution

  • This is a common issue even I face when I use curl with "--" options. The Gitlab yaml parser gets thrown off by this "--".

    Here is how you can work around this. Please note the "|" at the begining of your after_script, with which you can avoid both adding a "-" on each line of your script and also this solves the nested string error message.

    You can use "|" similarly in before_script and script as well.

    sample after script with dummy echo just to show mutiple lines of script

    after_script: |
      echo "I dont have to use "-" on each line os script section anymore"
      curl --globoff --header "PRIVATE-TOKEN: <your_access_token>" "<your gitlab api url>"
      echo "more lines..."