From b48a00753bbc894603a4e6dfef2ac8911931c08c Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Sun, 19 Sep 2021 12:38:54 -0700 Subject: [PATCH] Initial commit. new file: action.yml new file: run.sh --- action.yml | 20 ++++++++++++++++++++ run.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 action.yml create mode 100755 run.sh diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..18182ca --- /dev/null +++ b/action.yml @@ -0,0 +1,20 @@ +name: 'Cached APT Install' +description: 'Install APT based packages and cache them for future runs.' +inputs: + packages: + description: 'Space delimited list of packages to install.' + required: true + default: '' +runs: + using: "composite" + steps: + - name: Cache Packages + uses: actions/cache@v2 + id: cached-apt-install-packages + with: + path: "~/cached-apt-install-packages" + key: cached-apt-install-packages + + - name: Install Packages + run: ${{ github.action_path }}/install.sh "~/cached-apt-install-packages" "${{ inputs.packages }}" + shell: bash diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..5942963 --- /dev/null +++ b/run.sh @@ -0,0 +1,51 @@ +#!/bin/bash -x + +cache_dir=$1 +packages="${@:2}" + +if [ ! -d "$cache_dir" ]; then + echo "Cache directory '$cache_dir' does not exist." + exit 1 +fi +if [ $packages = "" ]; then + echo "Packages argument cannot be empty." + exit 2 +fi + +for dir in `ls $cache_dir`; do + remove=true + for package in $packages; do + if [ $dir == $package ]; then + remove=false + break + fi + done + [ $remove ] && rm -fr $cache_dir/$dir +done + +for package in $packages; do + + package_dir=$cache_dir/$package + + if [ -d $package_dir ]; then + + echo "Restoring $package from cache $package_dir..." + sudo cp --verbose --force --recursive $package_dir/* / + sudo apt-get --yes --only-upgrade install $package + + else + + echo "Clean install $package and caching to $package_dir..." + sudo apt-get --yes install $package + + echo "Caching $package to $package_dir..." + mkdir --parents $package_dir + # Pipe all package files (no folders) to copy command. + sudo dpkg -L $package | + while IFS= read -r f; do + if test -f $f; then echo $f; fi; + done | + xargs cp -p -t $package_dir + fi + +done