Skip to content

Commit

Permalink
ci: use optimized caching strategy for Poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted committed Mar 25, 2024
1 parent 0a68556 commit 398da3b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
1 change: 0 additions & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ tests_task:
- echo "PATH=${POETRY_HOME}/bin:${PATH}" >> $CIRRUS_ENV

setup_environment_script:
# FIXME: --sync is broken due to a bug with py-sqlite3 packaging.
# TODO: caching
- poetry install
- poetry env info
Expand Down
27 changes: 16 additions & 11 deletions .github/actions/bootstrap-poetry/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@ description: Configure the environment with the specified Python and Poetry vers

inputs:
python-version:
description: Desired Python version
default: "3.12"
description: Desired Python version expression
default: '3.12'
python-latest:
description: Use an uncached Python if a newer match is available
default: 'false'
python-prereleases:
description: Allow usage of pre-release Python versions
default: 'false'
poetry-spec:
description: pip-compatible installation specification to use for Poetry
default: 'poetry'

runs:
using: composite
steps:
- uses: actions/setup-python@v5
id: setup-python
with:
python-version: ${{ inputs.python-version }}
check-latest: ${{ inputs.python-latest == 'true' }}
allow-prereleases: ${{ inputs.python-prereleases == 'true' }}
update-environment: false

- run: pipx install --python '${{ steps.setup-python.outputs.python-path }}' '${{ inputs.poetry-spec }}'
shell: bash

# Enable handling long path names (+260 char) on the Windows platform
# https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
- run: git config --system core.longpaths true
if: runner.os == 'Windows'
shell: pwsh

- run: pipx install '${{ inputs.poetry-spec }}'
shell: bash

- uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
check-latest: ${{ inputs.python-latest == 'true' }}
cache: poetry
42 changes: 42 additions & 0 deletions .github/actions/poetry-install/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Poetry Install
description: Run `poetry install` with optional artifact and metadata caching

inputs:
args:
description: Arguments for `poetry install`
cache:
description: Enable transparent Poetry artifact and metadata caching
default: 'true'

outputs:
cache-hit:
description: Whether an exact cache hit occured
value: ${{ steps.cache.outputs.cache-hit }}

runs:
using: composite
steps:
- run: printf 'cache-dir=%s\n' "$(poetry config cache-dir)" >> $GITHUB_OUTPUT
id: poetry-config
shell: bash

- uses: actions/cache@v4
id: cache
if: inputs.cache == 'true'
with:
path: ${{ steps.poetry-config.outputs.cache-dir }}/{artifacts,cache}
key: poetry-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
restore-keys: |
poetry-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-
poetry-${{ runner.os }}-
poetry-
enableCrossOsArchive: true

- run: poetry install ${{ inputs.args }}
shell: bash

- run: poetry env info
shell: bash

- run: poetry show
shell: bash
26 changes: 9 additions & 17 deletions .github/workflows/.tests-matrix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@ jobs:
with:
python-version: ${{ inputs.python-version }}

- run: poetry install --sync

- run: poetry env info

- run: poetry show
- uses: ./.github/actions/poetry-install

- uses: actions/cache@v4
with:
path: .mypy_cache
key: mypy-${{ runner.os }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
key: mypy-${{ runner.os }}-py${{ inputs.python-version }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
restore-keys: |
mypy-${{ runner.os }}-${{ env.pythonLocation }}-
mypy-${{ runner.os }}-py${{ inputs.python-version }}-
mypy-${{ runner.os }}-
- run: poetry run mypy
Expand All @@ -61,11 +57,9 @@ jobs:
with:
python-version: ${{ inputs.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show
- uses: ./.github/actions/poetry-install
with:
args: --with github-actions

- run: poetry run pytest --integration -v
env:
Expand All @@ -85,11 +79,9 @@ jobs:
with:
python-version: ${{ inputs.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show
- uses: ./.github/actions/poetry-install
with:
args: --with github-actions

- run: poetry run pip list --format json | jq -r '.[] | "\(.name)=\(.version)"' >> $GITHUB_OUTPUT
id: package-versions
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ jobs:

- uses: ./.github/actions/bootstrap-poetry

- run: poetry install --sync --no-root --only main
- uses: ./.github/actions/poetry-install
with:
args: --no-root --only main

- name: website-build
run: |
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ jobs:

- uses: ./.github/actions/bootstrap-poetry

- run: poetry install --sync --only main,test
- uses: ./.github/actions/poetry-install
with:
args: --only main,test

- run: poetry run env PYTHONPATH="$GITHUB_WORKSPACE" python tests/repositories/fixtures/pypi.org/generate.py

Expand Down

0 comments on commit 398da3b

Please sign in to comment.