[Github Actions] One-liner solution to Get a Multiline Output
echo "PAYLOAD<<EOF"$'\n'"$payload"$'\n'EOF >> "$GITHUB_OUTPUT".
This snippet was kindly shared with me by tjtharrison a while ago and I really like it.
You can go now, you’re welcome. The rest of this post is for my own reference, how I solved an issue using a multiline output.
The single line problem
Alas, I couldn’t find a way to dynamically build a one-line string that would escape "
, escape \n
, have a markdown format "Drift detected: ```<escaped_output_here>```"
compatible with GITHUB_OUTPUT
and work well with toJSON
so it properly rendered in Slack. Sigh. Gotta up my bash game.
I got into this because we have a terraform drift detection in Github. It uses slackapi/slack-github-action
to Slack us about deviations, adding a summary of affected resources inside a ```markdown``` block.
All was well until we got a payload with unescaped quotes:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Drift detected in terraform: \n```module.user["maria@example.com"]\n\nDetails:\nhttps://github.com/org/repo/actions/runs/1234567890```"
}
}
]
}
What I did
- Got a multiline
GITHUB_OUTPUT
value - Replaced ```markdown``` with a Slack block of type
context
withplain_text
inside, to mimic the markdown <code>…