|
| 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. |
| 8 | + |
| 9 | +inputs: |
| 10 | + python-version: |
| 11 | + description: Python version, supporting MAJOR.MINOR only |
| 12 | + required: true |
| 13 | + |
| 14 | + poetry-version: |
| 15 | + description: Poetry version |
| 16 | + required: true |
| 17 | + |
| 18 | + cache-key: |
| 19 | + description: Cache key to use for manual handling of caching |
| 20 | + required: true |
| 21 | + |
| 22 | + working-directory: |
| 23 | + description: Directory whose poetry.lock file should be cached |
| 24 | + required: true |
| 25 | + |
| 26 | +runs: |
| 27 | + using: composite |
| 28 | + steps: |
| 29 | + - uses: actions/setup-python@v5 |
| 30 | + name: Setup python ${{ inputs.python-version }} |
| 31 | + id: setup-python |
| 32 | + with: |
| 33 | + python-version: ${{ inputs.python-version }} |
| 34 | + |
| 35 | + - uses: actions/cache@v4 |
| 36 | + id: cache-bin-poetry |
| 37 | + name: Cache Poetry binary - Python ${{ inputs.python-version }} |
| 38 | + env: |
| 39 | + SEGMENT_DOWNLOAD_TIMEOUT_MIN: "1" |
| 40 | + with: |
| 41 | + path: | |
| 42 | + /opt/pipx/venvs/poetry |
| 43 | + # This step caches the poetry installation, so make sure it's keyed on the poetry version as well. |
| 44 | + key: bin-poetry-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-${{ inputs.poetry-version }} |
| 45 | + |
| 46 | + - name: Refresh shell hashtable and fixup softlinks |
| 47 | + if: steps.cache-bin-poetry.outputs.cache-hit == 'true' |
| 48 | + shell: bash |
| 49 | + env: |
| 50 | + POETRY_VERSION: ${{ inputs.poetry-version }} |
| 51 | + PYTHON_VERSION: ${{ inputs.python-version }} |
| 52 | + run: | |
| 53 | + set -eux |
| 54 | +
|
| 55 | + # Refresh the shell hashtable, to ensure correct `which` output. |
| 56 | + hash -r |
| 57 | +
|
| 58 | + # `actions/cache@v3` doesn't always seem able to correctly unpack softlinks. |
| 59 | + # Delete and recreate the softlinks pipx expects to have. |
| 60 | + rm /opt/pipx/venvs/poetry/bin/python |
| 61 | + cd /opt/pipx/venvs/poetry/bin |
| 62 | + ln -s "$(which "python$PYTHON_VERSION")" python |
| 63 | + chmod +x python |
| 64 | + cd /opt/pipx_bin/ |
| 65 | + ln -s /opt/pipx/venvs/poetry/bin/poetry poetry |
| 66 | + chmod +x poetry |
| 67 | +
|
| 68 | + # Ensure everything got set up correctly. |
| 69 | + /opt/pipx/venvs/poetry/bin/python --version |
| 70 | + /opt/pipx_bin/poetry --version |
| 71 | +
|
| 72 | + - name: Install poetry |
| 73 | + if: steps.cache-bin-poetry.outputs.cache-hit != 'true' |
| 74 | + shell: bash |
| 75 | + env: |
| 76 | + POETRY_VERSION: ${{ inputs.poetry-version }} |
| 77 | + PYTHON_VERSION: ${{ inputs.python-version }} |
| 78 | + # Install poetry using the python version installed by setup-python step. |
| 79 | + run: pipx install "poetry==$POETRY_VERSION" --python '${{ steps.setup-python.outputs.python-path }}' --verbose |
| 80 | + |
| 81 | + - name: Restore pip and poetry cached dependencies |
| 82 | + uses: actions/cache@v4 |
| 83 | + env: |
| 84 | + SEGMENT_DOWNLOAD_TIMEOUT_MIN: "4" |
| 85 | + WORKDIR: ${{ inputs.working-directory == '' && '.' || inputs.working-directory }} |
| 86 | + with: |
| 87 | + path: | |
| 88 | + ~/.cache/pip |
| 89 | + ~/.cache/pypoetry/virtualenvs |
| 90 | + ~/.cache/pypoetry/cache |
| 91 | + ~/.cache/pypoetry/artifacts |
| 92 | + ${{ env.WORKDIR }}/.venv |
| 93 | + key: py-deps-${{ runner.os }}-${{ runner.arch }}-py-${{ inputs.python-version }}-poetry-${{ inputs.poetry-version }}-${{ inputs.cache-key }}-${{ hashFiles(format('{0}/**/poetry.lock', env.WORKDIR)) }} |
0 commit comments