I've got this cURL command:
curl -X POST --data-urlencode 'payload={"text": "A new version has been deployed to production."}' https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX
I need to use this in a GitLab CI file which has the Yaml support. The Yaml parser does not accept it. Normally I would surround it in quotes but I already use both quotes and apostrophes in the command.
This is how the command looks right now.
How can I escape the sequence properly?
deploy:
stage: deploy
script:
- "curl -X POST --data-urlencode 'payload={"text": "A new version has been deployed to production."}' https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX"
In YAML, \n is used to represent a new line, \t is used to represent a tab, and \\ is used to represent the slash. In addition, since whitespace is folded, YAML uses bash style "\ " to escape additional spaces that are part of the content and should not be folded.
The backslash ( \ ) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
The DQUOTE function extracts characters enclosed in double quote marks.
The easiest way would be to use a block scalar:
deploy:
stage: deploy
script: |-
curl -X POST --data-urlencode 'payload={"text": "A new version has been deployed to production."}' https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX
|
starts a literal block scalar, -
tells YAML to discard the trailing newline (which would otherwise be part of the scalar). For readability, you can use a folded block scalar instead (newlines will be converted to spaces):
deploy:
stage: deploy
script: >-
curl -X POST --data-urlencode 'payload={"text":
"A new version has been deployed to production."}'
https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX
Finally, it is also possible to use double quotes, as long as you escape the double quotes inside the scalar (newlines are also folded into spaces):
deploy:
stage: deploy
script:
"curl -X POST --data-urlencode 'payload={\"text\":
\"A new version has been deployed to production.\"}'
https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With