Skip to content

Instantly share code, notes, and snippets.

@zen0wu
Created May 28, 2021 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zen0wu/a0a7cd95fe3f2f550467c4428ef0f87c to your computer and use it in GitHub Desktop.
Save zen0wu/a0a7cd95fe3f2f550467c4428ef0f87c to your computer and use it in GitHub Desktop.
Custom composite cache actions on github actions
name: Cache (v2)
description: >
Cache files onto S3 bucket. It has 3 steps:
1. It tries to restore the cache using the given key.
If the caches hits, exit.
2. It runs the given script using shell, if the cache missed in step 1.
3. It saves to the cache after the script.
inputs:
s3-bucket:
description: The bucket name.
required: true
key:
description: An explicit key for the cached files.
required: true
paths:
description: >
List of files/directories to cache. Separated by new line.
Use relative path to the project root.
required: true
run:
description: Script to run to generate the cached files.
required: true
outputs:
hit:
description: A boolean value to indicate whether there is a cache hit.
value: ${{ steps.restore-cache.outputs.hit }}
runs:
using: composite
steps:
- name: Restore cache
id: restore-cache
run: |
set +x
echo "Checking cache: $BUCKET_KEY"
CACHE_HIT=$(aws --debug s3api head-object --bucket "$BUCKET" --key "$BUCKET_KEY" &> /dev/null; echo $?)
if [ "$CACHE_HIT" -ne 0 ]; then
echo "::set-output name=hit::false"
else
echo "Restoring cache: $BUCKET_KEY"
aws s3 cp "s3://$BUCKET/$BUCKET_KEY" - | tar -I zstd -x
echo "::set-output name=hit::true"
fi
shell: bash
env:
BUCKET: "${{ inputs.s3-bucket }}"
BUCKET_KEY: "cache/${{ inputs.key }}.tar.z"
- name: Run
run: |
set +x
if [ "${{ steps.restore-cache.outputs.hit }}" = "true" ]; then
exit 0
fi
${{ inputs.run }}
shell: bash
- name: Save cache
run: |
set +x
if [ "${{ steps.restore-cache.outputs.hit }}" = "true" ]; then
exit 0
fi
echo "Saving cache: ${{ inputs.paths }} -> $BUCKET_KEY"
echo "${{ inputs.paths }}" | xargs tar -I zstd -c | aws s3 cp - "s3://$BUCKET/$BUCKET_KEY"
shell: bash
env:
BUCKET: "${{ inputs.s3-bucket }}"
BUCKET_KEY: "cache/${{ inputs.key }}.tar.z"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment