Get output of a specific step in github actions

script picture script · Dec 5, 2019 · Viewed 7.7k times · Source

I have this file of GitHub action 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 }}

Any help will be much appreciated. Thanks

Answer

smac89 picture smac89 · Dec 5, 2019

You need to do 3 things:

  1. Add an id to the step you want the output from
  2. Create the outputs using the set-output command
  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 "::set-output name=mix-compile--warnings-as-errors::$(mix compile --warnings-as-errors)\n"
    echo "::set-output name=mix-format--check-formatted::$(mix format --check-formatted)\n"
    echo "::set-output name=mix-ecto_create::$(mix ecto.create)\n"
    echo "::set-output name=mix-ecto_migrate::$(mix ecto.migrate)\n"
    echo "::set-output name=mix-test::$(mix test)\n"
  id: run_tests
  env:
    MIX_ENV: test
    PGHOST: localhost
    PGUSER: postgres

- name: Slack Notification
  uses: rtCamp/action-slack-notify@master
  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