|
1 | | -# An action for setting up poetry install with caching. |
2 | | -# Using a custom action since the default action does not |
3 | | -# take poetry install groups into account. |
4 | | -# Action code from: |
5 | | -# https://github.com/actions/setup-python/issues/505#issuecomment-1273013236 |
6 | | -name: poetry-install-with-caching |
7 | | -description: Poetry install with support for caching of dependency groups. |
| 1 | +name: uv-sync-with-caching |
| 2 | +description: Install deps with uv, with cache for uv and .venv. |
8 | 3 |
|
9 | 4 | inputs: |
10 | 5 | python-version: |
11 | | - description: Python version, supporting MAJOR.MINOR only |
| 6 | + description: Python version (MAJOR.MINOR) |
12 | 7 | required: true |
13 | | - |
14 | | - poetry-version: |
15 | | - description: Poetry version |
16 | | - required: true |
17 | | - |
| 8 | + uv-version: |
| 9 | + description: uv version (optional; defaults to latest) |
| 10 | + required: false |
| 11 | + default: "" |
18 | 12 | cache-key: |
19 | | - description: Cache key to use for manual handling of caching |
| 13 | + description: Extra cache key salt (e.g., CI job name) |
20 | 14 | required: true |
| 15 | + uv-sync-flags: |
| 16 | + description: Extra flags for `uv sync` (e.g., "--all-groups --all-extras") |
| 17 | + required: false |
| 18 | + default: "" |
21 | 19 |
|
22 | 20 | runs: |
23 | 21 | using: composite |
24 | 22 | steps: |
25 | 23 | - uses: actions/setup-python@v5 |
26 | | - name: Setup python ${{ inputs.python-version }} |
27 | | - id: setup-python |
| 24 | + name: Setup Python ${{ inputs.python-version }} |
28 | 25 | with: |
29 | 26 | python-version: ${{ inputs.python-version }} |
30 | 27 |
|
31 | | - - uses: actions/cache@v4 |
32 | | - id: cache-bin-poetry |
33 | | - name: Cache Poetry binary - Python ${{ inputs.python-version }} |
34 | | - env: |
35 | | - SEGMENT_DOWNLOAD_TIMEOUT_MIN: "1" |
| 28 | + - name: Install uv |
| 29 | + uses: astral-sh/setup-uv@v6 |
36 | 30 | with: |
37 | | - path: | |
38 | | - /opt/pipx/venvs/poetry |
39 | | - # This step caches the poetry installation, so make sure it's keyed on the poetry version as well. |
40 | | - key: bin-poetry-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-${{ inputs.poetry-version }} |
41 | | - |
42 | | - - name: Refresh shell hashtable and fixup softlinks |
43 | | - if: steps.cache-bin-poetry.outputs.cache-hit == 'true' |
44 | | - shell: bash |
45 | | - env: |
46 | | - POETRY_VERSION: ${{ inputs.poetry-version }} |
47 | | - PYTHON_VERSION: ${{ inputs.python-version }} |
48 | | - run: | |
49 | | - set -eux |
| 31 | + version: ${{ inputs.uv-version }} |
50 | 32 |
|
51 | | - # Refresh the shell hashtable, to ensure correct `which` output. |
52 | | - hash -r |
53 | | -
|
54 | | - # `actions/cache@v3` doesn't always seem able to correctly unpack softlinks. |
55 | | - # Delete and recreate the softlinks pipx expects to have. |
56 | | - rm /opt/pipx/venvs/poetry/bin/python |
57 | | - cd /opt/pipx/venvs/poetry/bin |
58 | | - ln -s "$(which "python$PYTHON_VERSION")" python |
59 | | - chmod +x python |
60 | | - cd /opt/pipx_bin/ |
61 | | - ln -s /opt/pipx/venvs/poetry/bin/poetry poetry |
62 | | - chmod +x poetry |
63 | | -
|
64 | | - # Ensure everything got set up correctly. |
65 | | - /opt/pipx/venvs/poetry/bin/python --version |
66 | | - /opt/pipx_bin/poetry --version |
67 | | -
|
68 | | - - name: Install poetry |
69 | | - if: steps.cache-bin-poetry.outputs.cache-hit != 'true' |
| 33 | + # Use a stable, repo-local cache dir so Actions cache works across runners. |
| 34 | + - name: Set uv cache dir |
70 | 35 | shell: bash |
71 | | - env: |
72 | | - POETRY_VERSION: ${{ inputs.poetry-version }} |
73 | | - PYTHON_VERSION: ${{ inputs.python-version }} |
74 | | - # Install poetry using the python version installed by setup-python step. |
75 | | - run: pipx install "poetry==$POETRY_VERSION" --python '${{ steps.setup-python.outputs.python-path }}' --verbose |
| 36 | + run: echo "UV_CACHE_DIR=$GITHUB_WORKSPACE/.uv_cache" >> "$GITHUB_ENV" |
76 | 37 |
|
77 | | - - name: Restore pip and poetry cached dependencies |
| 38 | + - name: Restore uv and venv caches |
78 | 39 | uses: actions/cache@v4 |
79 | | - env: |
80 | | - SEGMENT_DOWNLOAD_TIMEOUT_MIN: "4" |
81 | 40 | with: |
82 | 41 | path: | |
83 | | - ~/.cache/pip |
84 | | - ~/.cache/pypoetry/virtualenvs |
85 | | - ~/.cache/pypoetry/cache |
86 | | - ~/.cache/pypoetry/artifacts |
| 42 | + ./.uv_cache |
87 | 43 | ./.venv |
88 | | - key: py-deps-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-poetry-${{ inputs.poetry-version }}-${{ inputs.cache-key }}-${{ hashFiles('./poetry.lock') }} |
| 44 | + key: > |
| 45 | + uv-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-${{ inputs.cache-key }}- |
| 46 | + ${{ hashFiles('pyproject.toml') }}- |
| 47 | + ${{ hashFiles('uv.lock') }} |
| 48 | + restore-keys: | |
| 49 | + uv-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-${{ inputs.cache-key }}- |
| 50 | + uv-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}- |
| 51 | + uv-${{ runner.os }}-${{ runner.arch }}- |
| 52 | +
|
| 53 | + - name: Sync dependencies with uv |
| 54 | + shell: bash |
| 55 | + run: uv sync ${{ inputs.uv-sync-flags }} |
| 56 | + |
| 57 | + - name: Show env info |
| 58 | + shell: bash |
| 59 | + run: | |
| 60 | + uv --version |
| 61 | + uv pip list | head -n 50 |
0 commit comments