NatLibFi / Annif

Annif is a multi-algorithm automated subject indexing tool for libraries, archives and museums.
https://annif.org
Other
195 stars 41 forks source link

Fix slow CI/CD runs for Python 3.10 #672

Closed juhoinkinen closed 1 year ago

juhoinkinen commented 1 year ago

Lately the Python 3.10 test CI/CD jobs have been slow (~7 mins), because the Python dependencies have not been available from cache. First occurrence is in the CI/CD job #308, however it seems that the reason for the slow-down is introducing the linting job in PR #656, because this job has shared the cache keys with the Python 3.10 test job. This has prevented (re)creating/updating the cache by the test job (with the necessary optional dependencies); the "Post" steps for setup-python and caching steps in the test jobs have had errors like:

Failed to save: Unable to reserve cache with key setup-python-Linux-python-3.10.9-poetry-6025e512699131453ed16a5ae9a4be9139ae85329e34f47f750e6fb7af36d308, another job may be creating this cache. More details: Cache already exists.

To avoid this in the caching step the cache key is appended with -${{ github.job }} so the keys are now clearly different in lint and test jobs (poetry-installation-and-repos-3.10.8-1.2.0-lint vs. poetry-installation-and-repos-3.10-1.2.0-test). In the setup-python action the key cannot be specified, so the fix (for now) is to to pin the Python version to 3.10.8 in the linting job, but in the test job to more relaxed 3.10, which makes the keys different by the patch version (setup-python-Linux-python-3.10.8-poetry-<hash> vs. setup-python-Linux-python-3.10.9-poetry-<hash>).

Now restoring and saving cache is more controlled by using separate cache/restore and cache/save actions: restoring is performed before every job, but saving only after the test jobs that have various optional dependencies installed.

Also now the cache should be updated/recreated if some dependency has been updated, as the cache key includes hash of the poetry.lock. The cache restoration is based on "closest matching" key by using restore-keys (the plain key is not used at all).

The cache/save step is conditional: it is skipped if there are no changes in dependencies.

To make the workflow setup more manageable, the caching, Poetry install and Python setup steps are gathered in one composite action in .github/actions/prepare/action.yml file.

codecov[bot] commented 1 year ago

Codecov Report

Base: 99.56% // Head: 99.56% // Increases project coverage by +0.00% :tada:

Coverage data is based on head (c10792c) compared to base (8a194c4). Patch has no changes to coverable lines.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #672 +/- ## ======================================= Coverage 99.56% 99.56% ======================================= Files 87 87 Lines 6145 6158 +13 ======================================= + Hits 6118 6131 +13 Misses 27 27 ``` | [Impacted Files](https://codecov.io/gh/NatLibFi/Annif/pull/672?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NatLibFi) | Coverage Δ | | |---|---|---| | [annif/corpus/subject.py](https://codecov.io/gh/NatLibFi/Annif/pull/672?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NatLibFi#diff-YW5uaWYvY29ycHVzL3N1YmplY3QucHk=) | `100.00% <0.00%> (ø)` | | | [annif/util.py](https://codecov.io/gh/NatLibFi/Annif/pull/672?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NatLibFi#diff-YW5uaWYvdXRpbC5weQ==) | `98.57% <0.00%> (+0.26%)` | :arrow_up: | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NatLibFi). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NatLibFi)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

juhoinkinen commented 1 year ago

Back to draft, there should be a better way to fix this.

juhoinkinen commented 1 year ago

Force-pushed to drop very many unsuccessful commits trying to make caching step conditional, with lines like

if: steps.prepare.outputs.cache-matched-key != format('poetry-installation-and-cache-{0}-{1}-{2}', ${{ matrix.python-version }}, ${{ env.POETRY_VERSION }}, ${{ hashFiles('**/poetry.lock') }})
sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information