Merge pull request #7 from awalsh128/staging

Merge from staging.
This commit is contained in:
Andrew Walsh 2021-10-16 22:53:54 -07:00 committed by GitHub
commit d6265bde5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 22 deletions

View file

@ -18,6 +18,7 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
### Inputs ### Inputs
* `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.
### Outputs ### Outputs
@ -44,6 +45,7 @@ jobs:
- uses: awalsh128/cache-apt-pkgs-action@v1 - uses: awalsh128/cache-apt-pkgs-action@v1
with: with:
packages: dia doxygen doxygen-doc doxygen-gui doxygen-latex graphviz mscgen packages: dia doxygen doxygen-doc doxygen-gui doxygen-latex graphviz mscgen
version: 1.0
- name: Build - name: Build
run: | run: |

View file

@ -10,36 +10,42 @@ inputs:
description: 'Space delimited list of packages to install.' description: 'Space delimited list of packages to install.'
required: true required: true
default: '' default: ''
version:
description: 'Version will create a new cache and install packages.'
required: false
default: ''
outputs: outputs:
cache-hit: cache-hit:
description: 'A boolean value to indicate a cache was found for the packages requested.' description: 'A boolean value to indicate a cache was found for the packages requested.'
value: ${{ steps.load-pkg-cache.outputs.cache-hit }} # This compound expression is needed because lhs can be empty.
# Need to output true and false instead of true and nothing.
value: ${{ steps.load-cache.outputs.cache-hit || false }}
runs: runs:
using: "composite" using: "composite"
steps: steps:
- name: Validate Packages - 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 shell: bash
- name: Create Cache Key - name: Create Cache Key
run: echo ${{ inputs.packages }} | sed 's/[\s,]+/\n/g' | sort > /tmp/package_list.txt run: |
${{ github.action_path }}/create_cache_key.sh "${{ inputs.version }})" ${{ inputs.packages }}
shell: bash shell: bash
- name: Load Package Cache - name: Load Package Cache
id: load-pkg-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_${{ hashFiles('/tmp/package_list.txt') }} key: cache-apt-pkgs_${{ env.CACHE_KEY }}
- name: Install and Cache Packages - name: Load Packages
if: steps.load-pkg-cache.outputs.cache-hit != 'true' run: |
run: ${{ github.action_path }}/install_and_cache.sh ~/cache-apt-pkgs ${{ inputs.packages }} if [ ${{ steps.load-cache.outputs.cache-hit }} ]; then
shell: bash ${{ github.action_path }}/restore_pkgs.sh ~/cache-apt-pkgs /
else
- name: Restore Cached Packages ${{ github.action_path }}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${{ inputs.packages }}
if: steps.load-pkg-cache.outputs.cache-hit != 'true' fi
run: ${{ github.action_path }}/run.sh ~/cache-apt-pkgs /
shell: bash shell: bash

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" >> $GITHUB_ENV

View file

@ -1,10 +1,19 @@
#!/bin/bash #!/bin/bash
# Fail on any error.
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)
echo "* Clean installing $package_count packages..."
for package in $packages; do
echo " - $package"
done
mkdir -p $cache_dir mkdir -p $cache_dir
for package in $packages; do for package in $packages; do
cache_filepath=$cache_dir/$package.tar.gz cache_filepath=$cache_dir/$package.tar.gz
@ -19,6 +28,6 @@ for package in $packages; do
if test -f $f; then echo $f; fi; if test -f $f; then echo $f; fi;
done | done |
xargs tar -czf $cache_filepath -C / xargs tar -czf $cache_filepath -C /
fi done
echo "Action complete. ${#packages[@]} package(s) installed and cached." echo "Action complete. $(echo $packages | wc -w) package(s) installed and cached."

View file

@ -1,15 +1,30 @@
#!/bin/bash #!/bin/bash
# Fail on any error.
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}"
for cache_filepath in $(ls $cache_dir); do cache_filenames=$(ls -1 $cache_dir | sort)
echo "* Restoring $package from cache $cache_filepath... " cache_filename_count=$(echo $cache_filenames | wc -w)
echo "* Found $cache_filename_count files in cache..."
for cache_filename in $cache_filenames; do
echo " - $cache_filename"
done
for package in $packages; do
cache_filepath=$cache_dir/$package.tar.gz
echo "* Restoring package $package ($cache_filepath) from cache... "
sudo tar -xf $cache_filepath -C $cache_restore_root sudo tar -xf $cache_filepath -C $cache_restore_root
# Upgrade the install from last state.
# TODO(awalsh128) Add versioning to cache key creation.
sudo apt-get --yes --only-upgrade install $package sudo apt-get --yes --only-upgrade install $package
fi done
echo "Action complete. ${#packages[@]} package(s) restored." echo "Action complete. $cache_filename_count package(s) restored."

View file

@ -1,19 +1,30 @@
#!/bin/bash #!/bin/bash
packages=$1 version=$1
packages=${@:2}
echo -n "* Validating action arguments... "; echo -n "* Validating action arguments... ";
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 if [ "$packages" == "" ]; then
echo "aborted." echo "aborted."
echo "* Packages argument cannot be empty." >&2 echo "* Packages argument cannot be empty." >&2
exit 1 exit 2
fi fi
for package in $packages; do for package in $packages; do
apt-cache search ^$package$ | grep $package > /dev/null apt-cache search ^$package$ | grep $package > /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "aborted." echo "aborted."
echo "* Package '$package' not found." >&2 echo "* Package '$package' not found." >&2
exit 2 exit 3
fi fi
done done
echo "done." echo "done."