mirror of
https://github.com/awalsh128/cache-apt-pkgs-action.git
synced 2024-12-22 18:55:30 +00:00
Merge pull request #9 from awalsh128/staging
Merge from staging with new features.
This commit is contained in:
commit
4d3d45c02f
|
@ -20,12 +20,11 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
|
||||||
|
|
||||||
* `packages` - Space delimited list of packages to install.
|
* `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.
|
* `version` - Version of cache to load. Each version will have its own cache. Note, all characters except spaces are allowed.
|
||||||
* `refresh` - Refresh / upgrade the packages in the same cache.
|
|
||||||
|
|
||||||
### 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
|
||||||
|
|
42
action.yml
42
action.yml
|
@ -25,44 +25,34 @@ 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 }}/post_cache_action.sh \
|
||||||
${{ github.action_path }}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${{ inputs.packages }}
|
~/cache-apt-pkgs \
|
||||||
else
|
/ \
|
||||||
${{ github.action_path }}/restore_pkgs.sh ~/cache-apt-pkgs /
|
"${{ steps.load-cache.outputs.cache-hit }}" \
|
||||||
fi
|
${{ inputs.packages }}
|
||||||
shell: bash
|
echo "::set-output name=package-version-list::$(cat ~/cache-apt-pkgs/manifest.log)"
|
||||||
|
|
||||||
- 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
|
||||||
|
|
|
@ -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
|
|
|
@ -5,39 +5,47 @@ 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)."
|
||||||
|
echo "Package list:"
|
||||||
for package in $packages; do
|
for package in $packages; do
|
||||||
echo "::debug::- $package"
|
echo "- $package"
|
||||||
done
|
done
|
||||||
echo "::endgroup::"
|
|
||||||
|
|
||||||
mkdir -p $cache_dir
|
echo -n "Updating APT package list..."
|
||||||
|
sudo apt-get update > /dev/null
|
||||||
echo "::group::Update APT Package List"
|
echo "done."
|
||||||
sudo apt-get update
|
|
||||||
echo "::endgroup::"
|
|
||||||
|
|
||||||
|
echo "Clean installing and caching $(echo $packages | wc -w) 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 "::group::Clean install $package"
|
echo "- $package"
|
||||||
sudo apt-get --yes install $package
|
echo -n " Installing..."
|
||||||
echo "::endgroup::"
|
sudo apt-get --yes install $package > /dev/null
|
||||||
|
echo "done."
|
||||||
|
|
||||||
echo "::group::Caching $package to $cache_filepath"
|
echo -n " Caching 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 "done."
|
||||||
|
|
||||||
echo "::group::Finished"
|
manifest_filepath="$cache_dir/manifest.log"
|
||||||
echo "::debug::$(echo $packages | wc -w) package(s) installed and cached."
|
echo -n "Writing package manifest to $manifest_filepath..."
|
||||||
echo "::endgroup::"
|
manifest=
|
||||||
|
for package in $packages; do
|
||||||
|
manifest=$manifest$package:$(dpkg -s $package | grep Version | awk '{print $2}'),
|
||||||
|
done
|
||||||
|
# Remove trailing comma.
|
||||||
|
echo ${manifest:0:-1} > $manifest_filepath
|
||||||
|
echo "done."
|
26
post_cache_action.sh
Executable file
26
post_cache_action.sh
Executable file
|
@ -0,0 +1,26 @@
|
||||||
|
#!/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
|
||||||
|
|
||||||
|
# List of the packages to use.
|
||||||
|
packages="${@:4}"
|
||||||
|
|
||||||
|
script_dir=$(dirname $0)
|
||||||
|
|
||||||
|
if [ "$cache_hit" == true ]; then
|
||||||
|
$script_dir/restore_pkgs.sh ~/cache-apt-pkgs $cache_restore_root
|
||||||
|
else
|
||||||
|
$script_dir/install_and_cache_pkgs.sh ~/cache-apt-pkgs $packages
|
||||||
|
fi
|
||||||
|
echo ""
|
59
pre_cache_action.sh
Executable file
59
pre_cache_action.sh
Executable file
|
@ -0,0 +1,59 @@
|
||||||
|
#!/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}
|
||||||
|
|
||||||
|
# Create cache directory so artifacts can be saved.
|
||||||
|
mkdir -p $cache_dir
|
||||||
|
|
||||||
|
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"
|
|
@ -5,29 +5,30 @@ 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.
|
|
||||||
packages="${@:3}"
|
|
||||||
|
|
||||||
cache_filenames=$(ls -1 $cache_dir | sort)
|
cache_filepaths=$(ls -1 $cache_dir | sort)
|
||||||
cache_filename_count=$(echo $cache_filenames | wc -w)
|
echo "Found $(echo $cache_filepaths | wc -w) files in the cache."
|
||||||
|
for cache_filepath in $cache_filepaths; do
|
||||||
echo "::group::Found $cache_filename_count files in cache."
|
echo "- $(basename $cache_filepath)"
|
||||||
for cache_filename in $cache_filenames; do
|
|
||||||
echo "::debug::$cache_filename"
|
|
||||||
done
|
done
|
||||||
echo "::endgroup::"
|
|
||||||
|
|
||||||
echo "::group::Package Restore"
|
echo "Reading from manifest..."
|
||||||
for package in $packages; do
|
for logline in $(cat $cache_dir/manifest.log | tr ',' '\n' ); do
|
||||||
cache_filepath=$cache_dir/$package.tar.gz
|
echo "- $(echo $logline | tr ':' ' ')"
|
||||||
echo "::debug::Restoring package $package ($cache_filepath) from cache... "
|
|
||||||
sudo tar -xf $cache_filepath -C $cache_restore_root
|
|
||||||
done
|
done
|
||||||
echo "::endgroup::"
|
echo "done."
|
||||||
|
|
||||||
echo "::group::Finished"
|
# Only search for archived results. Manifest and cache key also live here.
|
||||||
echo "::debug::Action complete. $cache_filename_count package(s) restored."
|
cache_pkg_filepaths=$(ls -1 $cache_dir/*.tar.gz | sort)
|
||||||
echo "::endgroup::"
|
cache_pkg_filecount=$(echo $cache_pkg_filepaths | wc -w)
|
||||||
|
echo "Restoring $cache_pkg_filecount packages from cache..."
|
||||||
|
for cache_pkg_filepath in $cache_pkg_filepaths; do
|
||||||
|
echo -n "- $(basename $cache_pkg_filepath) restoring..."
|
||||||
|
sudo tar -xf $cache_pkg_filepath -C $cache_restore_root > /dev/null
|
||||||
|
echo "done."
|
||||||
|
done
|
||||||
|
echo "done."
|
||||||
|
|
|
@ -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::"
|
|
Loading…
Reference in a new issue