Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to escape new line character in message? #106

Closed
nuhkoca opened this issue Aug 7, 2021 · 16 comments
Closed

How to escape new line character in message? #106

nuhkoca opened this issue Aug 7, 2021 · 16 comments

Comments

@nuhkoca
Copy link
Contributor

nuhkoca commented Aug 7, 2021

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

@nuhkoca
Copy link
Contributor Author

nuhkoca commented Aug 9, 2021

@mrrobot47 cc

@mym0404
Copy link

mym0404 commented Nov 17, 2021

I want to know how to this too

@nuhkoca
Copy link
Contributor Author

nuhkoca commented Nov 17, 2021

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 nuhkoca closed this as completed Nov 17, 2021
@choyzer
Copy link

choyzer commented Feb 9, 2022

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"

@nuhkoca
Copy link
Contributor Author

nuhkoca commented Feb 9, 2022

@choyzer thanks for warning, I updated it!

@Julian88Tex
Copy link

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

@nuhkoca
Copy link
Contributor Author

nuhkoca commented Nov 2, 2022

@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

@Julian88Tex
Copy link

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?

@Julian88Tex
Copy link

@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 }}`

@nuhkoca
Copy link
Contributor Author

nuhkoca commented Nov 2, 2022

@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 🙂

@Julian88Tex
Copy link

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

@nr-pcfruit
Copy link

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. ☺️

@Caesarsage
Copy link

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.

@yoavniran
Copy link

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

@NikolozCh
Copy link

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

@ashmenon
Copy link

ashmenon commented Aug 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants