bashyamlgitlabgitlab-ci

Unable to print string containing double quotes in GitLab CI YAML


I'm using the CI Lint tester to try and figure out how to store an expected JSON result, which I later compare to a curl response. Neither of these work:

Attempt 1

---
  image: ruby:2.1
  script:
  - EXPECT_SERVER_OUTPUT='{"message": "Hello World"}'

Fails with:

did not find expected key while parsing a block mapping at line 4 column 5

Attempt 2

---
  image: ruby:2.1
  script:
  - EXPECT_SERVER_OUTPUT="{\"message\": \"Hello World\"}"

Fails with:

jobs:script config should be a hash

I've tried using various combinations of echo as well, without a working solution.


Solution

  • You could use literal block scalar1 style notation and put the variable definition and subsequent script lines on separate lines2 without worrying about quoting:

    myjob:
      script:
        - |
          EXPECT_SERVER_OUTPUT='{"message": "Hello World"}'
    

    or you can escape the nested double quotes:

    myjob:
      script:
        - "EXPECT_SERVER_OUTPUT='{\"message\": \"Hello World\"}'"
    

    but you may also want to just use variables like:

    myjob:
      variables:
        EXPECT_SERVER_OUTPUT: '{"message": "Hello World"}'
      script:
        - dothething.sh
    

    Note: variables are by default expanded inside variable definitions so take care with any $ characters inside the variable value (they must be written as $$ to be literal). This feature can also be turned off.

    1See this answer for an explanation of this and related notation
    2See this section of the GitLab docs for more info on multi-line commands