From 82c659a794224d4e7bb2dfb8a2aa3072f4de5114 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Sat, 16 Oct 2021 22:37:18 -0700 Subject: [PATCH] Validate version and break out cache key creation logic. --- README.md | 2 ++ action.yml | 6 ++---- create_cache_key.sh | 21 +++++++++++++++++++++ validate_pkgs.sh | 17 ++++++++++++++--- 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100755 create_cache_key.sh diff --git a/README.md b/README.md index b1762f5..e03856e 100644 --- a/README.md +++ b/README.md @@ -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: | diff --git a/action.yml b/action.yml index dfa60e5..f53bcd4 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/create_cache_key.sh b/create_cache_key.sh new file mode 100755 index 0000000..679c672 --- /dev/null +++ b/create_cache_key.sh @@ -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" \ No newline at end of file diff --git a/validate_pkgs.sh b/validate_pkgs.sh index 3b63a70..34c9830 100755 --- a/validate_pkgs.sh +++ b/validate_pkgs.sh @@ -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."