如何获取 GitHub 操作中特定步骤的输出?

How do I get the output of a specific step in GitHub Actions?

我有这个运行测试的 GitHub Actions 工作流,但现在我在其中集成了松弛通知。我想获取 Run tests 步骤的输出并在松弛步骤中将其作为消息发送。

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

你需要做三件事:

  1. id 添加到您想要输出的步骤
  2. 使用 set-output 命令创建输出
  3. Use the id and the output name in another step to get the outputs and then join 将它们合并为一条消息,以便 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@v2
  env:
    SLACK_MESSAGE: ${{join(steps.run_tests.outputs.*, '\n')}}
    SLACK_TITLE: CI Test Suite
    SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

有关输出名称描述,请参阅 Metadata Syntax

我只是想补充一下@smac89 的解决方案很有帮助,但对我来说不太管用。我正在使用不同的 Slack 操作 (pullreminders/slack-action) 来构建更具体的内容。我发现我在每个换行符所在的位置得到 single-quotes,并且每行的前导 space 也被截断了。在阅读 https://github.com/actions/toolkit/issues/403 并四处游玩之后,我发现在我的情况下,我需要在输出中实际转义换行符(文字 \n),所以我将 \n 字符替换为 \n。然后,我用 Unicode 'En Space' 字符替换了常规 space 字符。

这是有效的方法:

Bash 运行 步骤:

        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"

松弛通知步骤:

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

当前 的问题是该步骤的结果将始终是 成功 因为测试执行结果被 echo命令。

对最后一行的修改应该可以保留原始退出状态:

mix test 2>&1 | tee test.log
result_code=${PIPESTATUS[0]}
echo "::set-output name=mix-test::$(cat test.log)"
exit $result_code

我制作了一个 action,其接口与 run 相同,将 stdoutstderr 存储在输出变量中,以简化某些情况,例如:

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