Get output of a specific step in github actions

Deadly 提交于 2020-12-30 08:46:36

问题


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


回答1:


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




回答2:


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```\"}]}'


来源:https://stackoverflow.com/questions/59191913/get-output-of-a-specific-step-in-github-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!