Consolidate steps, and make reporting less verbose.

This commit is contained in:
awalsh128 2021-10-21 20:57:52 -07:00
parent 89dc8d2e9e
commit e5a9854ee1
8 changed files with 132 additions and 106 deletions

View file

@ -25,7 +25,7 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
### Outputs ### Outputs
* `cache-hit` - A boolean value to indicate a cache was found for the packages requested. * `cache-hit` - A boolean value to indicate a cache was found for the packages requested.
* `package_version_list` - The packages and versions that are installed as a comma delimited list with colon delimit on the package version (i.e. \<package1>:<version1\>,\<package2>:\<version2>,...). * `package-version-list` - The packages and versions that are installed as a comma delimited list with colon delimit on the package version (i.e. \<package1>:<version1\>,\<package2>:\<version2>,...).
### Cache scopes ### Cache scopes

View file

@ -25,44 +25,27 @@ outputs:
# This compound expression is needed because lhs can be empty. # This compound expression is needed because lhs can be empty.
# Need to output true and false instead of true and nothing. # Need to output true and false instead of true and nothing.
value: ${{ steps.load-cache.outputs.cache-hit || false }} value: ${{ steps.load-cache.outputs.cache-hit || false }}
package_version_list: package-version-list:
description: 'The packages and versions that are installed as a comma delimited list with colon delimit on the package version (i.e. <package>:<version,<package>:<version>).' description: 'The packages and versions that are installed as a comma delimited list with colon delimit on the package version (i.e. <package>:<version,<package>:<version>).'
value: ${{ steps.get-package-versions.outputs.package_version_list }} value: ${{ steps.post-cache.outputs.package-version-list }}
runs: runs:
using: "composite" using: "composite"
steps: steps:
- name: Validate Packages - id: pre-cache
run: ${{ github.action_path }}/validate_pkgs.sh "${{ inputs.version }}" ${{ inputs.packages }}
shell: bash
- name: Create Cache Key
run: | run: |
${{ github.action_path }}/create_cache_key.sh "${{ inputs.version }})" ${{ inputs.packages }} ${{ github.action_path }}/pre_cache_action.sh ~/cache-apt-pkgs "${{ inputs.version }}" ${{ inputs.packages }}
echo "CACHE_KEY=$(cat ~/cache-apt-pkgs/cache_key.md5)" >> $GITHUB_ENV
shell: bash shell: bash
- name: Load Package Cache - id: load-cache
id: load-cache
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
path: ~/cache-apt-pkgs path: ~/cache-apt-pkgs
key: cache-apt-pkgs_${{ env.CACHE_KEY }} key: cache-apt-pkgs_${{ env.CACHE_KEY }}
- name: Load Packages - id: post-cache
run: | run: |
if [ ! ${{ steps.load-cache.outputs.cache-hit }} ] || [ ${{ inputs.refresh }} ]; then ${{ github.action_path }}/pre_cache_action.sh ~/cache-apt-pkgs "${{ inputs.version }}" ${{ inputs.packages }}
${{ github.action_path }}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${{ inputs.packages }} echo "::set-output name=package-version-list::$(cat ~/cache-apt-pkgs/manifest.log)"
else
${{ github.action_path }}/restore_pkgs.sh ~/cache-apt-pkgs /
fi
shell: bash
- name: Get Package Versions
id: get-package-versions
run: |
output=
for package in ${{ inputs.packages }}; do
output=$output,$package:$(dpkg -s $package | grep Version | awk '{print $2}')
done
echo '::set-output name=package_version_list::output'
shell: bash shell: bash

View file

@ -1,23 +0,0 @@
#!/bin/bash
# Fail on any error.
set -e
version=$1
packages=${@:2}
echo "::group::Create 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 "::debug::Normalized package list is '$normalized_list'."
value=$(echo $normalized_list @ $version)
echo "::debug::Value to hash is '$value'."
key=$(echo $value | md5sum | /bin/cut -f1 -d' ')
echo "::debug::Value hashed as '$key'."
echo "::endgroup::"
echo "CACHE_KEY=$key" >> $GITHUB_ENV

View file

@ -5,39 +5,39 @@ set -e
# Directory that holds the cached packages. # Directory that holds the cached packages.
cache_dir=$1 cache_dir=$1
# List of the packages to use. # List of the packages to use.
packages="${@:2}" packages="${@:2}"
package_count=$(echo $packages | wc -w) package_count=$(echo $packages | wc -w)
echo "::debug::Clean installing $package_count packages..." echo "Clean installing and caching $package_count package(s).\n"
echo "Package list:"
for package in $packages; do for package in $packages; do
echo "::debug::- $package" echo "- $package"
done done
echo "::endgroup::" echo ""
mkdir -p $cache_dir mkdir -p $cache_dir
echo "::group::Update APT Package List" echo -n "Updating APT package list..."
sudo apt-get update sudo apt-get update > /dev/null
echo "::endgroup::" echo "done."
for package in $packages; do for package in $packages; do
cache_filepath=$cache_dir/$package.tar.gz cache_filepath=$cache_dir/$package.tar.gz
echo "::group::Clean install $package" echo -n "Clean installing $package..."
sudo apt-get --yes install $package sudo apt-get --yes install $package > /dev/null
echo "::endgroup::" echo "done."
echo "::group::Caching $package to $cache_filepath" echo -n "Caching $package to $cache_filepath..."
# Pipe all package files (no folders) to Tar. # Pipe all package files (no folders) to Tar.
dpkg -L $package | dpkg -L $package |
while IFS= read -r f; do while IFS= read -r f; do
if test -f $f; then echo $f; fi; if test -f $f; then echo ${f:1}; fi; #${f:1} removes the leading slash that Tar disallows
done | done |
xargs tar -czf $cache_filepath -C / xargs tar -czf $cache_filepath -C /
echo "::endgroup::" echo "done."
done done
echo "::group::Finished" echo "$(echo $packages | wc -w) package(s) installed and cached."
echo "::debug::$(echo $packages | wc -w) package(s) installed and cached."
echo "::endgroup::"

