Skip to content

Commit 02168a6

Browse files
committed
Use token when uploading unit test code coverage data to Codecov from workflow
Codecov claims a token is not needed when using the codecov/codecov-action GitHub Actions action in workflows of a public repository: https://github.com/codecov/codecov-action#usage > For public repositories, no token is needed However, experience shows that that step of the workflow is subject to intermittent spurious failures caused by a 404 error during the upload attempt: ``` [2023-10-17T04:37:33.792Z] ['error'] There was an error running the uploader: Error uploading to https://codecov.io: Error: There was an error fetching the storage URL during POST: 404 - {'detail': ErrorDetail(string='Unable to locate build via Github Actions API. Please upload with the Codecov repository upload token to resolve issue.', code='not_found')} ``` It is suggested that this can be avoided by providing the upload token: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954 It should be noted that PRs from forks do not have access to repository secrets, so the recommended approach of using an encrypted repository secret for the token would mean that PRs from forks (the workflow runs for which don't have access to secrets) would still be subject to the same intermittent spurious workflow run failures. The alternative solution is to add the token in plaintext directly in the workflow. The security implications of that approach are described here: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954 > Public repositories that rely on PRs via forks will find that they cannot effectively use Codecov if the token is > stored as a GitHub secret. The scope of the Codecov token is only to confirm that the coverage uploaded comes from a > specific repository, not to pull down source code or make any code changes. > > For this reason, we recommend that teams with public repositories that rely on PRs via forks consider the security > ramifications of making the Codecov token available as opposed to being in a secret. > > A malicious actor would be able to upload incorrect or misleading coverage reports to a specific repository if they > have access to your upload token, but would not be able to pull down source code or make any code changes. We have evaluated the risks of exposing the token and are intentionally choosing to accept the possibility of abuse.
1 parent 6d66474 commit 02168a6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

.github/workflows/test-python-poetry-task.yml

+15
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,23 @@ jobs:
104104
coverage xml \
105105
-o "${{ github.workspace }}/${{ env.COVERAGE_DATA_FILENAME }}"
106106
107+
# A token is used to avoid intermittent spurious job failures caused by rate limiting.
108+
- name: Set up Codecov upload token
109+
run: |
110+
if [[ "${{ github.repository }}" == "arduino/compile-sketches" ]]; then
111+
# In order to avoid uploads of data from forks, only use the token for runs in the parent repo.
112+
# Token is intentionally exposed.
113+
# See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954
114+
CODECOV_TOKEN="0c2a8127-e253-4812-8b83-6dcc586c2bf7"
115+
else
116+
# codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input.
117+
CODECOV_TOKEN=""
118+
fi
119+
echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV"
120+
107121
- name: Upload coverage report to Codecov
108122
uses: codecov/codecov-action@v3
109123
with:
110124
fail_ci_if_error: true
111125
file: ${{ env.COVERAGE_DATA_FILENAME }}
126+
token: ${{ env.CODECOV_TOKEN }}

0 commit comments

Comments
 (0)