首頁 > 後端開發 > Python教學 > 用 uv 加速 CI ⚡

用 uv 加速 CI ⚡

Susan Sarandon
發布: 2024-11-04 00:39:03
原創
912 人瀏覽過

Speed up CI with uv ⚡

我們可以用 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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板