Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 457
Craig Otis Avatar asked Jan 22 '17 21:01

Craig Otis


2 Answers

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 expanded inside variable definitions so take care with any $ characters inside the variable value (they must be written as $$ to be literal).

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

like image 105
Grisha Levit Avatar answered Oct 30 '22 23:10

Grisha Levit


I made it work like this:

  script: |
   "EXPECT_SERVER_OUTPUT='{\"message\": \"Hello World\"}'"
   echo $EXPECT_SERVER_OUTPUT
like image 22
user48656 Avatar answered Oct 30 '22 22:10

user48656