Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quoting YAML (for Travis CI)

How would I escape a whole line in YAML? I want to have json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}' in a list, but I can't get it to parse into a string. I can put single quotes around the whole line, but then I would have to escape every single quote in my string, making it very hard to read. The string will be run as a bash command in Travis CI

like image 595
saul.shanabrook Avatar asked May 13 '12 14:05

saul.shanabrook


People also ask

What is Travis Yaml?

travis. yml , which is a YAML format text file, to the root directory of the repository. This file specifies the programming language used, the desired building and testing environment (including dependencies which must be installed before the software can be built and tested), and various other parameters.


1 Answers

The most elegant solution is to use the literal style | indicator, with the - modifier to strip the final newline. That way there are no extra quotes necessary.

If this scalar happens to be the only thing in a YAML file use:

|-
  json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}'

if it is a mapping value for key abc:

abc: |-
  json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}'

or if it is part of a list:

- |-
  json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}'
like image 122
Anthon Avatar answered Oct 11 '22 18:10

Anthon