Replace Checkout action with Cache action for build file caching
Convert the composite action from Checkout to Cache that: - Stores build files under ~/.cache/ by default - Auto-generates cache keys from lockfile hashes - Uses actions/cache@v5 for restore and save - Supports custom path, key, key-file, and restore-keys inputs - Exposes cache-hit output for conditional workflow steps
This commit is contained in:
+48
@@ -0,0 +1,48 @@
|
||||
name: 'Cache'
|
||||
description: 'Cache build files under ~/.cache/ using a key file hash'
|
||||
inputs:
|
||||
path:
|
||||
description: 'A list of files, directories, or wildcard patterns to cache. Defaults to ~/.cache/'
|
||||
required: false
|
||||
default: '~/.cache/'
|
||||
key:
|
||||
description: 'An explicit key for a cache entry. Defaults to runner.os-cache-{hash}'
|
||||
required: false
|
||||
key-file:
|
||||
description: 'The file to hash for generating the cache key. Used if key is not provided.'
|
||||
required: false
|
||||
default: ''
|
||||
restore-keys:
|
||||
description: 'An ordered list of prefix-matched keys for restoring stale cache'
|
||||
required: false
|
||||
default: ''
|
||||
outputs:
|
||||
cache-hit:
|
||||
description: 'A boolean value to indicate an exact match was found for the key'
|
||||
value: ${{ steps.restore-cache.outputs.cache-hit }}
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- name: Generate cache key
|
||||
id: gen-key
|
||||
if: inputs.key == ''
|
||||
shell: bash
|
||||
run: |
|
||||
if [ -n "${{ inputs.key-file }}" ]; then
|
||||
KEY_HASH=$(sha256sum "${{ inputs.key-file }}" | awk '{print $1}')
|
||||
else
|
||||
KEY_HASH=$(find . -type f \( -name "*.csproj" -o -name "*.sln" -o -name "package.json" -o -name "package-lock.json" -o -name "yarn.lock" -o -name "pnpm-lock.yaml" -o -name "Cargo.lock" -o -name "requirements.txt" -o -name "Pipfile.lock" -o -name "go.mod" -o -name "go.sum" -o -name "pom.xml" -o -name "build.gradle*" -o -name "Gemfile.lock" -o -name "composer.lock" \) -exec sha256sum {} + 2>/dev/null | sha256sum | awk '{print $1}')
|
||||
if [ "$KEY_HASH" = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ]; then
|
||||
KEY_HASH=$(find . -type f -not -path './.git/*' -not -path './.cache/*' | sort | xargs sha256sum 2>/dev/null | sha256sum | awk '{print $1}')
|
||||
fi
|
||||
fi
|
||||
echo "cache-key=${{ runner.os }}-cache-${KEY_HASH}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Restore cache
|
||||
id: restore-cache
|
||||
uses: actions/cache@v5
|
||||
with:
|
||||
path: ${{ inputs.path }}
|
||||
key: ${{ inputs.key != '' && inputs.key || steps.gen-key.outputs.cache-key }}
|
||||
restore-keys: ${{ inputs.restore-keys != '' && inputs.restore-keys || format('{0}-cache-', runner.os) }}
|
||||
|
||||
Reference in New Issue
Block a user