'Cannot escape triple backtick embedded in a string in github action yaml

I have a GitHub Action in which a JSON report is generated. I want to post that JSON content to the PR, but I want it formatted properly. To do that, I expect I need to surround it with triple backticks to create a code block:

```json
<file content>
```

I'd indent with spaces (like I have for this question), but then I wouldn't get the syntax highlighting for the JSON.

What I'm doing:

run: |
  echo "<details><summary>Expand to see report</summary>" >> pr-message.md
  echo "\`\`\`json" >> pr-message.md
  cat my-json-content.json >> pr-message.md
  echo "\`\`\`" >> pr-message.md
  echo "</details>" >> pr-message.md

This should write a <details> element, the backtick sequence, the file, the backtick sequence, and then close the <details> element. (In the next step, I use a defined action to post the file to the PR.)

I've tried with and without escaping the backticks. Without the escapes, it skips over the cat entirely, and pr-message.md just contains the HTML elements. With the escapes, I get an error about GHA failing to parse the YAML file due to a syntax issue at the next step, and the action doesn't run at all.

I've tried running the echo line with and with out the backticks in bash locally. With the backticks, it prints the line perfectly

$ echo "\`\`\`json"
```json

Without the backticks, it seems to expect a continuation of the input string

$ echo "```json"
>

and then it just waits for more text. (I can ctrl+c out of this to exist the command.)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source