|
| 1 | +name: Upload Coverage Report |
| 2 | + |
| 3 | +on: |
| 4 | + # This workflow is triggered after every successfull execution |
| 5 | + # of `Generate Coverage Report` workflow. |
| 6 | + workflow_run: |
| 7 | + workflows: ["Generate Coverage Report"] |
| 8 | + types: |
| 9 | + - completed |
| 10 | + |
| 11 | +permissions: |
| 12 | + actions: write |
| 13 | + contents: write |
| 14 | + issues: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + coverage: |
| 19 | + name: Upload Coverage Report |
| 20 | + environment: coverage |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: "Download existing coverage report" |
| 24 | + id: prepare_report |
| 25 | + uses: actions/github-script@v7 |
| 26 | + with: |
| 27 | + script: | |
| 28 | + var fs = require('fs'); |
| 29 | +
|
| 30 | + // List artifacts of the workflow run that triggered this workflow |
| 31 | + var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 32 | + owner: context.repo.owner, |
| 33 | + repo: context.repo.repo, |
| 34 | + run_id: context.payload.workflow_run.id, |
| 35 | + }); |
| 36 | +
|
| 37 | + let codecovReport = artifacts.data.artifacts.filter((artifact) => { |
| 38 | + return artifact.name == "codecov_report"; |
| 39 | + }); |
| 40 | +
|
| 41 | + if (codecovReport.length != 1) { |
| 42 | + throw new Error("Unexpected number of {codecov_report} artifacts: " + codecovReport.length); |
| 43 | + } |
| 44 | +
|
| 45 | + var download = await github.rest.actions.downloadArtifact({ |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + artifact_id: codecovReport[0].id, |
| 49 | + archive_format: 'zip', |
| 50 | + }); |
| 51 | + fs.writeFileSync('codecov_report.zip', Buffer.from(download.data)); |
| 52 | +
|
| 53 | + let prNumber = artifacts.data.artifacts.filter((artifact) => { |
| 54 | + return artifact.name == "pr_number"; |
| 55 | + }); |
| 56 | +
|
| 57 | + if (prNumber.length != 1) { |
| 58 | + throw new Error("Unexpected number of {pr_number} artifacts: " + prNumber.length); |
| 59 | + } |
| 60 | +
|
| 61 | + var download = await github.rest.actions.downloadArtifact({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + artifact_id: prNumber[0].id, |
| 65 | + archive_format: 'zip', |
| 66 | + }); |
| 67 | + fs.writeFileSync('pr_number.zip', Buffer.from(download.data)); |
| 68 | +
|
| 69 | + let commitSha = artifacts.data.artifacts.filter((artifact) => { |
| 70 | + return artifact.name == "commit_sha"; |
| 71 | + }); |
| 72 | +
|
| 73 | + if (commitSha.length != 1) { |
| 74 | + throw new Error("Unexpected number of {commit_sha} artifacts: " + commitSha.length); |
| 75 | + } |
| 76 | +
|
| 77 | + var download = await github.rest.actions.downloadArtifact({ |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + artifact_id: commitSha[0].id, |
| 81 | + archive_format: 'zip', |
| 82 | + }); |
| 83 | + fs.writeFileSync('commit_sha.zip', Buffer.from(download.data)); |
| 84 | +
|
| 85 | + - id: parse_previous_artifacts |
| 86 | + run: | |
| 87 | + unzip codecov_report.zip |
| 88 | + unzip pr_number.zip |
| 89 | + unzip commit_sha.zip |
| 90 | +
|
| 91 | + echo "Detected PR is: $(<pr_number.txt)" |
| 92 | + echo "Detected commit_sha is: $(<commit_sha.txt)" |
| 93 | +
|
| 94 | + # Make the params available as step output |
| 95 | + echo "override_pr=$(<pr_number.txt)" >> "$GITHUB_OUTPUT" |
| 96 | + echo "override_commit=$(<commit_sha.txt)" >> "$GITHUB_OUTPUT" |
| 97 | +
|
| 98 | + - name: Checkout repository |
| 99 | + uses: actions/checkout@v4 |
| 100 | + with: |
| 101 | + ref: ${{ steps.parse_previous_artifacts.outputs.override_commit || '' }} |
| 102 | + path: repo_root |
| 103 | + |
| 104 | + - name: Upload coverage to Codecov |
| 105 | + uses: codecov/codecov-action@v5 |
| 106 | + with: |
| 107 | + verbose: true |
| 108 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 109 | + files: ${{ github.workspace }}/codecov.json |
| 110 | + fail_ci_if_error: true |
| 111 | + # Manual overrides for these parameters are needed because automatic detection |
| 112 | + # in codecov-action does not work for non-`pull_request` workflows. |
| 113 | + # In `main` branch push, these default to empty strings since we want to run |
| 114 | + # the analysis on HEAD. |
| 115 | + override_commit: ${{ steps.parse_previous_artifacts.outputs.override_commit || '' }} |
| 116 | + override_pr: ${{ steps.parse_previous_artifacts.outputs.override_pr || '' }} |
| 117 | + working-directory: ${{ github.workspace }}/repo_root |
| 118 | + # Location where coverage report files are searched for |
| 119 | + directory: ${{ github.workspace }} |
0 commit comments