diff --git a/.github/actions/run-service-tests/action.yml b/.github/actions/run-service-tests/action.yml index 370c039d6..4e85fbfab 100644 --- a/.github/actions/run-service-tests/action.yml +++ b/.github/actions/run-service-tests/action.yml @@ -8,6 +8,13 @@ inputs: uv-version: description: 'uv version to use' required: true + save-cache: + description: >- + Save the uv and HuggingFace caches. Only one job per cache key should + save; pass "false" from every other caller, and always from a run that + executes untrusted code. + required: false + default: 'true' pytest-args: description: 'Extra arguments passed to pytest through `make test-all`' required: false @@ -16,11 +23,15 @@ inputs: runs: using: 'composite' steps: - - name: Cache HuggingFace Models - uses: actions/cache@v5 + # Bump the version suffix to drop a stale model cache; the key is static, + # so a saved cache is never refreshed otherwise. Keep it in sync with the + # restore steps in test.yml. + - name: Restore HuggingFace models + id: hf-cache + uses: actions/cache/restore@v5 with: path: hf_cache - key: ${{ runner.os }}-hf-cache + key: ${{ runner.os }}-hf-cache-v2 - name: Set HuggingFace token shell: bash @@ -33,15 +44,26 @@ runs: with: python-version: ${{ inputs.python-version }} + # Caching is done by the restore/save steps around the install instead of + # by setup-uv, which has no restore-keys fallback. See test.yml. - name: Install uv uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ inputs.uv-version }} - enable-cache: true + enable-cache: false + cache-local-path: ~/.cache/uv # sets UV_CACHE_DIR python-version: ${{ inputs.python-version }} - cache-dependency-glob: | - pyproject.toml - uv.lock + + # The runner OS is hard-coded in the key; callers pin runs-on: ubuntu-24.04. + # Key format is shared with test.yml and lint.yml; keep all three in sync. + - name: Restore uv cache + id: uv-cache + uses: actions/cache/restore@v5 + with: + path: ~/.cache/uv + key: uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ inputs.python-version }}-pruned-${{ hashFiles('pyproject.toml', 'uv.lock') }} + restore-keys: | + uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ inputs.python-version }}-pruned- - name: Install required dependencies shell: bash @@ -58,6 +80,20 @@ runs: run: | uv sync --all-extras + # Saved before the tests, so a failing test doesn't cost the next run its + # cache. `prune --ci` keeps only wheels built from source. + - name: Prune uv cache + if: inputs.save-cache == 'true' && steps.uv-cache.outputs.cache-hit != 'true' + shell: bash + run: uv cache prune --ci + + - name: Save uv cache + if: inputs.save-cache == 'true' && steps.uv-cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v5 + with: + path: ~/.cache/uv + key: ${{ steps.uv-cache.outputs.cache-primary-key }} + - name: Authenticate to Google Cloud uses: google-github-actions/auth@v1 with: @@ -69,3 +105,13 @@ runs: PYTEST_ARGS: ${{ inputs.pytest-args }} run: | make test-all ARGS="$PYTEST_ARGS" + + # After the tests, which download the models. Only on success, because the + # key is static: a cache saved after an early failure would be missing + # models until the key is bumped. + - name: Save HuggingFace models + if: inputs.save-cache == 'true' && steps.hf-cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v5 + with: + path: hf_cache + key: ${{ steps.hf-cache.outputs.cache-primary-key }} diff --git a/.github/workflows/auto-release.yml b/.github/workflows/auto-release.yml index 6b89c2590..4e243e8cb 100644 --- a/.github/workflows/auto-release.yml +++ b/.github/workflows/auto-release.yml @@ -111,11 +111,11 @@ jobs: uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + # No cache: a release job must not run anything restored from the + # shared cache, and its saves would only compete with the test + # jobs' caches for the repository's 10 GB. + enable-cache: false python-version: ${{ env.PYTHON_VERSION }} - cache-dependency-glob: | - pyproject.toml - uv.lock - name: Install dependencies run: uv sync --frozen @@ -194,11 +194,11 @@ jobs: uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + # No cache: a release job must not run anything restored from the + # shared cache, and its saves would only compete with the test + # jobs' caches for the repository's 10 GB. + enable-cache: false python-version: ${{ env.PYTHON_VERSION }} - cache-dependency-glob: | - pyproject.toml - uv.lock - name: Apply release version to pyproject.toml and uv.lock run: | @@ -320,11 +320,11 @@ jobs: uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + # No cache: a release job must not run anything restored from the + # shared cache, and its saves would only compete with the test + # jobs' caches for the repository's 10 GB. + enable-cache: false python-version: ${{ env.PYTHON_VERSION }} - cache-dependency-glob: | - pyproject.toml - uv.lock - name: Install dependencies run: uv sync --frozen diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 96cf7e2d5..7414350ab 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -6,26 +6,33 @@ on: branches: - main +# A new push to a PR cancels that PR's previous run; pushes to main are never +# cancelled. Same rule as test.yml. +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.pull_request.number || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + env: UV_VERSION: "0.13.1" + # The uv cache key below hard-codes the runner OS, so the jobs pin + # `runs-on: ubuntu-24.04` (all of them, for uniformity). Change both together. jobs: lock: name: Check uv.lock is up to date - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - name: Check out repository uses: actions/checkout@v6 + # No cache: `uv lock --check` installs nothing, so a cache would only take + # up space. - name: Install uv uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true - cache-dependency-glob: | - pyproject.toml - uv.lock + enable-cache: false # Fails a pyproject.toml change that was never locked, before the drift # lands on main and dirties the next contributor's tree. @@ -34,7 +41,7 @@ jobs: check: name: Style-check ${{ matrix.python-version }} - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 strategy: matrix: python-version: @@ -57,11 +64,21 @@ jobs: uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + enable-cache: false + cache-local-path: ~/.cache/uv # sets UV_CACHE_DIR python-version: ${{ matrix.python-version }} # sets UV_PYTHON - cache-dependency-glob: | - pyproject.toml - uv.lock + + # Restore-only: the Test Suite workflow saves these caches (same key format + # as test.yml; keep them in sync). This job installs the test legs' + # `--all-extras` environment plus the docs group, so it reuses them as + # they are. + - name: Restore uv cache + uses: actions/cache/restore@v5 + with: + path: ~/.cache/uv + key: uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ matrix.python-version }}-pruned-${{ hashFiles('pyproject.toml', 'uv.lock') }} + restore-keys: | + uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ matrix.python-version }}-pruned- - name: Install dependencies run: | diff --git a/.github/workflows/test-fork-pr.yml b/.github/workflows/test-fork-pr.yml index 715102084..f16ea33ae 100644 --- a/.github/workflows/test-fork-pr.yml +++ b/.github/workflows/test-fork-pr.yml @@ -13,6 +13,9 @@ permissions: checks: write pull-requests: read +# No `concurrency` block: cancelling a run would leave the check run created +# below stuck at in_progress on the fork PR. + env: PYTHON_VERSION: "3.11" UV_VERSION: "0.13.1" @@ -20,7 +23,7 @@ env: jobs: setup: name: Validate PR - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 outputs: head_sha: ${{ steps.pr-info.outputs.head_sha }} head_repo: ${{ steps.pr-info.outputs.head_repo }} @@ -59,8 +62,13 @@ jobs: service-tests: name: Service Tests - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 needs: setup + # This job runs fork code on the dispatching ref (main), and a cache saved + # here would be restored by every later run on main and by every PR. + # `read` gives the job a cache token that can restore but not save, so + # the cache service refuses a save from any step, fork code included. + cache-mode: read steps: - name: Create check run id: check @@ -82,12 +90,26 @@ jobs: with: repository: ${{ needs.setup.outputs.head_repo }} ref: ${{ needs.setup.outputs.head_sha }} + # Keep GITHUB_TOKEN (checks: write) out of .git/config, where the + # fork's code could read it. + persist-credentials: false + + # A second guard, in case `cache-mode` is ever not honoured: run the + # action from this repository rather than from the fork checkout, so + # the fork cannot edit away `save-cache: "false"`. + - name: Check out trusted actions + uses: actions/checkout@v6 + with: + path: trusted-actions + sparse-checkout: .github/actions + persist-credentials: false - name: Run service tests - uses: ./.github/actions/run-service-tests + uses: ./trusted-actions/.github/actions/run-service-tests with: python-version: ${{ env.PYTHON_VERSION }} uv-version: ${{ env.UV_VERSION }} + save-cache: "false" env: HF_HOME: ${{ github.workspace }}/hf_cache HF_TOKEN: ${{ secrets.HF_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fcc5c2434..a24b14726 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,14 +12,24 @@ on: workflow_dispatch: +# A new push to a PR cancels that PR's previous run. Every other event gets a +# group of its own (the run ID), so pushes to main, the nightly schedule and +# manual dispatches are never cancelled. +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.pull_request.number || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + env: PYTHON_VERSION: "3.11" UV_VERSION: "0.13.1" + # The uv cache keys below hard-code the runner OS, so every job pins + # `runs-on: ubuntu-24.04` rather than following ubuntu-latest. Change both + # together. jobs: service-tests: name: Service Tests - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 # Needs API-key secrets. Fork PRs and Dependabot-triggered runs get none, so # they skip here; a maintainer runs them via test-fork-pr.yml instead. if: github.event_name != 'pull_request' || (github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]') @@ -27,11 +37,17 @@ jobs: - name: Check out repository uses: actions/checkout@v6 + # The canary saves the PYTHON_VERSION uv cache and, when its tests pass, the + # HuggingFace model cache. It finishes before the matrix starts, so the + # matrix legs and notebooks restore what it saved and never save these + # themselves. When it is skipped (fork and Dependabot PRs) they restore + # from the base branch instead. - name: Run service tests uses: ./.github/actions/run-service-tests with: python-version: ${{ env.PYTHON_VERSION }} uv-version: ${{ env.UV_VERSION }} + save-cache: "true" pytest-args: --cov --cov-report= env: HF_HOME: ${{ github.workspace }}/hf_cache @@ -76,7 +92,7 @@ jobs: test: name: Python ${{ matrix.python-version }} - redis-py ${{ matrix.redis-py-version }} [${{ matrix.redis-image }}] - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 # `needs` is here to order this job after the HF model cache is primed, not # to gate it on the service suite passing (it was `needs: prime-cache` when # it was introduced in #306). Without a status check function, `needs` also @@ -101,6 +117,8 @@ jobs: redis-py-version: ["6.x", "7.x", "8.x"] # `redis:latest` floats, so a failure on it is not reproducible from a # SHA; it is advisory below. Pinned tags carry the blocking coverage. + # The uv cache saver below matches redis-py-version 8.x and + # redis:latest; update it if either value goes. redis-image: ["redis:8.2", "redis:8.4", "redis:latest"] include: - redis-image: "redis:latest" @@ -109,32 +127,47 @@ jobs: - name: Check out repository uses: actions/checkout@v6 - - name: Cache HuggingFace Models - uses: actions/cache@v4 + # Restore-only: service-tests is the one job that saves this cache. + - name: Restore HuggingFace models + uses: actions/cache/restore@v5 with: path: hf_cache - key: ${{ runner.os }}-hf-cache + key: ${{ runner.os }}-hf-cache-v2 - name: Install Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} + # setup-uv's own cache has no restore-keys fallback, so any uv.lock change + # (the release bot's version bump included) would start every leg cold. + # The restore and save steps around the install do the caching instead. - name: Install uv uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + enable-cache: false + cache-local-path: ~/.cache/uv # sets UV_CACHE_DIR python-version: ${{ matrix.python-version }} # sets UV_PYTHON - cache-dependency-glob: | - pyproject.toml - uv.lock + + # Key format is shared with lint.yml and the run-service-tests action; + # keep all three in sync. + - name: Restore uv cache + id: uv-cache + uses: actions/cache/restore@v5 + with: + path: ~/.cache/uv + key: uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ matrix.python-version }}-pruned-${{ hashFiles('pyproject.toml', 'uv.lock') }} + restore-keys: | + uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ matrix.python-version }}-pruned- - name: Install dependencies run: | set -euo pipefail - uv sync --all-extras --frozen + # The docs group (Sphinx and friends) is only for building docs; + # no test imports it. + uv sync --all-extras --no-group docs --frozen # Must cover every redis-py-version matrix value; unhandled values fail. case "$REDIS_PY_VERSION" in @@ -150,6 +183,24 @@ jobs: uv pip install "$spec" uv run python -c 'import redis; print("redis-py:", redis.__version__)' + # One saver per Python version, so legs don't race to save the same key. + # service-tests saves PYTHON_VERSION; for the others the redis-py 8.x x + # redis:latest leg saves (`redis:latest` is the image value that never + # rotates; its continue-on-error doesn't matter because the save comes + # before the tests). Saving here rather than after the tests keeps the + # cache when a test fails. `prune --ci` keeps only wheels built from + # source; pre-built wheels download faster than they restore. + - name: Prune uv cache + if: ${{ matrix.python-version != env.PYTHON_VERSION && matrix.redis-py-version == '8.x' && matrix.redis-image == 'redis:latest' && steps.uv-cache.outputs.cache-hit != 'true' }} + run: uv cache prune --ci + + - name: Save uv cache + if: ${{ matrix.python-version != env.PYTHON_VERSION && matrix.redis-py-version == '8.x' && matrix.redis-image == 'redis:latest' && steps.uv-cache.outputs.cache-hit != 'true' }} + uses: actions/cache/save@v5 + with: + path: ~/.cache/uv + key: ${{ steps.uv-cache.outputs.cache-primary-key }} + - name: Run tests run: | # One data file per leg; the coverage job combines them. Artifact @@ -170,7 +221,7 @@ jobs: coverage: name: Coverage - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 # Runs whenever the test jobs finish, pass or fail: coverage from a red run # is still a measurement. A skipped job (service-tests on fork PRs, # cluster-tests on every PR and push) just leaves its segment out, and @@ -186,15 +237,14 @@ jobs: with: python-version: ${{ env.PYTHON_VERSION }} + # No cache: this job installs only the dev group, without the project, + # so a cache would hold little. - name: Install uv uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + enable-cache: false python-version: ${{ env.PYTHON_VERSION }} # sets UV_PYTHON - cache-dependency-glob: | - pyproject.toml - uv.lock - name: Install coverage # The dev group pins coverage through pytest-cov, so this reads the @@ -310,7 +360,7 @@ jobs: # No other job passes `--run-cluster-tests`, so this is the only one that # runs `requires_cluster` tests. if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 env: HF_HOME: ${{ github.workspace }}/hf_cache REDIS_PY_VERSION: ${{ matrix.redis-py-version }} @@ -328,14 +378,15 @@ jobs: - name: Check out repository uses: actions/checkout@v6 - - name: Cache HuggingFace Models - uses: actions/cache@v4 + # Restore-only, like the uv cache below: service-tests saves both. + - name: Restore HuggingFace models + uses: actions/cache/restore@v5 with: path: hf_cache - key: ${{ runner.os }}-hf-cache + key: ${{ runner.os }}-hf-cache-v2 - name: Install Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: ${{ env.PYTHON_VERSION }} @@ -343,11 +394,17 @@ jobs: uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + enable-cache: false + cache-local-path: ~/.cache/uv # sets UV_CACHE_DIR python-version: ${{ env.PYTHON_VERSION }} # sets UV_PYTHON - cache-dependency-glob: | - pyproject.toml - uv.lock + + - name: Restore uv cache + uses: actions/cache/restore@v5 + with: + path: ~/.cache/uv + key: uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ env.PYTHON_VERSION }}-pruned-${{ hashFiles('pyproject.toml', 'uv.lock') }} + restore-keys: | + uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ env.PYTHON_VERSION }}-pruned- - name: Install dependencies run: | @@ -396,7 +453,7 @@ jobs: notebooks: name: Notebook Validation - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 # Ordered after the HF cache is primed, not gated on it; see the `test` job. needs: service-tests # Needs API-key secrets, unavailable to fork PRs and Dependabot. Keep in @@ -408,14 +465,15 @@ jobs: - name: Check out repository uses: actions/checkout@v6 - - name: Cache HuggingFace Models - uses: actions/cache@v4 + # Restore-only, like the uv cache below: service-tests saves both. + - name: Restore HuggingFace models + uses: actions/cache/restore@v5 with: path: hf_cache - key: ${{ runner.os }}-hf-cache + key: ${{ runner.os }}-hf-cache-v2 - name: Install Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: ${{ env.PYTHON_VERSION }} @@ -423,11 +481,17 @@ jobs: uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + enable-cache: false + cache-local-path: ~/.cache/uv # sets UV_CACHE_DIR python-version: ${{ env.PYTHON_VERSION }} # sets UV_PYTHON - cache-dependency-glob: | - pyproject.toml - uv.lock + + - name: Restore uv cache + uses: actions/cache/restore@v5 + with: + path: ~/.cache/uv + key: uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ env.PYTHON_VERSION }}-pruned-${{ hashFiles('pyproject.toml', 'uv.lock') }} + restore-keys: | + uv-v1-ubuntu-24.04-${{ runner.arch }}-py${{ env.PYTHON_VERSION }}-pruned- - name: Install dependencies run: | @@ -485,26 +549,24 @@ jobs: make test-notebooks docs: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - name: Check out repository uses: actions/checkout@v6 - name: Install Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: ${{ env.PYTHON_VERSION }} + # No cache: the docs install is small. - name: Install uv uses: astral-sh/setup-uv@v10.0.0 with: version: ${{ env.UV_VERSION }} - enable-cache: true + enable-cache: false python-version: ${{ env.PYTHON_VERSION }} # sets UV_PYTHON - cache-dependency-glob: | - pyproject.toml - uv.lock - + - name: Install dependencies run: | uv sync --group docs --frozen