👷 Optimize cache usage in workflows - #16152
Conversation
📝 Docs previewLast commit ff6a7c0 at: https://9dad2be2.fastapitiangolo.pages.dev |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| env: | ||
| UV_NO_SYNC: true |
There was a problem hiding this comment.
Without this, it would skip installing dev group on uv sync --locked --no-dev --group docs, but still install it later on uv run ./scripts/docs.py build-lang ${{ matrix.lang }} and then cache it.
The same in other workflows
| cache-dependency-glob: | | ||
| base/pyproject.toml | ||
| base/uv.lock |
There was a problem hiding this comment.
We need to use project from base/. By default it would compute the hash on all projects (root directory and base/ directory)
This comment was marked as resolved.
This comment was marked as resolved.
|
This pull request has a merge conflict that needs to be resolved. |
| cache-suffix: dev-all | ||
| # Disable saving cache as we install narrower set of dependencies than dev-all has. | ||
| save-cache: false |
There was a problem hiding this comment.
This workflow only needs prek installed.
We can add a dependency group with just prek, use it here and disable cache. But dev-all is always warm (see warm-cache workflow), so I decided to reuse it instead and just disabled saving.
| - name: Install dependencies | ||
| run: uv sync --locked --group dev |
There was a problem hiding this comment.
Added explicit dependency install steps so that it would be clear what dependencies are used by the job
| uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 | ||
| with: | ||
| version: "latest-known" | ||
| cache-suffix: github-actions |
There was a problem hiding this comment.
| cache-suffix: github-actions | |
| cache-suffix: github-actions | |
| enable-cache: true |
Since setup-uv v10.0.0, cache for this workflow is disabled (it's triggered by workflow_run) - see release notes.
I think we can safely enable it - we use workflow_run trigger with caution:
- No checkout of untrusted branches
- Artifacts used safely (just uploading)
- Narrow permissions (only
statuses: write) - No unpinned dependencies
- We use Zizmor to ensure workflow security
- Nothing untrusted can write the cache that this job restores
We can also consider disabling Smokeshow workflow temporary for FastAPI repo as it always fails due to upload limits.
| with: | ||
| version: "latest-known" | ||
| enable-cache: true | ||
| cache-suffix: tests-${{ matrix.uv-resolution }} |
There was a problem hiding this comment.
We need to include matrix.uv-resolution into cache key in order to prevent possible future collisions if we have two equal matrix items with only difference in uv-resolution.
See: https://github.com/astral-sh/setup-uv#should-i-include-the-resolution-strategy-in-the-cache-key
| base/pyproject.toml | ||
| base/uv.lock | ||
| # Only ever runs on pull requests, so anything it saved would be PR-scoped and useless. | ||
| save-cache: false |
There was a problem hiding this comment.
regression-test only runs on pull requests, so anything it saved would be PR-scoped and useless. But it will still use cache written by other jobs
| # Populates the `dev-all` uv cache at default-branch scope so that it could be used by | ||
| # `pre-commit.yml` (it has `pull_request` trigger and can't write cache available across PRs) | ||
| # and some others that use subset of dev dependencies. |
There was a problem hiding this comment.
Other workflows can't write this cache (for dev-all key) because they are either PR-scoped or run too rarely.
This workflow will run every day to ensure dev-all cache is warm and frequently running pre-commit workflow can use it
Description
After upgrading https://github.com/astral-sh/setup-uv to 9.0.0 it started saving downloaded wheels to cache (
prune-cacheis nowfalseby default).But there are currently several things that make caching less efficient:
--no-devbut don't useUV_NO_SYNC: false, souv runstill installs dev dependencies and caches thempull_requeststill save cache, but this cache is scoped to merge branch and can't be read by othersregression-testjob always has cache miss because it copies project from the base branch intobase/andcache-dependency-globuses all projects to compute the hash by defaultThis PR introduces several cache key suffixes with different set of dependencies, and ensures that there is at least one job for each cache key that writes cache to default branch scope, so that other workflows would be able to read it.
Cache key suffixes:
dev-all:--group dev --extra allwarm-cache.yml(default-branch scoped),pre-commit.yml(PR-branch scoped, only available to this PR)pre-commit.yml,bump-pre-commit-hooks.yml,create-draft-release.yml,prepare-release.ymlwarm-cache.ymlbecausepre-commit.ymlruns onpull_requestand can only write cache scoped to PR branch (others can't read), and other workflows run not regularly and can't keep cache warmdocs:--group docsbuild-docs.yml(writes to default branch scope on push to master branch, so that all other runs can read it)github-actions:--group github-actionslabel-approved.yml,notify-translations.yml,smokeshow.yml,sponsors.yml,topic-repos.yml,translate.ymltests-highest:--group tests --extra alltestjob intest.ymlexcept those that uselowest-direct(writes to default branch scope on push to master branch, so that all other runs can read it)test.ymlexcept those that uselowest-directtests-lowest-direct:--group tests --extra allresolved withUV_RESOLUTION=lowest-directtestjob intest.ymlthat uselowest-direct(writes to default branch scope on push to master branch, so that all other runs can read it)translations:--group github-actions --group translationslangsjobtranslatejobAI Disclaimer
Used Claude Code (Opus 5) to investigate and implement the fix. Checked manually
Checklist