41
post_cache_action.sh Executable file
View file

@ -0,0 +1,41 @@
#!/bin/bash
# Fail on any error.
set -e
# Directory that holds the cached packages.
cache_dir=$1
# Root directory to untar the cached packages to.
# Typically filesystem root '/' but can be changed for testing.
cache_restore_root=$2
# Indicates that the cache was found.
cache_hit=$3
# Indicates that a refresh of the packages in the cache is required.
refresh=$4
# List of the packages to use.
packages="${@:5}"
if [ ! $cache_hit ] || [ $refresh ]; then
./install_and_cache_pkgs.sh ~/cache-apt-pkgs $packages
else
./restore_pkgs.sh ~/cache-apt-pkgs $cache_restore_root
fi
echo ""
echo "Creating package manifest..."
manifest=
for package in $packages; do
item=$package:$(dpkg -s $package | grep Version | awk '{print $2}')
echo "- $item"
manifest=$manifest$item,
done
# Remove trailing comma.
manifest=${manifest:0:-1}
manifest_filepath="$cache_dir/manifest.log"
echo $manifest > $manifest_filepath
echo "Manifest written to $manifest_filepath"

56
pre_cache_action.sh Executable file
View file

@ -0,0 +1,56 @@
#!/bin/bash
# Directory that holds the cached packages.
cache_dir=$1
# Version of the cache to create or load.
version=$2
# List of the packages to use.
packages=${@:3}
echo -n "Validating action arguments (version='$version', packages='$packages')...";
echo $version | grep -o " " > /dev/null
if [ $? -eq 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 2
fi
echo "done."
echo -n "Verifying packages..."
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 3
fi
done
echo "done."
# Abort on any failure at this point.
set -e
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 "done."
key_filepath="$cache_dir/cache_key.md5"
echo $key > $key_filepath
echo "Hash value written to $key_filepath"

View file

@ -5,29 +5,27 @@ set -e
# Directory that holds the cached packages. # Directory that holds the cached packages.
cache_dir=$1 cache_dir=$1
# Root directory to untar the cached packages to. # Root directory to untar the cached packages to.
# Typically filesystem root '/' but can be changed for testing. # Typically filesystem root '/' but can be changed for testing.
cache_restore_root=$2 cache_restore_root=$2
# List of the packages to use. # List of the packages to use.
packages="${@:3}" packages="${@:3}"
cache_filenames=$(ls -1 $cache_dir | sort) cache_filenames=$(ls -1 $cache_dir | sort)
cache_filename_count=$(echo $cache_filenames | wc -w) cache_filename_count=$(echo $cache_filenames | wc -w)
echo "::group::Found $cache_filename_count files in cache." echo "Found $cache_filename_count packages in cache."
for cache_filename in $cache_filenames; do for cache_filename in $cache_filenames; do
echo "::debug::$cache_filename" echo "- $cache_filename"
done done
echo "::endgroup::"
echo "::group::Package Restore" echo "Restoring cached packages..."
for package in $packages; do for package in $packages; do
cache_filepath=$cache_dir/$package.tar.gz cache_filepath=$cache_dir/$package.tar.gz
echo "::debug::Restoring package $package ($cache_filepath) from cache... " echo "- $package ($cache_filepath)"
sudo tar -xf $cache_filepath -C $cache_restore_root sudo tar -xf $cache_filepath -C $cache_restore_root > /dev/null
done done
echo "::endgroup::"
echo "::group::Finished" echo "$cache_filename_count package(s) restored."
echo "::debug::Action complete. $cache_filename_count package(s) restored."
echo "::endgroup::"

View file

@ -1,29 +0,0 @@
#!/bin/bash
version=$1
packages=${@:2}
echo "::group::Validate Action Arguments";
echo $version | grep -o " " > /dev/null
if [ $? -eq 0 ]; then
echo "::error::Aborted. Version value '$version' cannot contain spaces." >&2
exit 1
fi
echo "::debug::Version '$version' is valid."
if [ "$packages" == "" ]; then
echo "::error::Aborted. Packages argument cannot be empty." >&2
exit 2
fi
for package in $packages; do
apt-cache search ^$package$ | grep $package > /dev/null
if [ $? -ne 0 ]; then
echo "::error::Aborted. Package '$package' not found." >&2
exit 3
fi
done
echo "::debug::Packages '$packages' are valid."
echo "::endgroup::"