Skip to content

CI add codecov to GitHub Action workflow - #31941

Merged
ogrisel merged 10 commits into
scikit-learn:mainfrom
StefanieSenger:gha_coverage
Aug 28, 2025
Merged

ogrisel merged 10 commits into
scikit-learn:mainfrom
StefanieSenger:gha_coverage

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Aug 13, 2025 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Follow up on #31832

What does this implement/fix? Explain your changes.

This PR adds steps to the github actions workflow (unit-tests.yml) that merge and upload codecov coverage reports.

Thanks for your support @lesteve!

@github-actions

github-actions Bot commented Aug 13, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 71a8b64. Link to the linter CI: here

@StefanieSenger

Copy link
Copy Markdown
Member Author

This will not fail, but no coverage report will be created, because we are lacking a token. It needs to be collected from codecov and added into the project's secrets, like this guide explains: https://docs.codecov.com/docs/adding-the-codecov-token

When I try to sign in to codecov, I am asked to confirm to an oauth which among others lists "act on your behalf". Now, I am unsure. Should I confirm that, or would you rather add the token, @lesteve?

@lesteve

lesteve commented Aug 13, 2025

Copy link
Copy Markdown
Member

I just added the Codecov token in the GitHub secrets.

Let's see what happens with the CI. My guess is that the codecov upload will work because no token is needed on a public repo PR, see doc for more details.

It's only when this PR is merged that a Codecov token will be needed for the Codecov upload of the main branch.

@lesteve

lesteve commented Aug 13, 2025 •

Copy link
Copy Markdown
Member

One slightly weird thing I noticed in the log is that it uploads multiple coverage.xml the one in the root folder and the one in the TEST_DIR

info - 2025-08-13 12:59:23,247 -- Found 2 coverage files to report
info - 2025-08-13 12:59:23,247 -- > /home/runner/work/scikit-learn/scikit-learn/tmp_folder/coverage.xml
info - 2025-08-13 12:59:23,247 -- > /home/runner/work/scikit-learn/scikit-learn/coverage.xml

Having a quick look at the codecov-action README, maybe we want to use files: ./coverage.xml (i.e. additional ./) and/or disable_search: true?

@lesteve

lesteve commented Aug 13, 2025 •

Copy link
Copy Markdown
Member

I pushed a possible fix for the pylatest_pip_openblas_pandas failure. We'll see what the CI says about it ...

What's special about this build is that we are setting PIP_BUILD_ISOLATION: 'true' in azure-pipelines.yml and we build in non-editable mode. I guess in this case you need to be out of the scikit-learn root folder to be able to do python -c 'import sklearn'.

@StefanieSenger

Copy link
Copy Markdown
Member Author

Having a quick look at the codecov-action README, maybe we want to use files: ./coverage.xml (i.e. additional ./) and/or disable_search: true?

Thanks, @lesteve. Let's try it.

Comment thread .github/workflows/unit-tests.yml Outdated
uses: codecov/codecov-action@v5
if: ${{ env.COVERAGE == 'true' }} # also condition on that SELECTED_TESTS is empty?
# This step (also the previous step) should depend on whether we run the whole
# test suite (maybe adding && env.SELECTED_TESTS == ''):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now we can ignore this and leave it for later when we tackle commit-based markers. SELECTED_TESTS is something that is used when the [all random seeds] commit marker is used. When that happens you generally only run a few tests (but with all random seeds) and it wouldn't make sense to upload the coverage because it would be small.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring it for now sounds like a practical way to deal with it. Do you think this comment helps remind us what we need to do here, or should I remove it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the comment if you prefer, but I would put a TODO in front to make it slightly clearer that we are planning to tackle it

Comment thread .github/workflows/unit-tests.yml Outdated
Comment on lines +91 to +93
# This step (also the previous step) should depend on whether we run the whole
# test suite (maybe adding && env.SELECTED_TESTS == ''):
if: ${{ env.COVERAGE == 'true' }}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Azure we here test if SELECTED_TESTS is empty (like adding && env.SELECTED_TESTS == '' in the condition below).

