|
| 1 | +--- |
| 2 | + |
| 3 | +name: cache-python-deps |
| 4 | +description: >- |
| 5 | + A GitHub Action maintaining caches dependent on Python runtime |
| 6 | + and ABI stability. |
| 7 | +
|
| 8 | +inputs: |
| 9 | + cache-directory-lookup-command: |
| 10 | + description: A command that prints cache directory to standard output. |
| 11 | + default: python -Im pip cache dir |
| 12 | + required: false |
| 13 | + cache-key-for-dependency-files: |
| 14 | + description: >- |
| 15 | + A cache key string derived from the dependency declaration files. |
| 16 | + required: true |
| 17 | + |
| 18 | +outputs: |
| 19 | + cache-directory-path: |
| 20 | + description: The discovered cache directory path. |
| 21 | + value: ${{ steps.cache-dir.outputs.dir }} |
| 22 | + cache-key-for-os: |
| 23 | + description: >- |
| 24 | + A cache key string derived from the current OS and interpreter stability. |
| 25 | + value: >- |
| 26 | + ${{ steps.python-runtime.outputs.restore-key-fallback-prefix }} |
| 27 | + cache-key-for-dependencies: |
| 28 | + description: >- |
| 29 | + A cache key string derived from the current interpreter version |
| 30 | + and the dependency file hashes. |
| 31 | + value: >- |
| 32 | + ${{ steps.python-runtime.outputs.cache-entry-key }} |
| 33 | + cache-key-for-python-interpreter: |
| 34 | + description: >- |
| 35 | + A cache key string derived from the current interpreter version. |
| 36 | + value: >- |
| 37 | + ${{ steps.python-runtime.outputs.restore-key-prefix }} |
| 38 | + is-stable-abi: |
| 39 | + description: >- |
| 40 | + Whether the currently used Python version has a reliable |
| 41 | + Application Binary Interface. If it doesn't, it's best to avoid |
| 42 | + caching any dependencies. |
| 43 | + value: ${{ steps.python-runtime.outputs.is-stable-abi }} |
| 44 | + |
| 45 | +runs: |
| 46 | + using: composite |
| 47 | + steps: |
| 48 | + - name: >- |
| 49 | + Calculate Python interpreter properties version hash value |
| 50 | + for use in the cache key |
| 51 | + id: python-runtime |
| 52 | + run: | |
| 53 | + # Determining the Python runtime features... |
| 54 | +
|
| 55 | + from hashlib import sha512 |
| 56 | + from os import environ |
| 57 | + from sys import version, version_info |
| 58 | +
|
| 59 | + FILE_APPEND_MODE = 'a' |
| 60 | +
|
| 61 | + is_stable_abi = version_info.releaselevel == 'final' |
| 62 | + version_hash = sha512(version.encode()).hexdigest() |
| 63 | +
|
| 64 | + stable_or_unstable = f'{"" if is_stable_abi else "un"}stable' |
| 65 | + restore_key_fallback_prefix = ( |
| 66 | + f'${{ runner.os }}-python-dep-cache-{stable_or_unstable}' |
| 67 | + ) |
| 68 | + restore_key_prefix = f'{restore_key_fallback_prefix}-{version_hash}' |
| 69 | + cache_entry_key = ( |
| 70 | + f'{restore_key_prefix}-{version_hash}-' |
| 71 | + '${{ inputs.cache-key-for-dependency-files }}' |
| 72 | + ) |
| 73 | +
|
| 74 | + print(f'Python ABI is found to be {stable_or_unstable}.') |
| 75 | + print(f'Python version-derived hash is {version_hash}.') |
| 76 | + print(f'The computed cache entry key is {cache_entry_key}.') |
| 77 | +
|
| 78 | + with open( |
| 79 | + environ['GITHUB_OUTPUT'], mode=FILE_APPEND_MODE, |
| 80 | + ) as outputs_file: |
| 81 | + print( |
| 82 | + 'is-stable-abi={is_stable_abi}'. |
| 83 | + format(is_stable_abi=str(is_stable_abi).lower()), |
| 84 | + file=outputs_file, |
| 85 | + ) |
| 86 | + print( |
| 87 | + f'restore-key-fallback-prefix={restore_key_fallback_prefix}', |
| 88 | + file=outputs_file, |
| 89 | + ) |
| 90 | + print( |
| 91 | + f'restore-key-prefix={restore_key_prefix}', |
| 92 | + file=outputs_file, |
| 93 | + ) |
| 94 | + print(f'cache-entry-key={cache_entry_key}', file=outputs_file) |
| 95 | + shell: python |
| 96 | + - name: Get cache dir |
| 97 | + id: cache-dir |
| 98 | + run: > |
| 99 | + # Discovering cache directory... |
| 100 | +
|
| 101 | + echo "dir=$(${{ inputs.cache-directory-lookup-command }})" |
| 102 | + >> "${GITHUB_OUTPUT}" |
| 103 | + shell: bash |
| 104 | + - name: Skip setting up cache |
| 105 | + if: >- |
| 106 | + !fromJSON(steps.python-runtime.outputs.is-stable-abi) |
| 107 | + run: > |
| 108 | + # Skipping cache configuration because due to unstable Python ABI... |
| 109 | +
|
| 110 | +
|
| 111 | + >&2 echo Skipping cache configuration because the current |
| 112 | + Python ABI is unstable... |
| 113 | + shell: bash |
| 114 | + - name: Set up cache |
| 115 | + if: fromJSON(steps.python-runtime.outputs.is-stable-abi) |
| 116 | + uses: actions/cache@v4 |
| 117 | + with: |
| 118 | + path: ${{ steps.cache-dir.outputs.dir }} |
| 119 | + key: >- |
| 120 | + ${{ steps.python-runtime.outputs.cache-entry-key }} |
| 121 | + restore-keys: | |
| 122 | + ${{ steps.python-runtime.outputs.cache-entry-key }} |
| 123 | + ${{ steps.python-runtime.outputs.restore-key-prefix }} |
| 124 | + ${{ steps.python-runtime.outputs.restore-key-fallback-prefix }} |
| 125 | +
|
| 126 | +... |
0 commit comments