获取 github 操作中特定步骤的输出

scr*_*ipt 22 github elixir github-actions

我有这个运行测试的 GitHub 操作文件,但现在我正在其中集成松弛通知。我想获取该Run tests步骤的输出并将其作为消息发送到 slack 步骤

  - 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 }}
Run Code Online (Sandbox Code Playgroud)

任何帮助都感激不尽。谢谢

sma*_*c89 38

你需要做3件事:

  1. 将一个添加id到您想要输出的步骤
  2. 使用set-output命令创建输出
  3. 在另一个步骤中使用 id和输出名称来获取输出,然后它们连接到一条消息中以用于 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 }}
Run Code Online (Sandbox Code Playgroud)

有关输出名称说明,请参阅元数据语法

  • 请注意,对于 Powershell,您应该像 `$env:GITHUB_OUTPUT` 一样引用环境变量“GITHUB_OUTPUT”,否则下一步计算输出变量将失败。 (4认同)

小智 21

我使用与存储和输出变量相同的接口进行了操作,以简化某些情况,如下所示:runstdoutstderr

- 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 }}
Run Code Online (Sandbox Code Playgroud)


jua*_*uan 5

当前接受的答案的问题是该步骤的结果将始终是成功的,因为测试执行结果被屏蔽,但echo命令被屏蔽。

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

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

  • [::set-output 已弃用](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/) (3认同)
  • 你测试过这个吗?[文档](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#exit-codes-and-error-action-preference) 说对于 `bash` 和对于非 Windows 计算机上的默认 shell,该 shell 使用“set -eo pipelinefail”运行。“-e”没有区别吗?我没有测试过,但我也没有看到使用“-e”将允许 shell 在命令失败时继续执行脚本的情况。我可以稍后测试来确认 (2认同)