-
Notifications
You must be signed in to change notification settings - Fork 807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Github actions code coverage CI job #1762
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: Generate Code Coverage | ||
on: [pull_request] | ||
|
||
jobs: | ||
cover-base: | ||
name: Generate Base Coverage | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout base | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.event.pull_request.base.ref }} | ||
|
||
- name: Set up go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '^1.20.2' | ||
|
||
- name: Generate report | ||
run: | | ||
go test -coverprofile base-coverage.tmp ./cmd/... ./pkg/... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any chance we can get the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe so, as we would have to host it somewhere. As far as I can tell GitHub does not host artifacts for you. I do have an idea to show the code coverage output on the prow test runs, but didn't think it was appropriate for this PR (and might not be possible? needs more investigation) |
||
cat base-coverage.tmp | grep -v "mock_" > base-coverage.out | ||
|
||
- name: Upload report | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: base-coverage | ||
path: base-coverage.out | ||
|
||
cover-pr: | ||
name: Generate PR Coverage | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout PR | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '^1.20.2' | ||
|
||
- name: Generate report | ||
run: | | ||
go test -coverprofile pr-coverage.tmp ./cmd/... ./pkg/... | ||
cat pr-coverage.tmp | grep -v "mock_" > pr-coverage.out | ||
|
||
- name: Upload report | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: pr-coverage | ||
path: pr-coverage.out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
name: Output Code Coverage | ||
on: | ||
workflow_run: | ||
workflows: [Generate Code Coverage] | ||
types: [completed] | ||
|
||
jobs: | ||
output-code-coverage: | ||
name: Output Code Coverage | ||
runs-on: ubuntu-latest | ||
if: ${{ github.event.workflow_run.conclusion == 'success' }} | ||
steps: | ||
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow | ||
- name: 'Download reports' | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
let fs = require('fs'); | ||
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
run_id: context.payload.workflow_run.id, | ||
}); | ||
for (const artifact of allArtifacts.data.artifacts) { | ||
let download = await github.rest.actions.downloadArtifact({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
artifact_id: artifact.id, | ||
archive_format: 'zip', | ||
}); | ||
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${artifact.id}.zip`, Buffer.from(download.data)); | ||
} | ||
|
||
- name: 'Determine source PR' | ||
uses: potiuk/get-workflow-origin@v1_1 | ||
id: source-run-info | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
sourceRunId: ${{ github.event.workflow_run.id }} | ||
|
||
- name: Set up go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '^1.20.2' | ||
|
||
- name: Install coverage tool | ||
run: go install k8s.io/test-infra/robots/coverage@latest | ||
|
||
- name: Generate comment | ||
id: generate-comment | ||
run: | | ||
unzip \*.zip | ||
echo 'comment<<EOF' >> $GITHUB_OUTPUT | ||
echo '<!-- pr-coverage -->' >> $GITHUB_OUTPUT | ||
echo '## Code Coverage Diff' >> $GITHUB_OUTPUT | ||
COVERAGE_DIFF=$(coverage diff base-coverage.out pr-coverage.out | sed -e '1,5d') | ||
if [[ -n "${COVERAGE_DIFF}" ]]; then | ||
printf -- "%s\n" "${COVERAGE_DIFF}" >> $GITHUB_OUTPUT | ||
else | ||
echo 'This PR does not change the code coverage' >> $GITHUB_OUTPUT | ||
fi | ||
echo 'EOF' >> $GITHUB_OUTPUT | ||
|
||
- name: Create or update comment | ||
uses: edumserrano/find-create-or-update-comment@v1 | ||
with: | ||
issue-number: ${{ steps.source-run-info.outputs.pullRequestNumber }} | ||
body-includes: '<!-- pr-coverage -->' | ||
comment-author: 'github-actions[bot]' | ||
body: ${{ steps.generate-comment.outputs.comment }} | ||
edit-mode: replace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe overkill, but can we avoid hardcoding with an env var? https://stackoverflow.com/questions/74021760/how-to-use-environment-variables-on-github-actions-without-hard-coding-them-is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you're suggesting? Can you clarify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently hardcode the go version for other workflows as well but its a good idea to grab it from go mod using
go-version-file
: https://github.com/actions/setup-go#getting-go-version-from-the-gomod-file.