Right now, we cannot do that here, because get_selected_tests.py only seems to create an environmental variable valid in azure I believe and also calls get_commit_message.py and that's a separate task that is discussed here: #31832 (comment). I'm not sure if the discussion let to us having a plan and which one.

After SELECTED_TESTS is available here, we could add this condition in this step, or, alternatively, set COVERAGE: 'false' if SELECTED_TESTS is not empty early on, so that codecov doesn't run at all and we save this CI time.

@StefanieSenger
StefanieSenger marked this pull request as ready for review August 14, 2025 10:57
Comment thread .codecov.yml Resolved

# Copy the combined coverage file to the root of the repository:
cp $TEST_DIR/coverage.xml $BUILD_REPOSITORY_LOCALPATH
cp $TEST_DIR/coverage.xml .

@lesteve lesteve Aug 14, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment for potential other reviewers: BUILD_REPOSITORY_LOCALPATH is a Azure-specific environment variable that points where the scikit-learn root folder is located.

Maybe GitHub has an equivalent environment variable, but it feels slightly simpler to rely on the assumption that the current working directory is the same as the scikit-learn repo root folder.

# report that otherwise hides the test failures and forces long scrolls in
# the CI logs.
export COVERAGE_PROCESS_START="$BUILD_SOURCESDIRECTORY/.coveragerc"
export COVERAGE_PROCESS_START="$CHECKOUT_FOLDER/.coveragerc"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as https://github.com/scikit-learn/scikit-learn/pull/31941/files#r2276870682. It feels slightly preferable to rely on the assumption that the current working directory is the scikit-learn repo root folder.

if: ${{ env.COVERAGE == 'true' }}

- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v5

@lesteve lesteve Aug 14, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess one thing to think about for potential reviewers is whether we are OK with using an external GitHub action. Full disclosure: a few years ago the Codecov uploader was compromised see this.

The only secret being exposed would be the Codecov token. I am not sure how much harm can be done with this token (upload fake coverage reports to Codecov sure but something else?).

An alternative would be to use our Azure script build_tools/azure/upload_coverage.shbuild_tools/azure/upload_codecov.sh but it would need to be adapted for GitHub, it uses Azure-specific BUILD_ environment variables.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the build_tools/azure/upload_codecov.sh file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only secret being exposed would be the Codecov token. I am not sure how much harm can be done with this token (upload fake coverage reports to Codecov sure but something else?).

The permissions is set to read for this workflow. The only other possibly harmful side effect would be cache poisoning but based on my understanding of how cache isolation work in GitHub Actions this should not be able to taint our release process.

Comment on lines -57 to +58
TEST_CMD="$TEST_CMD --cov-config='$COVERAGE_PROCESS_START' --cov sklearn --cov-report="
TEST_CMD="$TEST_CMD --cov-config='$COVERAGE_PROCESS_START' --cov=sklearn --cov-report="

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff was necessary to fix the order of the cli arguments.

For some reason, this was evaluated as TEST_CMD="$TEST_CMD --cov-config='$COVERAGE_PROCESS_START' --cov --cov-report= sklearn" when eval "$TEST_CMD" ran and thus not understood. With the = it works on azure and github actions.

@ogrisel ogrisel Aug 25, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why there is a = without anything else after for the --cov-report flag. Is it really necessary?

EDIT: please ignore, this is explained in the comment above.

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the CI run https://github.com/scikit-learn/scikit-learn/actions/runs/17209606816/job/48817950066?pr=31941#step:8:355 and the codecov report and everything looks fine. The github actions upload seem to have properly been associated with this PR:

image

The diff LGTM as well. Thanks for the improvement @StefanieSenger and @lesteve.

@ogrisel
ogrisel merged commit 5736956 into scikit-learn:main Aug 28, 2025
@jeremiedbb jeremiedbb mentioned this pull request Sep 3, 2025
13 tasks done
@StefanieSenger
StefanieSenger deleted the gha_coverage branch September 8, 2025 15:18
DeaMariaLeon pushed a commit to DeaMariaLeon/scikit-learn that referenced this pull request Sep 12, 2025
@lesteve lesteve mentioned this pull request Oct 8, 2025
11 of 14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants