Add our own GH cache action

This actions wraps actions/cache and forces an early exit if 'fail-on-cache-miss' is true

Based on the work from https://github.com/saltstack/salt/pull/66240
This commit is contained in:
Pedro Algarvio 2024-03-18 15:49:13 +00:00
parent f0284f75e0
commit 533af2a851
12 changed files with 92 additions and 15 deletions

View file

@ -28,7 +28,7 @@ runs:
- name: Cache Deps Onedir Package Directory
id: onedir-pkg-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: artifacts/${{ inputs.package-name }}
key: >

View file

@ -31,7 +31,7 @@ runs:
- name: Download Cached Deps Onedir Package Directory
id: onedir-bare-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: artifacts/${{ inputs.package-name }}
key: >

77
.github/actions/cache/action.yml vendored Normal file
View file

@ -0,0 +1,77 @@
---
name: cache
description: GitHub Actions Cache
inputs:
path:
description: 'A list of files, directories, and wildcard patterns to cache and restore'
required: true
key:
description: 'An explicit key for restoring and saving the cache'
required: true
restore-keys:
description: 'An ordered list of keys to use for restoring stale cache if no cache hit occurred for key. Note `cache-hit` returns false in this case.'
required: false
upload-chunk-size:
description: 'The chunk size used to split up large files during upload, in bytes'
required: false
enableCrossOsArchive:
description: 'An optional boolean when enabled, allows windows runners to save or restore caches that can be restored or saved respectively on other platforms'
default: 'false'
required: false
fail-on-cache-miss:
description: 'Fail the workflow if cache entry is not found'
default: 'false'
required: false
lookup-only:
description: 'Check if a cache entry exists for the given input(s) (key, restore-keys) without downloading the cache'
default: 'false'
required: false
save-always:
description: 'Run the post step to save the cache even if another step before fails'
default: 'false'
required: false
outputs:
cache-hit:
description: 'A boolean value to indicate an exact match was found for the primary key'
value: ${{ steps.github-cache.outputs.cache-hit }}
runs:
using: composite
steps:
- name: Map inputs to environment variables
shell: bash
run: |
echo "GHA_CACHE_PATH=${{ inputs.path }}" | tee -a "${GITHUB_ENV}"
echo "GHA_CACHE_KEY=${{ inputs.key }}" | tee -a "${GITHUB_ENV}"
echo "GHA_CACHE_ENABLE_CROSS_OS_ARCHIVE=${{ inputs.enableCrossOsArchive }}" | tee -a "${GITHUB_ENV}"
echo "GHA_CACHE_FAIL_ON_CACHE_MISS=${{ inputs.fail-on-cache-miss }}" | tee -a "${GITHUB_ENV}"
echo "GHA_CACHE_LOOKUP_ONLY=${{ inputs.lookup-only }}" | tee -a "${GITHUB_ENV}"
echo "GHA_CACHE_SAVE_ALWAYS=${{ inputs.save-always }}" | tee -a "${GITHUB_ENV}"
echo "GHA_CACHE_RESTORE_KEYS=${{ inputs.restore-keys }}" | tee -a "${GITHUB_ENV}"
echo "GHA_CACHE_UPLOAD_CHUNK_SIZE=${{ inputs.upload-chunk-size }}" | tee -a "${GITHUB_ENV}"
- name: Cache Provided Path (GitHub Actions)
id: github-cache
uses: actions/cache@v4
with:
path: ${{ env.GHA_CACHE_PATH }}
key: ${{ env.GHA_CACHE_KEY }}
enableCrossOsArchive: ${{ env.GHA_CACHE_ENABLE_CROSS_OS_ARCHIVE }}
fail-on-cache-miss: ${{ env.GHA_CACHE_FAIL_ON_CACHE_MISS }}
lookup-only: ${{ env.GHA_CACHE_LOOKUP_ONLY }}
save-always: ${{ env.GHA_CACHE_SAVE_ALWAYS }}
restore-keys: ${{ env.GHA_CACHE_RESTORE_KEYS }}
upload-chunk-size: ${{ env.GHA_CACHE_UPLOAD_CHUNK_SIZE }}
- name: Verify 'fail-on-cache-miss'
if: ${{ inputs.fail-on-cache-miss == 'true' }}
shell: bash
run: |
CACHE_HIT="${{ steps.github-cache.outputs.cache-hit }}"
if [ "$CACHE_HIT" != "true" ]; then
echo "No cache hit and fail-on-cache-miss is set to true."
exit 1
fi

View file

@ -51,7 +51,7 @@ runs:
- name: Cache VirtualEnv
id: cache-virtualenv
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
key: ${{ steps.setup-cache-key.outputs.cache-key }}
path: ${{ steps.virtualenv-path.outputs.venv-path }}

View file

@ -16,7 +16,7 @@ runs:
steps:
- name: Cache actionlint Binary
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: /usr/local/bin/actionlint
key: ${{ inputs.cache-seed }}|${{ runner.os }}|${{ runner.arch }}|actionlint|${{ inputs.version }}

