返回 2026-07-15
🛠 工具 / 开源

在 GitHub Actions 中以缓存友好的方式使用 uvxUsing uvx in GitHub Actions in a cache-friendly way

simonwillison.net·2026-07-14 节选正文

在 GitHub Actions 中使用 `uvx tool-name` 运行工具时,缓存失效是一个常见痛点。通过在工作流开始时设置 `UV_EXCLUDE_NEWER` 环境变量并将其作为 GitHub Actions 缓存键的一部分,可以实现高效的缓存复用。这种方法确保了工具版本在特定日期前保持稳定,避免了每次运行都重新下载依赖。作者认为这是一个理想且实用的缓存友好型解决方案。

Simon Willison

TIL Using uvx in GitHub Actions in a cache-friendly way — I often find myself wanting to run a quick Python tool inside of GitHub Actions using `uvx name-of-tool` - but I don't want that to result in a network request to PyPI every time the workflow runs. I want the tool to be fetched the first time and then reused from the GitHub Actions cache for subsequent runs.

I finally found a cache-friendly recipe for using uvx tool-name in GitHub Actions workflows that I like.

The trick is setting a UV_EXCLUDE_NEWER: "2026-07-12" environment variable at the start of the workflow and then using that as part of the GitHub Actions cache key. This means any uvx tool-name commands will resolve to the most recent version as-of that date, and you can bust the cache and upgrade the tools by bumping the date in the future.

My goal here is to use Python tools in GitHub Actions without every run of the workflow hitting PyPI to download a fresh copy of the tool and its dependencies.

Update: Here's an existing issue against the astral-sh/setup-uv repository requesting that they switch the default to cache rather than purge wheels from PyPI.

需要完整排版与评论请前往来源站点阅读。