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