我们可以使用 uv 在 GitHub Actions 上进行 linting 和测试,速度大约快 1.5 倍。
使用预提交进行 linting 时:
name: Lint on: [push, pull_request, workflow_dispatch] env: FORCE_COLOR: 1 permissions: contents: read jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: "3.x" cache: pip - uses: pre-commit/action@v3.0.1
我们可以用 tox-dev/action-pre-commit-uv 替换预提交/操作:
- uses: actions/setup-python@v5 with: python-version: "3.x" - cache: pip - - uses: pre-commit/action@v3.0.1 + - uses: tox-dev/action-pre-commit-uv@v1
name: Lint on: [push, pull_request, workflow_dispatch] env: FORCE_COLOR: 1 permissions: contents: read jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: persist-credentials: false - uses: actions/setup-python@v5 with: python-version: "3.x" - uses: tox-dev/action-pre-commit-uv@v1
这意味着 uv 将创建虚拟环境并安装用于预提交的软件包,这对于没有缓存时的初始种子操作来说更快。
例如:python/blurb#32
Before | After | Times faster | |
---|---|---|---|
No cache | 60s | 37s | 1.62 |
With cache | 11s | 11s | 1.00 |
使用 tox 进行测试时:
name: Test on: [push, pull_request, workflow_dispatch] permissions: contents: read env: FORCE_COLOR: 1 jobs: test: runs-on: ubuntu-latest strategy: fail-fast: false matrix: python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"] steps: - uses: actions/checkout@v4 with: persist-credentials: false - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} allow-prereleases: true cache: pip - name: Install dependencies run: | python --version python -m pip install -U pip python -m pip install -U tox - name: Tox tests run: | tox -e py
我们可以用 tox-uv 代替 tox:
- name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} allow-prereleases: true - cache: pip - - name: Install dependencies - run: | - python --version - python -m pip install -U pip - python -m pip install -U tox + - name: Install uv + uses: hynek/setup-cached-uv@v2 - name: Tox tests run: | - tox -e py + uvx --with tox-uv tox -e py
name: Test on: [push, pull_request, workflow_dispatch] permissions: contents: read env: FORCE_COLOR: 1 jobs: test: runs-on: ubuntu-latest strategy: fail-fast: false matrix: python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] steps: - uses: actions/checkout@v4 with: persist-credentials: false - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} allow-prereleases: true - name: Install uv uses: hynek/setup-cached-uv@v2 - name: Tox tests run: | uvx --with tox-uv tox -e py
tox-uv 是 tox 插件,用于在您的 tox 环境中用 uv 替换 virtualenv 和 pip。我们只需要安装 uv,并使用 uvx 来安装 tox-uv 和运行 tox,以便更快地安装 tox、虚拟环境及其中的依赖项。
例如:python/blurb#32
Before | After | Times faster | |
---|---|---|---|
No cache | 2m 0s | 1m 26s | 1.40 |
With cache | 1m 58s | 1m 22s | 1.44 |
运行新工具 zizmor 来查找 GitHub Actions 中的安全问题。
标题照片:“1952 年赫尔辛基奥运会上的公路自行车”,由 Olympia-Kuva Oy 和赫尔辛基市博物馆提供,公共领域。
以上是用 uv 加速 CI ⚡的详细内容。更多信息请关注PHP中文网其他相关文章!