73

I have this GitHub Actions workflow which runs tests, but now I am integrating slack notification in it. I want to get the output of the Run tests step and send it as a message in the slack step.

  - name: Run tests
    run: |
      mix compile --warnings-as-errors
      mix format --check-formatted
      mix ecto.create
      mix ecto.migrate
      mix test
    env:
      MIX_ENV: test
      PGHOST: localhost
      PGUSER: postgres

  - name: Slack Notification
    uses: rtCamp/action-slack-notify@master
    env:
      SLACK_MESSAGE: Run tests output
      SLACK_TITLE: CI Test Suite
      SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

4 Answers 4

116

You need to do 3 things:

  1. Add an id to the step you want the output from
  2. Create the outputs using the GITHUB_OUTPUT environment variable
  3. Use the id and the output name in another step to get the outputs and then join them into one message for slack
- name: Run tests
  run: |
    echo "mix-compile--warnings-as-errors=$(mix compile --warnings-as-errors)\n" >> $GITHUB_OUTPUT
    echo "mix-format--check-formatted=$(mix format --check-formatted)\n" >> $GITHUB_OUTPUT
    echo "mix-ecto_create=$(mix ecto.create)\n" >> $GITHUB_OUTPUT
    echo "mix-ecto_migrate=$(mix ecto.migrate)\n" >> $GITHUB_OUTPUT
    echo "mix-test=$(mix test)\n" >> $GITHUB_OUTPUT
  id: run_tests
  env:
    MIX_ENV: test
    PGHOST: localhost
    PGUSER: postgres

- name: Slack Notification
  uses: rtCamp/action-slack-notify@v2
  env:
    SLACK_MESSAGE: ${{join(steps.run_tests.outputs.*, '\n')}}
    SLACK_TITLE: CI Test Suite
    SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

See Metadata Syntax for outputs name description

7
  • 1
    This worked for me, sorta. My output ends up with each line's leading whitespace truncated and each line is inexplicably surrounded by single-quotes. Is this happening for anyone else? This is very odd behavior and not well-documented. I'm doing the exact same thing; trying to capture output from a command and send it to Slack, though I'm using pullreminders/slack-action. Sep 30, 2020 at 3:36
  • 4
    GitHub is deprecating the set-output command: github.blog/changelog/… Oct 14, 2022 at 10:37
  • 2
    @RafaelMembrives thanks for the tip. The answer has been updated to use the new environment variable
    – smac89
    Oct 14, 2022 at 16:15
  • 1
    Is it necessary to do anything special for whitespace in VALUE?
    – Max Murphy
    Oct 17, 2022 at 20:47
  • 3
    Please be aware that for Powershell, you should reference the environment variable "GITHUB_OUTPUT" like this $env:GITHUB_OUTPUT, otherwise the next step that evaluates the output variable will fail.
    – Antebios
    Oct 18, 2022 at 15:54
21

The problem with the current accepted answer is that the result for the step will always be success since the test execution result is being masked by the echo command.

This modification to the last line should work in preserving the original exit status:

mix test 2>&1 | tee test.log
result_code=${PIPESTATUS[0]}
echo "::set-output name=mix-test::$(cat test.log)"
exit $result_code
4
  • 2
    Did you test this? The docs say that for bash and for the default shell on non-windows machines, the shell is run with set -eo pipefail. Doesn't the -e make a difference? I haven't tested, but I've also not seen a situation where using -e will allow the shell to continue executing the script when a command fails. I can test later to confirm
    – smac89
    Jun 10, 2021 at 15:06
  • I have this running with no problems, yes
    – juan
    Jun 15, 2021 at 12:35
  • This also worked for me when the accepted answer did not. Jun 7, 2022 at 11:54
  • 1
    Be careful with this solution: if test.log contains multiple lines, the output mix-test will only receive the first line of the file. See: Set-output Truncates Multiline Strings #26288
    – Ludal
    Aug 11, 2022 at 13:18
11

I made an action with the same interface as run that stores stdout and stderr in output variables to maybe simplify some cases like this:

- name: Run tests
  uses: mathiasvr/command-output@v1
  id: tests
  with:
    run: |
      mix compile --warnings-as-errors
      mix format --check-formatted
      mix ecto.create
      mix ecto.migrate
      mix test

- name: Slack Notification
  uses: rtCamp/action-slack-notify@master
  env:
    SLACK_MESSAGE: ${{ steps.tests.outputs.stdout }}         
    SLACK_TITLE: CI Test Suite
    SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
3

I just wanted to add @smac89's solution was helpful but didn't quite work for me. I'm using a different Slack action (pullreminders/slack-action) to build more specific content. I found that I was getting single-quotes where each newline was, and my leading spaces on each line were also being truncated. After reading https://github.com/actions/toolkit/issues/403 and playing around, I found that in my case, I needed newlines to actually be escaped in the output (a literal \n), so I replaced \n characters with \\n. Then, I replaced regular space characters with a Unicode 'En Space' character.

Here's what worked:

Bash Run Step:

        Tools/get-changed-fields.sh src/objects origin/${{ env.DIFF_BRANCH }} > changed-fields.out
        output="$(cat changed-fields.out)"
        output="${output//$'\n'/\\n}"
        output="${output// / }"     # replace regular space with 'En Space'
        echo "::set-output name=changed-fields-output::$output"

Slack Notification Step:

    - name: Changed Fields Slack Notification
      if: ${{ success() && steps.summarize-changed-fields.outputs.changed-fields-output != '' && steps.changed-fields-cache.outputs.cache-hit != 'true' }}
      env:
        SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
      uses: pullreminders/slack-action@master
      with:
        args: '{\"channel\":\"${{ env.SUCCESS_SLACK_CHANNEL }}\",\"attachments\":[{\"color\":\"#36a64f\",\"title\":\"Changed Fields Report:\",\"author_name\":\"${{ github.workflow }} #${{ github.run_number }}: ${{ env.BRANCH }} -> ${{ env.TARGET_ORG }} (by: ${{ github.actor }})\",\"author_link\":\"${{ github.server_url }}/${{ github.repository }}/runs/${{ github.run_id }}\",\"text\":\"```\n${{ steps.summarize-changed-fields.outputs.changed-fields-output }}\n```\"}]}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.