|
| 1 | +# Warm Python Dependency Cache |
| 2 | +# |
| 3 | +# Pre-downloads all Python dependencies via JFrog Artifactory and saves them |
| 4 | +# to the GitHub Actions cache. PR workflows (including fork PRs, which cannot |
| 5 | +# authenticate to JFrog) restore this cache and build fully offline. |
| 6 | +# |
| 7 | +# Triggers: |
| 8 | +# - push to main when dependency files change (keeps cache fresh) |
| 9 | +# - daily schedule (prevents 7-day GitHub Actions cache eviction) |
| 10 | +# - manual dispatch (with optional PR number to warm cache for a fork's deps) |
| 11 | + |
| 12 | +name: Warm Python Dependency Cache |
| 13 | + |
| 14 | +on: |
| 15 | + push: |
| 16 | + branches: [main] |
| 17 | + paths: |
| 18 | + - "uv.lock" |
| 19 | + - "pyproject.toml" |
| 20 | + - ".pre-commit-config.yaml" |
| 21 | + schedule: |
| 22 | + - cron: "0 6 * * *" # Daily at 06:00 UTC |
| 23 | + workflow_dispatch: |
| 24 | + inputs: |
| 25 | + pr_number: |
| 26 | + description: "PR number to warm cache for (reads lockfiles from the PR branch). Leave empty to warm from main." |
| 27 | + required: false |
| 28 | + type: string |
| 29 | + |
| 30 | +permissions: |
| 31 | + id-token: write |
| 32 | + contents: read |
| 33 | + pull-requests: read |
| 34 | + |
| 35 | +jobs: |
| 36 | + warm-cache: |
| 37 | + runs-on: |
| 38 | + group: databricks-protected-runner-group |
| 39 | + labels: linux-ubuntu-latest |
| 40 | + |
| 41 | + env: |
| 42 | + UV_FROZEN: "1" |
| 43 | + |
| 44 | + steps: |
| 45 | + - name: Checkout main branch |
| 46 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 |
| 47 | + |
| 48 | + - name: Overlay PR dependency files |
| 49 | + if: inputs.pr_number != '' |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + set -euo pipefail |
| 53 | +
|
| 54 | + PR_DATA=$(curl -sLS \ |
| 55 | + -H "Accept: application/vnd.github+json" \ |
| 56 | + -H "Authorization: Bearer ${{ github.token }}" \ |
| 57 | + "https://api.github.com/repos/${{ github.repository }}/pulls/${{ inputs.pr_number }}") |
| 58 | +
|
| 59 | + FORK_REPO=$(echo "$PR_DATA" | jq -r '.head.repo.full_name') |
| 60 | + FORK_REF=$(echo "$PR_DATA" | jq -r '.head.ref') |
| 61 | +
|
| 62 | + echo "Warming cache for PR #${{ inputs.pr_number }} from ${FORK_REPO}@${FORK_REF}" |
| 63 | +
|
| 64 | + git remote add fork "https://github.com/${FORK_REPO}.git" |
| 65 | + git fetch --depth=1 fork "${FORK_REF}" |
| 66 | + git checkout FETCH_HEAD -- uv.lock pyproject.toml .pre-commit-config.yaml |
| 67 | +
|
| 68 | + - name: Setup JFrog PyPI Proxy |
| 69 | + uses: ./.github/actions/setup-jfrog-pypi |
| 70 | + |
| 71 | + - name: Set up Python |
| 72 | + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5 |
| 73 | + with: |
| 74 | + python-version: "3.10" |
| 75 | + |
| 76 | + - name: Install uv |
| 77 | + uses: astral-sh/setup-uv@38f3f104447c67c051c4a08e39b64a148898af3a # v4 |
| 78 | + with: |
| 79 | + cache-local-path: ~/.cache/uv |
| 80 | + |
| 81 | + - name: Install Hatch |
| 82 | + uses: pypa/hatch@257e27e51a6a5616ed08a39a408a21c35c9931bc # install |
| 83 | + |
| 84 | + - name: Install Python versions for test matrix |
| 85 | + run: uv python install 3.11 3.12 3.13 |
| 86 | + |
| 87 | + - name: Create hatch environments (populates uv cache) |
| 88 | + run: | |
| 89 | + set -euo pipefail |
| 90 | + hatch env create default |
| 91 | + hatch env create test.py3.10 |
| 92 | + hatch env create test.py3.11 |
| 93 | + hatch env create test.py3.12 |
| 94 | + hatch env create test.py3.13 |
| 95 | + hatch env create verify |
| 96 | +
|
| 97 | + - name: Warm pre-commit cache |
| 98 | + run: hatch run pre-commit install-hooks |
| 99 | + |
| 100 | + - name: Create pip wheelhouse |
| 101 | + run: | |
| 102 | + set -euo pipefail |
| 103 | + mkdir -p ~/.cache/pip-wheelhouse |
| 104 | + hatch run python -c " |
| 105 | + try: |
| 106 | + import tomllib |
| 107 | + except ImportError: |
| 108 | + import tomli as tomllib |
| 109 | + with open('pyproject.toml', 'rb') as f: |
| 110 | + data = tomllib.load(f) |
| 111 | + for dep in data['project']['dependencies']: |
| 112 | + print(dep) |
| 113 | + " > /tmp/runtime-deps.txt |
| 114 | + hatch run pip download --dest ~/.cache/pip-wheelhouse \ |
| 115 | + setuptools wheel twine check-wheel-contents \ |
| 116 | + -r /tmp/runtime-deps.txt |
| 117 | +
|
| 118 | + - name: Build package |
| 119 | + run: hatch -v build |
| 120 | + |
| 121 | + - name: Generate cache key |
| 122 | + id: cache-key |
| 123 | + shell: bash |
| 124 | + run: | |
| 125 | + TIMESTAMP=$(date -u +%Y%m%d%H%M%S) |
| 126 | + LOCK_HASH="${{ hashFiles('uv.lock', 'pyproject.toml') }}" |
| 127 | + echo "python-deps-key=python-deps-${LOCK_HASH}-${TIMESTAMP}" >> "$GITHUB_OUTPUT" |
| 128 | +
|
| 129 | + PRECOMMIT_HASH="${{ hashFiles('.pre-commit-config.yaml') }}" |
| 130 | + echo "pre-commit-key=pre-commit-deps-${PRECOMMIT_HASH}-${TIMESTAMP}" >> "$GITHUB_OUTPUT" |
| 131 | +
|
| 132 | + - name: Save uv and pip cache |
| 133 | + uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 |
| 134 | + with: |
| 135 | + path: | |
| 136 | + ~/.cache/uv |
| 137 | + ~/.cache/pip |
| 138 | + ~/.cache/pip-wheelhouse |
| 139 | + key: ${{ steps.cache-key.outputs.python-deps-key }} |
| 140 | + |
| 141 | + - name: Save pre-commit cache |
| 142 | + uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 |
| 143 | + with: |
| 144 | + path: ~/.cache/pre-commit |
| 145 | + key: ${{ steps.cache-key.outputs.pre-commit-key }} |
0 commit comments