【问题标题】:How do I get the output of a specific step in GitHub Actions?如何获取 GitHub Actions 中特定步骤的输出?
【发布时间】:2020-03-30 04:39:24
【问题描述】:

我有这个运行测试的 GitHub Actions 工作流,但现在我正在其中集成 slack 通知。我想获取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 }}

【问题讨论】:

    标签: github elixir github-actions


    【解决方案1】:

    你需要做三件事:

    1. id 添加到您想要输出的步骤中
    2. 使用set-output 命令创建输出
    3. Use the id 和输出名称在另一个步骤中获取输出,然后 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 了解输出名称说明

    【讨论】:

    • 这对我有用,有点。我的输出以每行的前导空格被截断而告终,每行莫名其妙地被单引号包围。这是否发生在其他人身上?这是非常奇怪的行为,并且没有很好的记录。我正在做同样的事情;尝试捕获命令的输出并将其发送到 Slack,尽管我使用的是pullreminders/slack-action
    • 嗯,这导致测试步骤在实际失败时注册为成功
    【解决方案2】:

    当前accepted answer 的问题在于,由于测试执行结果被echo 命令屏蔽,因此该步骤的结果将始终为成功

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

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

    【讨论】:

    • 你测试过这个吗? docs 表示对于bash 和非Windows 机器上的默认shell,shell 使用set -eo pipefail 运行。 -e 没有区别吗?我还没有测试过,但我也没有看到使用-e 将允许shell 在命令失败时继续执行脚本的情况。我可以稍后测试以确认
    • 我运行这个没有问题,是的
    【解决方案3】:

    我只是想添加@smac89 的解决方案很有帮助,但对我来说不太奏效。我正在使用不同的 Slack 操作 (pullreminders/slack-action) 来构建更具体的内容。我发现我在每个换行符所在的位置都得到了单引号,并且我在每行上的前导空格也被截断了。在阅读https://github.com/actions/toolkit/issues/403 并玩弄之后,我发现在我的情况下,我需要在输出中实际转义换行符(文字\n),所以我用\\n 替换了\n 字符。然后,我将常规空格字符替换为 Unicode 'En 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"
    

    Slack 通知步骤:

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

    【讨论】:

      【解决方案4】:

      我创建了一个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 }}
      

      【讨论】:

        猜你喜欢
        • 2020-11-28
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2020-01-16
        • 2023-02-21
        • 1970-01-01
        • 2020-01-11
        • 2020-05-12
        相关资源
        最近更新 更多