Skip to content

Commit

Permalink
🧪 Plug a reusable action for cache mgmt
Browse files Browse the repository at this point in the history
This action generalizes a piece of code for ABI-sensitive cache
management that we were copying around. It also handles corner cases
better than `actions/setup-python`.

Refs:
* https://github.com/marketplace/actions/cache-python-deps
* https://github.com/re-actors/cache-python-deps
* aio-libs/frozenlist#622
* aio-libs/frozenlist#633
* actions/setup-python#1034
* cherrypy/cheroot#739
  • Loading branch information
webknjaz committed Feb 27, 2025
1 parent 15eced1 commit 82792b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 168 deletions.
154 changes: 18 additions & 136 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ jobs:
)
&& true || false
}}
cache-key-files: >-
${{ steps.calc-cache-key-files.outputs.files-hash-key }}
cache-key-for-dep-files: >-
${{ steps.calc-cache-key-files.outputs.cache-key-for-dep-files }}
git-tag: ${{ steps.git-tag.outputs.tag }}
sdist-artifact-name: ${{ steps.artifact-name.outputs.sdist }}
wheel-artifact-name: ${{ steps.artifact-name.outputs.wheel }}
Expand Down Expand Up @@ -198,83 +198,27 @@ jobs:
) as outputs_file:
print('release-requested=true', file=outputs_file)
- name: Check out src from Git
if: >-
steps.request-check.outputs.release-requested != 'true'
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-depth: >-
${{
steps.request-check.outputs.release-requested == 'true'
&& 1
|| 0
}}
ref: ${{ github.event.inputs.release-committish }}
- name: >-
Calculate Python interpreter version hash value
for use in the cache key
if: >-
steps.request-check.outputs.release-requested != 'true'
id: calc-cache-key-py
run: |
from hashlib import sha512
from os import environ
from pathlib import Path
from sys import version
FILE_APPEND_MODE = 'a'
hash = sha512(version.encode()).hexdigest()
with Path(environ['GITHUB_OUTPUT']).open(
mode=FILE_APPEND_MODE,
) as outputs_file:
print(f'py-hash-key={hash}', file=outputs_file)
- name: >-
Calculate dependency files' combined hash value
for use in the cache key
if: >-
steps.request-check.outputs.release-requested != 'true'
id: calc-cache-key-files
run: |
from os import environ
from pathlib import Path
FILE_APPEND_MODE = 'a'
with Path(environ['GITHUB_OUTPUT']).open(
mode=FILE_APPEND_MODE,
) as outputs_file:
print(
"files-hash-key=${{
hashFiles(
'setup.cfg',
'setup.py',
'tox.ini',
'pyproject.toml',
'.pre-commit-config.yaml',
'pytest.ini'
)
}}",
file=outputs_file,
)
- name: Get pip cache dir
id: pip-cache-dir
if: >-
steps.request-check.outputs.release-requested != 'true'
run: >-
echo "dir=$(python -m pip cache dir)" >> "${GITHUB_OUTPUT}"
shell: bash
uses: ./.github/actions/cache-keys
- name: Set up pip cache
if: >-
steps.request-check.outputs.release-requested != 'true'
uses: actions/cache@v4
uses: re-actors/cache-python-deps@release/v1
with:
path: ${{ steps.pip-cache-dir.outputs.dir }}
key: >-
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key }}-${{
steps.calc-cache-key-files.outputs.files-hash-key }}
restore-keys: |
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key
}}-
${{ runner.os }}-pip-
${{ runner.os }}-
cache-key-for-dependency-files: >-
${{ steps.calc-cache-key-files.outputs.cache-key-for-dep-files }}
- name: Drop Git tags from HEAD for non-release requests
if: >-
steps.request-check.outputs.release-requested != 'true'
Expand Down Expand Up @@ -405,42 +349,11 @@ jobs:
}}
ref: ${{ github.event.inputs.release-committish }}

