以缓存友好方式在 GitHub Actions 中使用 uvx
摘要
Simon Willison 分享了一种以缓存友好的方式在 GitHub Actions 中使用 uvx 的方法,通过设置环境变量并将其纳入缓存键,以避免重复下载 PyPI。
暂无内容
查看缓存全文
缓存时间: 2026/07/14 04:12
# TIL:在 GitHub Actions 中以缓存友好方式使用 uvx
来源:https://simonwillison.net/2026/Jul/14/uvx-github-actions-cache/
TIL (https://simonwillison.net/elsewhere/til/)在 GitHub Actions 中以缓存友好方式使用 uvx (https://til.simonwillison.net/github-actions/uvx-github-actions-cache)—— 我经常想在 GitHub Actions 中通过 `uvx name-of-tool` 快速运行某个 Python 工具,但又不希望每次工作流运行时都向 PyPI 发起网络请求。我希望工具在首次获取后,后续运行能复用 GitHub Actions 缓存。
我终于找到了一个能兼容缓存的方案,用于在 GitHub Actions 工作流中使用 `uvx tool-name`,而且我很喜欢这个方案。
诀窍在于:在工作流开始时设置环境变量 `UV_EXCLUDE_NEWER: "2026-07-12"`,并将其作为 GitHub Actions 缓存键的一部分。这样一来,所有 `uvx tool-name` 命令都会解析为该日期下的最新版本;如果需要升级工具,只需把日期往后推就能刷新缓存。
我的目标是在 GitHub Actions 中使用 Python 工具,同时避免每次运行工作流时都去 PyPI 下载工具及其依赖的新副本。
**更新**:`astral-sh/setup-uv` 仓库已有一个现有 issue (https://github.com/astral-sh/setup-uv/issues/745),请求他们把默认行为改为缓存而非清除 PyPI 的 wheel。
相似文章
Fast Haskell Scripts on GitHub Actions
The article explains how to speed up running Haskell scripts with Magix on GitHub Actions by caching dependencies and compiled artifacts, reducing full build times from over 100 seconds to near-instant reruns.
@charliermarsh: 我们发现了一些优化,通过“压缩”环境标记来减小 uv.lock 文件的大小。…
uv 的锁文件大小通过压缩环境标记减少了 40-50%。
Uv 0.12.0
uv 0.12.0 是极速 Python 包和项目管理器的一个重大版本,该管理器用 Rust 编写,旨在替代 pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv 等工具。
uv 0.12.0
uv 0.12.0 对 `uv init` 引入了破坏性变更,默认采用 `src/` 为基础的项目结构,配置 uv_build 后端,并设置脚本别名。该更新鼓励使用现代 Python 项目结构。
将你的 GitHub CI 迁移至 Hugging Face Jobs
本文提供了一份逐步指南,教你如何将 GitHub Actions CI 迁移到 Hugging Face Jobs 上运行,从而为类似 Trackio 的项目启用基于 GPU 的测试套件,并将 CPU CI 时间缩短 30%。