Validate version and break out cache key creation logic.

This commit is contained in:
awalsh128 2021-10-16 22:37:18 -07:00
parent 3dc9d0c3ae
commit 82c659a794
4 changed files with 39 additions and 7 deletions

View file

@ -18,6 +18,7 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
### Inputs
* `packages` - Space delimited list of packages to install.
* `version` - Version of cache to load. Each version will have its own cache. Note, all characters except spaces are allowed.
### Outputs
@ -44,6 +45,7 @@ jobs:
- uses: awalsh128/cache-apt-pkgs-action@v1
with:
packages: dia doxygen doxygen-doc doxygen-gui doxygen-latex graphviz mscgen
version: 1.0
- name: Build
run: |

View file

@ -26,14 +26,12 @@ runs:
using: "composite"
steps:
- name: Validate Packages
run: ${{ github.action_path }}/validate_pkgs.sh ${{ inputs.packages }}
run: ${{ github.action_path }}/validate_pkgs.sh "${{ inputs.version }}" ${{ inputs.packages }}
shell: bash
- name: Create Cache Key
run: |
normalized_list=$(echo ${{ inputs.packages }} | sed 's/[\s,]+/ /g' | sort)
value=$(echo $normalized_list @ ${{ inputs.version }})
echo "CACHE_KEY=$(echo $value | md5sum | /bin/cut -f1 -d' ')" >> $GITHUB_ENV
${{ github.action_path }}/create_cache_key.sh "${{ inputs.version }})"" ${{ inputs.packages }} >> $GITHUB_ENV
shell: bash
- name: Load Package Cache

21
create_cache_key.sh Executable file
View file

@ -0,0 +1,21 @@
#!/bin/bash
# Fail on any error.
set -e
version=$1
packages=${@:2}
echo "* Creating cache key..."
# Remove package delimiters, sort (requires newline) and then convert back to inline list.
normalized_list=$(echo $packages | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ')
echo "* Normalized package list is '$normalized_list'."
value=$(echo $normalized_list @ $version)
echo "* Value to hash is '$value'."
key=$(echo $value | md5sum | /bin/cut -f1 -d' ')
echo "* Value hashed as '$key'."
echo "CACHE_KEY=$key"

View file

@ -3,20 +3,31 @@
# Fail on any error.
set -e
packages=$1
version=$1
packages=${@:2}
echo -n "* Validating action arguments... ";
echo $version | grep -o " " > /dev/null
if [ $? -ne 0 ]; then
echo "aborted."
echo "* Version value '$version' cannot contain spaces." >&2
exit 1
fi
if [ "$packages" == "" ]; then
echo "aborted."
echo "* Packages argument cannot be empty." >&2
exit 1
exit 2
fi
for package in $packages; do
apt-cache search ^$package$ | grep $package > /dev/null
if [ $? -ne 0 ]; then
echo "aborted."
echo "* Package '$package' not found." >&2
exit 2
exit 3
fi
done
echo "done."