Having issues with multiline strings in .gitlab-ci.yml

I am having issues representing multi-line strings in .gitlab-ci.yml. I have tried various options but nothing worked. I am trying to represent the following string in .gitlab-ci.yml using the right yaml indicators, so that it gets translated correctly. The following is a single line string containing both spaces and no spaces. I am struggling to make it work with .gitlab-ci.yml.

javac @/mnt/c/Temp/build/sources.txt -classpath /mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/bc-client.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/dif.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/difext.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/javacsv-2.0.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/java-jwt-2.2.1.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/javax.ws.rs-api-2.1.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/commons-beanutils.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/commons-collections.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/commons-digester.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/commons-lang3-3.7.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/commons-logging.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/commons-text-1.8.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/json_simple-1.1.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/log4j-1.2.12.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/okhttp-3.8.1.jar:/mnt/c/RTC/sandbox/JavaWorkspace/JavaProject/WebContent/WEB-INF/lib/okio-1.13.0.jar:/mnt/c/Projects/12021/GitLab_Pipelines/RTC/CMPJBE1_Build_Machine/jars/buildpath/commons-lang-2.1.jar:/mnt/c/Projects/12021/GitLab_Pipelines/RTC/CMPJBE1_Build_Machine/jars/frameworkLib/FrameworkLib.jar:/mnt/c/Projects/12021/GitLab_Pipelines/RTC/CMPJBE1_Build_Machine/jars/IBM_WAS_jars/j2ee.jar -d /mnt/c/Temp/build/classes 2>/mnt/c/Temp/build/logs/JavaProject_compile_output_Mar212022_1.txt

Hi @akaiserk
I am not sure where you want to use such a string, but in general:

  • Use > most of the time: interior line breaks are stripped out, although you get one at the end:
  key: >
    Your long
    string here.
  • Use | if you want those linebreaks to be preserved as \n (for instance, embedded markdown with paragraphs).
  key: |
    ### Heading

    * Bullet
    * Points
  • Use >- or |- instead if you don’t want a linebreak appended at the end.
1 Like

In case all else fails, you could of course put all of that on a single line.

I’ve been banging my head against the wall a couple of times now due to newline issues in GitLab CI/CD YAML. For a one-liner like yours, you can use something like

script:
  - echo
      hello world

That is, you should not use the typical shell new-line escapes.

I think that using @balonik’s > approach would also work.

script:
  - >
    echo
      hello world

However, if you use the | approach, you’ll need to escape your newlines

script:
  - |
    echo \
      hello world

Hope this is correct and helps.