如何将降价页面附加到 GitHub 操作工作流程 运行 摘要?

How to attach a markdown page to GitHub Actions workflow run summary?

The GitHub Action "dotnet-tests-report" 将带有测试结果的降价页面附加到 Github 操作工作流 运行 摘要。这真的很好。工作流程完成后,结果会立即变得清晰。一目了然。

它是开源的,但代码很复杂,所以我仍然没有弄清楚如何做到这一点。

我要的是这个:

  1. 运行一些生成降价文件的命令行语句
  2. 运行 一些“发布”这个的代码
  3. 将其附加到 Github 操作中的摘要
  4. 很高兴我公司的每个人都能看到附加到工作流的结果

它使用 GitHub API 到 create a check run

POST https://api.github.com/repos/{owner}/{repo}/check-runs

创建或更新检查运行时,您可以在请求正文中指定 output 参数。操作 dotnet-tests-report 使用 output 参数的文本 属性 报告:

Properties of the output object Description
title (string) Required. The title of the check run.
summary (string) Required. The summary of the check run. This parameter supports Markdown.
text (string) The details of the check run. This parameter supports Markdown.
annotations (array of objects) Adds information from your analysis to specific lines of code. Annotations are visible on GitHub in the Checks and Files changed tab of the pull request. The Checks API limits the number of annotations to a maximum of 50 per API request. To create more than 50 annotations, you have to make multiple requests to the Update a check run endpoint. Each time you update the check run, annotations are appended to the list of annotations that already exist for the check run. For details about how you can view annotations on GitHub, see "About status checks". See the annotations object description for details about how to use this parameter.
images (array of objects) Adds images to the output displayed in the GitHub pull request UI. See the images object description for details.

查看动作代码:https://github.com/zyborg/dotnet-tests-report/blob/237826dc017f02ebf61377af95d1a12f8409a527/action.ps1#L133-L149

$url = "https://api.github.com/repos/$repoFullName/check-runs"
$hdr = @{
    Accept = 'application/vnd.github.antiope-preview+json'
    Authorization = "token $ghToken"
}
$bdy = @{
    name       = $report_name
    head_sha   = $ref
    status     = 'completed'
    conclusion = $conclusion
    output     = @{
        title   = $report_title
        summary = "This run completed at ``$([datetime]::Now)``"
        text    = $reportData
    }
}
Invoke-WebRequest -Headers $hdr $url -Method Post -Body ($bdy | ConvertTo-Json)

在寻找类似的解决方案时,我发现现在有一个可用的操作: https://github.com/LouisBrunner/checks-action,

它实际上使您免于编写请求,甚至允许提供 markdown 文件,如下所示:

- name: Create CheckRun for code Coverage
      uses: LouisBrunner/checks-action@v1.2.0
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        name: Code Coverage
        conclusion: ${{ job.status }}
        output: "{\"summary\":\"Code Coverage\"}"
        output_text_description_file: coveragereport/Summary.md

2022 年 5 月 post“Supercharging GitHub Actions with Job Summaries" from Konrad Pabjan 确实提到:

Actions users have been asking for this type of functionality for a long time.
User-generated content from Actions has previously been limited to logs and annotations. It can be difficult to aggregate and group lots of information.

Annotations are important when it comes to highlighting things, like errors and warnings, and are not suited for rich output, like test summaries or build reports.

To get around these issues, we’ve even seen users manually create check runs using our API using the GITHUB_TOKEN that is provided as part of a run, which leads to decreased productivity.

(就是riQQ's 描述的,不是最优的)

It’s clear that there’s been a feature gap, forcing users to improvise with less than ideal solutions, which is why we’ve developed Job Summaries!

所以:

GitHub Actions Job Summaries allow for custom Markdown content on the run summary generated by each job.

Custom Markdown content can be used for a variety of creative purposes, such as:

  • Aggregating and displaying test results
  • Generating reports
  • Custom output independent of logs

Create summaries

Simply output Markdown content to a new environment variable we’ve introduced called $GITHUB_STEP_SUMMARY.
Any Markdown content added to this file will then be displayed on the Actions run summary page.
That’s it!

steps:
  - name: Adding markdown
    run: echo ‘### Hello world! :rocket:’ >> $GITHUB_STEP_SUMMARY

或者,运行 测试作为 CI 的一部分: