11

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.

Craig Otis
  • 31,257
  • 32
  • 136
  • 234

2 Answers2

25

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

Grisha Levit
  • 8,194
  • 2
  • 38
  • 53
  • 1
    You could expand the answer with information about the first example (folded multiline string), it's rather hard to guess what it does the way it is now. – Jakub Kania Jan 23 '17 at 09:10
2

I made it work like this:

  script: |
   "EXPECT_SERVER_OUTPUT='{\"message\": \"Hello World\"}'"
   echo $EXPECT_SERVER_OUTPUT
user48656
  • 131
  • 2