Skip to content
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

ci: Fix coverage uploads in separate workflow #3485

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,19 @@ jobs:
echo "Storing commit SHA ${{ github.event.pull_request.head.sha }}"
echo "${{ github.event.pull_request.head.sha }}" > commit_sha.txt

# Workaround for https://github.com/orgs/community/discussions/25220
# Triggered sub-workflow is not able to detect the original commit/PR which is available
# in this workflow.
- name: Store PR number in artifacts
uses: actions/upload-artifact@v4
with:
name: pr_number
path: pr_number.txt

- name: Store commit SHA in artifacts
uses: actions/upload-artifact@v4
with:
name: commit_sha
path: commit_sha.txt

# This stores the coverage report in artifacts. The actual upload to Codecov
# is executed by a different workflow `upload_coverage.yml`. The reason for this
# split is because `on.pull_request` workflows don't have access to secrets.
# This stores the coverage report and metadata in artifacts.
# The actual upload to Codecov is executed by a different workflow `upload_coverage.yml`.
# The reason for this split is because `on.pull_request` workflows don't have access to secrets.
- name: Store coverage report in artifacts
uses: actions/upload-artifact@v4
with:
name: codecov_report
path: codecov_report.json
path: |
cobertura.xml
pr_number.txt
commit_sha.txt
if-no-files-found: error

- run: |
echo "The coverage report was stored in Github artifacts."
echo "It will be uploaded to Codecov using `upload_coverage.yml` workflow shortly."
echo 'The coverage report was stored in Github artifacts.'
echo 'It will be uploaded to Codecov using `upload_coverage.yml` workflow shortly.'
50 changes: 8 additions & 42 deletions .github/workflows/upload_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
coverage:
name: Upload coverage report
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
steps:
- name: 'Fetch coverage report from artifacts'
id: prepare_report
Expand All @@ -24,9 +25,9 @@ jobs:

// List artifacts of the workflow run that triggered this workflow
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});

let codecovReport = artifacts.data.artifacts.filter((artifact) => {
Expand All @@ -38,50 +39,16 @@ jobs:
}

var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: codecovReport[0].id,
archive_format: 'zip',
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: codecovReport[0].id,
archive_format: 'zip',
});
fs.writeFileSync('codecov_report.zip', Buffer.from(download.data));

let prNumber = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr_number";
});

if (prNumber.length != 1) {
throw new Error("Unexpected number of {pr_number} artifacts: " + prNumber.length);
}

var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: prNumber[0].id,
archive_format: 'zip',
});
fs.writeFileSync('pr_number.zip', Buffer.from(download.data));

let commitSha = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "commit_sha";
});

if (commitSha.length != 1) {
throw new Error("Unexpected number of {commit_sha} artifacts: " + commitSha.length);
}

var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: commitSha[0].id,
archive_format: 'zip',
});
fs.writeFileSync('commit_sha.zip', Buffer.from(download.data));

- id: parse_previous_artifacts
run: |
unzip codecov_report.zip
unzip pr_number.zip
unzip commit_sha.zip

echo "Detected PR is: $(<pr_number.txt)"
echo "Detected commit_sha is: $(<commit_sha.txt)"
Expand All @@ -100,7 +67,6 @@ jobs:
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
files: ${{ github.workspace }}/codecov_report.json
fail_ci_if_error: true
# Manual overrides for these parameters are needed because automatic detection
# in codecov-action does not work for non-`pull_request` workflows.
Expand Down
Loading