View file

@ -30,7 +30,7 @@ runs:
${{ steps.pre-commit-virtualenv.outputs.python-executable }} -m pip install pre-commit==${{ inputs.version }}
- name: Cache Pre-Commit Hooks
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
id: pre-commit-hooks-cache
with:
key: ${{ steps.pre-commit-virtualenv.outputs.cache-key }}|${{ inputs.version }}|${{ hashFiles('.pre-commit-config.yaml') }}

View file

@ -44,7 +44,7 @@ runs:
cache-seed: tools|${{ steps.venv-hash.outputs.venv-hash }}
- name: Restore Python Tools Virtualenvs Cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: ${{ inputs.cwd }}/.tools-venvs
key: ${{ inputs.cache-prefix }}|${{ steps.venv-hash.outputs.venv-hash }}

View file

@ -37,7 +37,7 @@ runs:
python3 -m pip install relenv==${{ inputs.version }}
- name: Cache Relenv Data Directory
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: ${{ github.workspace }}/.relenv
key: ${{ inputs.cache-seed }}|relenv|${{ inputs.version }}|${{ inputs.python-version }}|${{ inputs.platform }}|${{ inputs.arch }}

View file

@ -16,7 +16,7 @@ runs:
steps:
- name: Cache shellcheck Binary
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: /usr/local/bin/shellcheck
key: ${{ inputs.cache-seed }}|${{ runner.os }}|${{ runner.arch }}|shellcheck|${{ inputs.version }}

View file

@ -74,7 +74,7 @@ jobs:
- name: Cache nox.linux.${{ matrix.arch }}.tar.* for session ${{ inputs.nox-session }}
id: nox-dependencies-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: nox.linux.${{ matrix.arch }}.tar.*
key: ${{ inputs.cache-prefix }}|testrun-deps|${{ matrix.arch }}|linux|${{ inputs.nox-session }}|${{ inputs.python-version }}|${{ inputs.nox-archive-hash }}
@ -183,7 +183,7 @@ jobs:
- name: Cache nox.macos.${{ matrix.arch }}.tar.* for session ${{ inputs.nox-session }}
id: nox-dependencies-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: nox.macos.${{ matrix.arch }}.tar.*
key: ${{ inputs.cache-prefix }}|testrun-deps|${{ matrix.arch }}|macos|${{ inputs.nox-session }}|${{ inputs.python-version }}|${{ inputs.nox-archive-hash }}
@ -272,7 +272,7 @@ jobs:
- name: Cache nox.windows.${{ matrix.arch }}.tar.* for session ${{ inputs.nox-session }}
id: nox-dependencies-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: nox.windows.${{ matrix.arch }}.tar.*
key: ${{ inputs.cache-prefix }}|testrun-deps|${{ matrix.arch }}|windows|${{ inputs.nox-session }}|${{ inputs.python-version }}|${{ inputs.nox-archive-hash }}

View file

@ -69,7 +69,7 @@ jobs:
- name: Cache Python Tools Docs Virtualenv
id: tools-venvs-dependencies-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: .tools-venvs/docs
key: ${{ inputs.cache-seed }}|${{ github.workflow }}|${{ github.job }}|tools-venvs|${{ steps.python-tools-scripts.outputs.version }}|docs|${{ steps.get-python-version.outputs.version }}|${{ hashFiles('requirements/**/docs.txt') }}

View file

@ -74,7 +74,7 @@ jobs:
- name: Cache nox.linux.${{ matrix.arch }}.tar.* for session ${{ inputs.nox-session }}
id: nox-dependencies-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: nox.linux.${{ matrix.arch }}.tar.*
key: ${{ inputs.cache-prefix }}|testrun-deps|${{ matrix.arch }}|linux|${{ inputs.nox-session }}|${{ inputs.python-version }}|${{ inputs.nox-archive-hash }}
@ -183,7 +183,7 @@ jobs:
- name: Cache nox.macos.${{ matrix.arch }}.tar.* for session ${{ inputs.nox-session }}
id: nox-dependencies-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: nox.macos.${{ matrix.arch }}.tar.*
key: ${{ inputs.cache-prefix }}|testrun-deps|${{ matrix.arch }}|macos|${{ inputs.nox-session }}|${{ inputs.python-version }}|${{ inputs.nox-archive-hash }}
@ -274,7 +274,7 @@ jobs:
- name: Cache nox.windows.${{ matrix.arch }}.tar.* for session ${{ inputs.nox-session }}
id: nox-dependencies-cache
uses: actions/cache@v3.3.1
uses: ./.github/actions/cache
with:
path: nox.windows.${{ matrix.arch }}.tar.*
key: ${{ inputs.cache-prefix }}|testrun-deps|${{ matrix.arch }}|windows|${{ inputs.nox-session }}|${{ inputs.python-version }}|${{ inputs.nox-archive-hash }}