- name: >-
Calculate Python interpreter version hash value
for use in the cache key
id: calc-cache-key-py
run: |
from hashlib import sha512
from os import environ
from pathlib import Path
from sys import version
FILE_APPEND_MODE = 'a'
hash = sha512(version.encode()).hexdigest()
with Path(environ['GITHUB_OUTPUT']).open(
mode=FILE_APPEND_MODE,
) as outputs_file:
print(f'py-hash-key={hash}', file=outputs_file)
shell: python
- name: Get pip cache dir
id: pip-cache-dir
run: >-
echo "dir=$(python -m pip cache dir)" >> "${GITHUB_OUTPUT}"
- name: Set up pip cache
uses: actions/cache@v4
uses: re-actors/cache-python-deps@release/v1
with:
path: ${{ steps.pip-cache-dir.outputs.dir }}
key: >-
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key }}-${{
needs.pre-setup.outputs.cache-key-files }}
restore-keys: |
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key
}}-
${{ runner.os }}-pip-
cache-key-for-dependency-files: >-
${{ needs.pre-setup.outputs.cache-key-for-dep-files }}
- name: Install tox
run: >-
Expand Down Expand Up @@ -603,42 +516,11 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: >-
Calculate Python interpreter version hash value
for use in the cache key
id: calc-cache-key-py
run: |
from hashlib import sha512
from os import environ
from pathlib import Path
from sys import version
FILE_APPEND_MODE = 'a'
hash = sha512(version.encode()).hexdigest()
with Path(environ['GITHUB_OUTPUT']).open(
mode=FILE_APPEND_MODE,
) as outputs_file:
print(f'py-hash-key={hash}', file=outputs_file)
shell: python
- name: Set up pip cache
uses: actions/cache@v4
uses: re-actors/cache-python-deps@release/v1
with:
path: ~/.cache/pip
key: >-
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key }}-${{
hashFiles('setup.cfg') }}-${{
hashFiles('tox.ini') }}-${{
hashFiles('pyproject.toml') }}-${{
hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key
}}-
${{ runner.os }}-pip-
${{ runner.os }}-
cache-key-for-dependency-files: >-
${{ needs.pre-setup.outputs.cache-key-for-dep-files }}
- name: Install tox
# NOTE: Tox4 does not work correctly in our CI under Windows. Limiting the
# NOTE: version is a workaround to be used until it's fixed.
Expand Down
38 changes: 6 additions & 32 deletions .github/workflows/reusable-linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,15 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: >-
Calculate Python interpreter version hash value
Calculate dependency files' combined hash value
for use in the cache key
id: calc_cache_key_py
run: |
from hashlib import sha512
from os import environ
from pathlib import Path
from sys import version
FILE_APPEND_MODE = 'a'
hash = sha512(version.encode()).hexdigest()
with Path(environ['GITHUB_OUTPUT']).open(
mode=FILE_APPEND_MODE,
) as outputs_file:
print(f'py_hash_key={hash}', file=outputs_file)
shell: python
id: calc-cache-key-files
uses: ./.github/actions/cache-keys
- name: Set up pip cache
uses: actions/cache@v4
uses: re-actors/cache-python-deps@release/v1
with:
path: ~/.cache/pip
key: >-
${{ runner.os }}-pip-${{
steps.calc_cache_key_py.outputs.py_hash_key }}-${{
hashFiles('setup.cfg') }}-${{
hashFiles('tox.ini') }}-${{
hashFiles('pyproject.toml') }}-${{
hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
${{ runner.os }}-pip-${{
steps.calc_cache_key_py.outputs.py_hash_key
}}-
${{ runner.os }}-pip-
${{ runner.os }}-
cache-key-for-dependency-files: >-
${{ steps.calc-cache-key-files.outputs.cache-key-for-dep-files }}
- name: Set up pre-commit cache
uses: actions/cache@v4
with:
Expand Down

0 comments on commit 82792b2

Please sign in to comment.