Cache the .tox
directory on GitHub Actions for pypackages
#34
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #10.
Problem
The
pyramid-app
cookiecutter already has caching of the.tox
directory on GitHub Actions, as do our pre-existing Pyramid apps, andpip-sync
updates the venvs from the requirements files each time CI runs. Our Python packages don't currently cache.tox
on GitHub Actions (neither the ones using thepypackage
cookiecutter in this repo nor the ones usingh-cookiecutter-pypackage
). This means:main
and on PR branches. This can be really annoying when you come to send a PR for a package and find that CI is brokenSolution
This PR adds caching of the
.tox
directory for thepypackage
cookiecutter. Consequences:CI will run faster because all the dependencies don't need to be installed every time. This isn't as big a deal as for our apps because our packages tend to have far fewer dependencies but it's still significant: I think it should save anything from 30s to a few minutes on each CI run.
CI won't break on
main
or on pull requests if a new version of a dependency is released that breaks our package because CI won't use the new version of the dependency: it'll use the previously cached version. We've effectively pinned our dependencies on CI but by using cached.tox
directories instead of pinned requirements files.If the project's test dependencies in the
tox.ini
file change tox should recreate the venvs from scratch (this is tox's default behaviour) which will install any new test dependencies (and uninstall any that've been removed) and also upgrade all dependencies to their latest versions. So a new version of a dependency could break CI in this case. Note that there's no concept of syncing/updsting a venv in place likepip-sync
does, that's not applicable here because we don't have pinned requirements file and it's not needed as much because recreating the venvs of packages (which have far fewer dependencies) from scratch is much quicker than doing so for appsIf the project's production dependencies in the
setup.cfg
file change that will cause a cache miss because the hash ofsetup.cfg
is included in the cache key, so the venvs will be recreated from scratch. Again this will install any new dependencies and also update all dependencies to their latest versions so a new version of a dependency could break CIWhen new versions of dependencies are released we actually want CI to upgrade to them because we want to test our packages against new versions of dependencies and get notified if they're broken. For this reason the scheduled nightly CI run of a package always recreates the venvs from scratch. If the nightly run passes then GHA will update the cached
.tox
dir and future CI runs onmain
or on a branch will use the new versions of the dependencies. If the nightly run fails then GHA will not update the cache (this is GHA's default behaviour) so non-nightly CI runs onmain
or on branches won't be broken.If your branch name ends in
-rm-tox-dir
then CI on your branch will delete and recreate the venvs. This is necessary when the nightly run is broken by a new version of a dependency and you want to send a PR with a fix. Your PR wouldn't be tested against the new version of the dependency because it'll use the cached version. If you add-rm-tox-dir
to the end of your branch name then your PR will be tested against the latest versions of all dependencies. PRs with-rm-tox-dir
will not affect the cached.tox
directories seen by CI runs onmain
or other branches: PR branches can only see files cached by workflow runs on their own branch or onmain
(again: default GHA behaviour).When running CI manually via the web interface there's a Delete and recreate cached .tox dir checkbox that you can use to run against the latest versions of all dependencies. If such a workflow run on
main
succeeds it'll update the cached venvs for future runs onmain
or other branches. If it fails it won't (default GHA behaviour). Manual workflow runs on non-main
branches won't update the cache formain
or other branches even if they succeed (default GHA behaviour).We'll be able to see in the GitHub Actions logs exactly what versions of all the dependencies were used for each workflow run: tox prints out the output of
pip freeze
by default (tox-faster disables this locally but not on CI)