如何使用GitHub动作在PR中添加评论
在github api中,有一个 api 创建问题评论。我想在GitHub Action CI/CD管道中使用它。
我创建工作流程。
name: Workflow
on:
push:
branches:
- main
pull_request:
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run jest coverage and log the result to GitHub summary
run: |
npm run test-coverage >> $GITHUB_STEP_SUMMARY
echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV
echo "" >> $GITHUB_STEP_SUMMARY
- name: Post jest coverage comments
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
SUMMARY_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/$REPO/issues/$PR_NUMBER/comments \
-d '{"body":"# Job Summary\n'$SUMMARY_URL'\n'$UNITTESTCOVERAGE'\n"}'
deploy:
...
GitHub机器人没有显示开玩笑的覆盖结果,而是添加了评论。
Job Summary
...
/home/runner/work/_temp/_runner_file_commands/step_summary_d0****01-f**9-4**7-b**7-f0******dc3f
如何获得实际结果?
更新1
我删除了无关的步骤以清楚地表明它。
响应
更新2
i SET jest -coverage
以文本格式创建覆盖范围报告(以及其他格式,在这里不重要),我想创建评论以显示文本格式报告。
在package.json
中,我添加了npm运行测试覆盖
运行jest
coverage Report。因此,npm运行测试覆盖
将在终端中打印结果。
我使用npm运行测试覆盖>> $ github_step_summary
将结果重定向到 github步骤摘要,因此我可以在 github步骤摘要中看到覆盖范围报告。
我想使用GitHub API来创建评论,以在PR中显示覆盖范围报告。我使用echo“ unittest -coverage = $ github_step_summary”>> $ github_env
将环境变量设置unitTest -coverage
为$ github_step_summary
unittest -coverage
可以获取覆盖范围。
但是,步骤发布开玩笑评论
无法获得覆盖范围报告。 Github API创建的评论代替了报告,显示了文件 /目录路径。
如何将覆盖范围报告添加到GitHub API的评论中?
In GitHub api, there is an api to create an issue comment. I want to use it in the GitHub actions CI/CD pipeline.
I create the workflow.
name: Workflow
on:
push:
branches:
- main
pull_request:
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run jest coverage and log the result to GitHub summary
run: |
npm run test-coverage >> $GITHUB_STEP_SUMMARY
echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV
echo "" >> $GITHUB_STEP_SUMMARY
- name: Post jest coverage comments
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
SUMMARY_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/$REPO/issues/$PR_NUMBER/comments \
-d '{"body":"# Job Summary\n'$SUMMARY_URL'\n'$UNITTESTCOVERAGE'\n"}'
deploy:
...
Instead of showing the jest coverage result, GitHub bot add the comment.
Job Summary
...
/home/runner/work/_temp/_runner_file_commands/step_summary_d0****01-f**9-4**7-b**7-f0******dc3f
How to do get the actual result?
Update 1
I removed unrelated steps to make it clear.
Response
Update 2
I set jest --coverage
to create coverage report in text format (and other formats, not important here), and I want to create a comment to show the text format report.
In package.json
, I added npm run test-coverage
to run Jest
coverage report. Therefore, npm run test-coverage
will print the result in terminal.
I used npm run test-coverage >> $GITHUB_STEP_SUMMARY
to redirect the result to GitHub step summary, so I could see the coverage report in GitHub step summary.
I wanted to use GitHub api to create a comment to show the coverage report in the PR. I used echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV
to set environment variable UNITTESTCOVERAGE
to be $GITHUB_STEP_SUMMARY
, and I wished UNITTESTCOVERAGE
can get the coverage report.
However, the step Post jest coverage comments
could not get the coverage report. Instead of the report, the comment created by GitHub api showed a file / directory path.
How to add the coverage report to a comment by GitHub api?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
环境变量
$ github_step_summary
是文件名。您的代码的作用:
npm运行测试覆盖>> $ github_step_summary
echo“ unittest -coverage = $ github_step_summary”>>> $ github_env
UnitTest -coverage
与github_step_summary
(fileName)相同
echo“”>> $ github_step_summary
,您可能想要的是将文件的内容设置为“步骤摘要env变量”作为新ENV变量:
echo:echo echo“ unittest -coverage = $ (cat $ github_step_summary)”>> $ github_env
The environment variable
$GITHUB_STEP_SUMMARY
is a filename.What your code does:
npm run test-coverage >> $GITHUB_STEP_SUMMARY
echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV
UNITTESTCOVERAGE
to the same asGITHUB_STEP_SUMMARY
(a filename)echo "" >> $GITHUB_STEP_SUMMARY
What you probably want, however, is to put set the contents of the file behind the step summary env variable as the new env variable:
echo "UNITTESTCOVERAGE=$(cat $GITHUB_STEP_SUMMARY)" >> $GITHUB_ENV