rtCamp/action-slack-notify

How to escape new line character in message?

Closed this issue Β· 16 comments

I have a well-detailed Slack message for our releases but without a break, message and title look too adjacent to each other. That's the reason, I want to have a break between title and message but seems you always trim whitespaces in the message field, am I right? I tried below but none of them worked.

"%0A*Version:* version
*Test Groups:* test-groups
*Changelog:* changelog"
"\\n*Version:* version
*Test Groups:* test-groups
*Changelog:* changelog"
"\n*Version:* version
*Test Groups:* test-groups
*Changelog:* changelog"

For example, **(asteriks) character works to make text bold. Please find screenshot for your reference.

Screenshot 2021-08-08 at 00 36 29

I want to know how to this too

If anyone is looking for a workaround or solution whatever you call my response, I solved this issue by below steps in my Github Actions pipeline. Feel free to edit SLACK_MESSAGE as per your need.

- name: Prepare Slack Message
  if: ${{ success() }}
  id: slack-message-creator
  run: |
    SLACK_MESSAGE="*Version:* 12345
    *Testing Groups:* my_test_group
    *Change Log:* my_change_log"

    echo "::set-output name=slack-message::${SLACK_MESSAGE//$'\n'/'%0A'}"

- name: Notify Release Channel
  if: ${{ success() }}
  uses: rtCamp/action-slack-notify@v2.1.3
  env:
    SLACK_USERNAME: '<your_username>'
    SLACK_WEBHOOK: <your_slack_hook>
    SLACK_ICON: <your_icon>
    SLACK_COLOR: ${{ job.status }}
    SLACK_TITLE: <your_title>
    SLACK_MESSAGE: '${{ steps.slack-message-creator.outputs.slack-message }}'
    MSG_MINIMAL: true

If someone is using @nuhkoca script, he missing " at the end of SLACK_MESSAGE
Should be
SLACK_MESSAGE="Version: 12345
Testing Groups: my_test_group
Change Log: my_change_log"

@choyzer thanks for warning, I updated it!

@nuhkoca Hm, I think I'm missing something. How are you getting SLACK_MESSAGE to be considered that entire multi-line?

@Julian88Tex If I understand you correctly, SLACK_MESSAGE includes multilines(\n) and asteriks(*) but to let Slack API understand it, we basically escape with ${SLACK_MESSAGE//$'\n'/'%0A'}. Then Slack will print them multilined and in bold

If anyone is looking for a workaround or solution whatever you call my response, I solved this issue by below steps in my Github Actions pipeline. Feel free to edit SLACK_MESSAGE as per your need.

- name: Prepare Slack Message
  if: ${{ success() }}
  id: slack-message-creator
  run: |
    SLACK_MESSAGE="*Version:* 12345
    *Testing Groups:* my_test_group
    *Change Log:* my_change_log"

    echo "::set-output name=slack-message::${SLACK_MESSAGE//$'\n'/'%0A'}"

- name: Notify Release Channel
  if: ${{ success() }}
  uses: rtCamp/action-slack-notify@v2.1.3
  env:
    SLACK_USERNAME: '<your_username>'
    SLACK_WEBHOOK: <your_slack_hook>
    SLACK_ICON: <your_icon>
    SLACK_COLOR: ${{ job.status }}
    SLACK_TITLE: <your_title>
    SLACK_MESSAGE: '${{ steps.slack-message-creator.outputs.slack-message }}'
    MSG_MINIMAL: true

@nuhkoca ah, so is there \n in this example that I'm just not seeing? Like SLACK_MESSAGE="*Version:* 12345 \n?

@nuhkoca actually nevermind I figured out what was going on. Apparently having an inline code format character was messing it up.

Works:

*Username:* ${{ needs.org_setup.outputs.username }}

Doesn't Work:

*Username:* `${{ needs.org_setup.outputs.username }}`

@Julian88Tex glad you fixed. But here

SLACK_MESSAGE="*Version:* 12345
*Testing Groups:* my_test_group
*Change Log:* my_change_log"

every line is a new line right? So it corresponds to \n in LF πŸ™‚

@nuhkoca thanks yeah it just wasn't interpreting it in that way with the ` characters. I guess just the way bash operates.

The set-output command will be deprecated soon, therefore you have to use $GITHUB_OUTPUT. However, using the new method the slack message does not translate the %0A to a new line. (see image "not working" ).

In order to solve it, I use the syntax for Multiline strings
based on this StackOverflow question

So, now my code looks something like this:

      - name: Summary
        id: summary
        run: |
          slack_message="*  Total tests:* 1
          *  Passes:* 2
          *  Failed:* 3
          *  Gist report: 4"

          echo 'slack-message<<EOF' >> $GITHUB_OUTPUT
          echo "$slack_message" >> $GITHUB_OUTPUT
          echo 'EOF' >> $GITHUB_OUTPUT

      - name: Slack Notification
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: *******
          SLACK_USERNAME: Demo - Integration Test on yada yada
          SLACK_TITLE: "Summary"
          SLACK_MESSAGE: ${{ steps.summary.outputs.slack-message }}
          SLACK_ICON: https://avatars.githubusercontent.com/u/95714513?s=400&u=a00afc56cfc57ef9aa0fab1540f70a8507df3e96
          SLACK_COLOR: ${{job.status}}

Not working:
image

Working:
image

Hope this might help anyone out there. ☺️

The set-output command will be deprecated soon, therefore you have to use $GITHUB_OUTPUT. However, using the new method the slack message does not translate the %0A to a new line. (see image "not working" ).

In order to solve it, I use the syntax for Multiline strings based on this StackOverflow question

So, now my code looks something like this:

      - name: Summary
        id: summary
        run: |
          slack_message="*  Total tests:* 1
          *  Passes:* 2
          *  Failed:* 3
          *  Gist report: 4"

          echo 'slack-message<<EOF' >> $GITHUB_OUTPUT
          echo "$slack_message" >> $GITHUB_OUTPUT
          echo 'EOF' >> $GITHUB_OUTPUT

      - name: Slack Notification
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: *******
          SLACK_USERNAME: Demo - Integration Test on yada yada
          SLACK_TITLE: "Summary"
          SLACK_MESSAGE: ${{ steps.summary.outputs.slack-message }}
          SLACK_ICON: https://avatars.githubusercontent.com/u/95714513?s=400&u=a00afc56cfc57ef9aa0fab1540f70a8507df3e96
          SLACK_COLOR: ${{job.status}}

Not working: image

Working: image

Hope this might help anyone out there. ☺️

This didn't work for be. The bolding.

hi @nuhkoca thanks for the example, unfortunately Im still not seeing multi-line output in slack.

this is what im doing (shortened...):

    - name: Get log between tags
      id: release-log
      shell: bash
      run: |
        ...
        gitcommits=$(git log previousTag..HEAD --pretty=format:"%h %s - %an %B %n" -- ${{ inputs.logRoot }}/) 
        echo "### Git commits since Last Tag πŸ“’" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        while IFS= read -r line; do
         echo $line >> $GITHUB_STEP_SUMMARY
        done <<< "$gitcommits"
        echo "RELEASE-LOG=${gitcommits//$'\n'/'%0A'}" >> "$GITHUB_OUTPUT"


      - name: Slack notification for successful deployment
        if: ${{ always() && steps.s3-deploy.outcome == 'success' }}
        uses: rtCamp/action-slack-notify@v2.2.0
        env:
         ...
          SLACK_MESSAGE: "Log: ${{ steps.release-log.outputs.RELEASE-LOG }}"

Im printing out the job summary which is coming out well inside the Github interface.
however, the message in slack is one big blob with the lines separated by: "%0A %0A%0A" characters

Hi, whoever encounters this question in the future. I am talking to you. So, new environment variable is available ENABLE_ESCAPES set it to true and use \n and \t freely.

Source: #164

For anyone who might need it, the way I managed to escape line breaks is using @nuhkoca's code but instead replacing \n with.... \n.

I assume this works by replacing the newline character with the literal string \n, but it looks weird af.

- name: Format Commit Message
   id: clean_message
   run: |
      COMMIT_MESSAGE="${{ github.event.head_commit.message }}"
      echo "clean-output=${COMMIT_MESSAGE//$'\n'/'\n'}" >> $GITHUB_OUTPUT