cache-apt-pkgs-action/pre_cache_action.sh

61 lines
1.5 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# Directory that holds the cached packages.
2022-03-18 22:24:55 -07:00
cache_dir=$1
# Version of the cache to create or load.
2022-03-18 22:24:55 -07:00
version=$2
# List of the packages to use.
2022-03-18 22:24:55 -07:00
packages=${@:3}
2021-10-21 21:12:42 -07:00
# Create cache directory so artifacts can be saved.
2022-03-18 22:24:55 -07:00
mkdir -p $cache_dir
2021-10-21 21:12:42 -07:00
2022-03-18 22:24:55 -07:00
echo -n "Validating action arguments (version='$version', packages='$packages')...";
echo $version | grep -o " " > /dev/null
if [ $? -eq 0 ]; then
echo "aborted."
2022-03-18 22:24:55 -07:00
echo "Version value '$version' cannot contain spaces." >&2
exit 1
fi
2022-03-18 22:24:55 -07:00
if [ "$packages" == "" ]; then
echo "aborted."
echo "Packages argument cannot be empty." >&2
exit 2
fi
echo "done."
echo -n "Verifying packages..."
2022-03-18 22:24:55 -07:00
for package in $packages; do
escaped=$(echo $package | sed 's/+/\\+/g')
apt-cache search ^$escaped$ | grep $package > /dev/null
if [ $? -ne 0 ]; then
echo "aborted."
2022-03-18 22:24:55 -07:00
echo "Package '$package' not found." >&2
exit 3
fi
done
echo "done."
# Abort on any failure at this point.
set -e
echo "Creating cache key..."
2022-03-18 22:24:55 -07:00
# 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'."
2022-03-18 22:24:55 -07:00
value=$(echo $normalized_list @ $version)
echo "- Value to hash is '$value'."
key=$(echo $value | md5sum | cut -f1 -d' ')
echo "- Value hashed as '$key'."
echo "done."
2022-03-18 22:24:55 -07:00
key_filepath="$cache_dir/cache_key.md5"
echo $key > $key_filepath
echo "Hash value written to $key_filepath"