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?
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 as GITHUB_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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With