From 49b4515f5120731306a7348beb612113ef06b3ea Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 3 Jun 2022 21:45:00 -0700 Subject: [PATCH 01/12] Fix arg ordinals and package sort return. --- lib.sh | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) mode change 100644 => 100755 lib.sh diff --git a/lib.sh b/lib.sh old mode 100644 new mode 100755 index 3802483..063492a --- a/lib.sh +++ b/lib.sh @@ -1,23 +1,19 @@ -#!/bin/bash +#!/bin/bash -x # Sort these packages by name and split on commas. function normalize_package_list { - stripped="$(echo \"${0}\" | sed 's/,//g')" + stripped="$(echo \"${1}\" | sed 's/,//g')" # Remove extraneous spaces at the middle, beginning, and end. trimmed="$(echo \"${stripped}\" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" - echo "$(\"${trimmed}\" | sort)" + echo "${trimmed}" | sort } # Split fully qualified package into name and version function get_package_name_ver { - IFS=\= read name ver <<< "${0}" + IFS=\= read name ver <<< "${1}" # If version not found in the fully qualified package value. if test -z "${ver}"; then ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')" fi echo 'package_name="${name}"; package_ver="${ver}"' } - -function blah { - > /dev/null 2>&1 -} \ No newline at end of file From 4d55f8a4fb2ddd13371e80d5a218b93f87493054 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 3 Jun 2022 21:51:44 -0700 Subject: [PATCH 02/12] Fix literal quotes and make variables use best practice. --- lib.sh | 4 ++-- pre_cache_action.sh | 22 +++++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib.sh b/lib.sh index 063492a..9e52157 100755 --- a/lib.sh +++ b/lib.sh @@ -2,9 +2,9 @@ # Sort these packages by name and split on commas. function normalize_package_list { - stripped="$(echo \"${1}\" | sed 's/,//g')" + stripped="$(echo ${1} | sed 's/,//g')" # Remove extraneous spaces at the middle, beginning, and end. - trimmed="$(echo \"${stripped}\" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" + trimmed="$(echo ${stripped} | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" echo "${trimmed}" | sort } diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 7c03802..70f3f87 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -5,10 +5,10 @@ script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh" # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # Version of the cache to create or load. -version=$2 +cache_version="${2}" # List of the packages to use. input_packages="${@:3}" @@ -17,12 +17,12 @@ input_packages="${@:3}" packages="$(normalize_package_list "${input_packages}")" # Create cache directory so artifacts can be saved. -mkdir -p $cache_dir +mkdir -p "${cache_dir}" -echo -n "Validating action arguments (version='$version', packages='$packages')..."; +echo -n "Validating action arguments (version='${cache_version}', packages='${packages}')..."; if grep -q " " <<< "${cache_version}"; then echo "aborted." - echo "Version value '$version' cannot contain spaces." >&2 + echo "Version value '${cache_version}' cannot contain spaces." >&2 exit 1 fi @@ -34,12 +34,16 @@ if test -z "${packages}"; then fi echo "done." +echo -n "Updating APT package list..." +sudo apt-get update > /dev/null +echo "done." + versioned_packages="" echo -n "Verifying packages..." for package in ${packages}; do if test ! "$(apt show "${package}")"; then echo "aborted." - echo "Package '$package' not found." >&2 + echo "Package '${package}' not found." >&2 exit 3 fi get_package_name_ver "${package}" # -> package_name, package_ver @@ -64,6 +68,6 @@ 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" +key_filepath="${cache_dir}/cache_key.md5" +echo "${key}" > "${key_filepath}" +echo "Hash value written to ${key_filepath}" From c21469a518328262c15cf717ef2ce3fddf522048 Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Thu, 14 Jul 2022 21:23:42 -0700 Subject: [PATCH 03/12] Update installation to consider symlinks as well. Addresses problem raised in #25 --- install_and_cache_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index bbf54dc..4de889d 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -33,7 +33,7 @@ for package in $packages; do # Pipe all package files (no folders) to Tar. dpkg -L $package | while IFS= read -r f; do - if test -f $f; then echo ${f:1}; fi; #${f:1} removes the leading slash that Tar disallows + if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows done | xargs tar -czf $cache_filepath -C / echo "done." @@ -48,4 +48,4 @@ for package in $packages; do done # Remove trailing comma. echo ${manifest:0:-1} > $manifest_filepath -echo "done." \ No newline at end of file +echo "done." From cda771820dacfe61a44891f26fc5f7445ad08f76 Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Fri, 15 Jul 2022 12:55:37 -0700 Subject: [PATCH 04/12] Update to v3 cache action. --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index e717de5..3ef2f6a 100644 --- a/action.yml +++ b/action.yml @@ -42,7 +42,7 @@ runs: shell: bash - id: load-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/cache-apt-pkgs key: cache-apt-pkgs_${{ env.CACHE_KEY }} From d5b9449cd38fc926b605bde918a404c876fc66c4 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 15:38:51 -0700 Subject: [PATCH 05/12] Merge script include snippet. --- install_and_cache_pkgs.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 467f980..2c22fb2 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -3,8 +3,12 @@ # Fail on any error. set -e +# Include library. +script_dir="$(dirname -- "$(realpath -- "${0}")")" +source "${script_dir}/lib.sh" + # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # List of the packages to use. input_packages="${@:2}" @@ -81,4 +85,3 @@ log "Writing main requested packages manifest to ${manifest_main_filepath}..." # Remove trailing comma and write to manifest_main file. echo "${manifest_main:0:-1}" > "${manifest_main_filepath}" log "done." - From 053557ecd1faef084fb18e80f6847038dd0f5925 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 16:11:14 -0700 Subject: [PATCH 06/12] Fix all packages reporting. --- install_and_cache_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 2c22fb2..79389dd 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -39,8 +39,8 @@ for package in ${normalized_packages}; do # Comma delimited name:ver pairs in the main requested packages manifest. manifest_main="${manifest_main}${package_name}:${package_ver}," - all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" - dep_packages="$(echo ${all_packages} | grep -v "${package_name}" | tr '\n' ,)" + all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':'))" + dep_packages="$(echo "${all_packages}" | grep -v "${package_name}" | tr '\n' ,)" if "${dep_packages}" == ","; then dep_packages="none"; fi From f20c69935b0e8fe6274e57c6ab57a93ceb2c44fa Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 16:14:30 -0700 Subject: [PATCH 07/12] Minor bug. --- install_and_cache_pkgs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 79389dd..61f08a1 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -39,7 +39,7 @@ for package in ${normalized_packages}; do # Comma delimited name:ver pairs in the main requested packages manifest. manifest_main="${manifest_main}${package_name}:${package_ver}," - all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':'))" + all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':')" dep_packages="$(echo "${all_packages}" | grep -v "${package_name}" | tr '\n' ,)" if "${dep_packages}" == ","; then dep_packages="none"; From 0735dbdc2248bbd7190763af7f5ef88bbb8db3f9 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Tue, 19 Jul 2022 19:53:44 -0700 Subject: [PATCH 08/12] Update package gathering. --- install_and_cache_pkgs.sh | 17 +++++++++-------- lib.sh | 10 +++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 61f08a1..1d420b2 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -39,21 +39,22 @@ for package in ${normalized_packages}; do # Comma delimited name:ver pairs in the main requested packages manifest. manifest_main="${manifest_main}${package_name}:${package_ver}," - all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':')" - dep_packages="$(echo "${all_packages}" | grep -v "${package_name}" | tr '\n' ,)" - if "${dep_packages}" == ","; then - dep_packages="none"; + read dep_packages < <(get_dep_packages "${package_name}") + if test -z "${dep_packages}"; then + dep_packages_text="none"; + else + dep_packages_text="${dep_packages}" fi log "- ${package_name}" log " * Version: ${package_ver}" - log " * Dependencies: ${dep_packages}" + log " * Dependencies: ${dep_packages_text}" log " * Installing..." # Zero interaction while installing or upgrading the system via apt. - sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null + sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package_name}" > /dev/null echo "done." - for cache_package in ${all_packages}; do + for cache_package in ${package_name}:${package_ver} ${dep_packages}; do cache_filepath="${cache_dir}/${cache_package}.tar.gz" if test ! -f "${cache_filepath}"; then @@ -65,7 +66,7 @@ for package in ${normalized_packages}; do if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows done | xargs tar -czf "${cache_filepath}" -C / - log "done (compressed size $(du -k "${cache_filepath}" | cut -f1))." + log "done (compressed size $(du -h "${cache_filepath}" | cut -f1))." fi # Comma delimited name:ver pairs in the all packages manifest. diff --git a/lib.sh b/lib.sh index f3757c9..fc8eb1e 100755 --- a/lib.sh +++ b/lib.sh @@ -9,9 +9,17 @@ function normalize_package_list { echo "${sorted}" } +# Gets a package list of dependencies as common delimited pairs +# :,... +function get_dep_packages { + echo $(apt-get install --dry-run --yes "${1}" | \ + grep "^Inst" | sort | awk '{print $2 $3}' | \ + tr '(' ':' | grep -v "${1}:") +} + # Split fully qualified package into name and version function get_package_name_ver { - IFS=\= read name ver <<< "${1}" + IFS=\: read name ver <<< "${1}" # If version not found in the fully qualified package value. if test -z "${ver}"; then ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')" From b61cf9a6f1e90144e5ba75bcac0ced6aef1e6446 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Tue, 19 Jul 2022 20:42:48 -0700 Subject: [PATCH 09/12] Copy from staging to dev. --- .github/workflows/pub_dev_push_event.yml | 19 +++++ .github/workflows/pub_master_push_event.yml | 4 +- README.md | 4 +- action.yml | 11 ++- install_and_cache_pkgs.sh | 81 ++++++++++++--------- lib.sh | 30 ++++++-- post_cache_action.sh | 16 ++-- pre_cache_action.sh | 46 ++++++------ restore_pkgs.sh | 40 +++++----- 9 files changed, 154 insertions(+), 97 deletions(-) create mode 100644 .github/workflows/pub_dev_push_event.yml diff --git a/.github/workflows/pub_dev_push_event.yml b/.github/workflows/pub_dev_push_event.yml new file mode 100644 index 0000000..3600112 --- /dev/null +++ b/.github/workflows/pub_dev_push_event.yml @@ -0,0 +1,19 @@ +name: Publish Dev Push Event +on: + workflow_dispatch: + push: + branches: + - dev + +jobs: + publish_event: + runs-on: ubuntu-latest + name: Publish dev push + steps: + - run: | + curl -i \ + -X POST \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${{ secrets.PUBLISH_PUSH_TOKEN }}" \ + https://api.github.com/repos/awalsh128/cache-apt-pkgs-action-ci/dispatches \ + -d '{"event_type":"dev_push"}' diff --git a/.github/workflows/pub_master_push_event.yml b/.github/workflows/pub_master_push_event.yml index b026e65..0339a05 100644 --- a/.github/workflows/pub_master_push_event.yml +++ b/.github/workflows/pub_master_push_event.yml @@ -8,7 +8,7 @@ on: jobs: publish_event: runs-on: ubuntu-latest - name: Publish staging push + name: Publish master push steps: - run: | curl -i \ @@ -16,4 +16,4 @@ jobs: -H "Accept: application/vnd.github.v3+json" \ -H "Authorization: token ${{ secrets.PUBLISH_PUSH_TOKEN }}" \ https://api.github.com/repos/awalsh128/cache-apt-pkgs-action-ci/dispatches \ - -d '{"event_type":"master_push"}' \ No newline at end of file + -d '{"event_type":"master_push"}' diff --git a/README.md b/README.md index 927e93a..972ce8a 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,8 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory ### Outputs * `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. \:,\:\,...). - +* `package-version-list` - The main requested packages and versions that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. \:,\:\,...). +* `all-package-version-list` - All the pulled in packages and versions, including dependencies, that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. \:,\:\,...). ### Cache scopes diff --git a/action.yml b/action.yml index e717de5..ee46a33 100644 --- a/action.yml +++ b/action.yml @@ -26,7 +26,10 @@ outputs: # Need to output true and false instead of true and nothing. value: ${{ steps.load-cache.outputs.cache-hit || false }} 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. ::).' + description: 'The main requested packages and versions that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. ::).' + value: ${{ steps.post-cache.outputs.package-version-list }} + all-package-version-list: + description: 'All the pulled in packages and versions, including dependencies, that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. ::).' value: ${{ steps.post-cache.outputs.package-version-list }} runs: @@ -42,7 +45,7 @@ runs: shell: bash - id: load-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/cache-apt-pkgs key: cache-apt-pkgs_${{ env.CACHE_KEY }} @@ -54,5 +57,7 @@ runs: / \ "${{ steps.load-cache.outputs.cache-hit }}" \ ${{ inputs.packages }} - echo "::set-output name=package-version-list::$(cat ~/cache-apt-pkgs/manifest.log)" + function create_list { local list=$(cat ~/cache-apt-pkgs/manifest_${1}.log | tr '\n' ','); echo ${list:0:-1}; }; + echo "::set-output name=package-version-list::$(create_list main)" + echo "::set-output name=all-package-version-list::$(create_list all)" shell: bash diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index e4f6c2c..1049d9b 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -8,61 +8,72 @@ script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh" # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # List of the packages to use. input_packages="${@:2}" # Trim commas, excess spaces, and sort. -packages="$(normalize_package_list "${input_packages}")" +normalized_packages="$(normalize_package_list "${input_packages}")" -package_count=$(echo "${packages}" | wc -w) -echo "Clean installing and caching ${package_count} package(s)." -echo "Package list:" -for package in "${packages}"; do - echo "- ${package}" +package_count=$(wc -w <<< "${normalized_packages}") +log "Clean installing and caching ${package_count} package(s)." +log "Package list:" +for package in ${normalized_packages}; do + log "- ${package}" done -echo -n "Updating APT package list..." +log "Updating APT package list..." sudo apt-get update > /dev/null echo "done." -manifest="" -echo "Clean installing and caching ${package_count} packages..." -for package in "${packages}"; do - get_package_name_ver "${package}" # -> package_name, package_ver - package_deps="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" +# Strictly contains the requested packages. +manifest_main="" +# Contains all packages including dependencies. +manifest_all="" - echo "- ${package_name}" - echo " * Version: ${package_ver}" - echo " * Dependencies: ${package_deps}" - echo -n " * Installing..." +log "Clean installing and caching ${package_count} packages..." +for package in ${normalized_packages}; do + read package_name package_ver < <(get_package_name_ver "${package}") + + # Comma delimited name:ver pairs in the main requested packages manifest. + manifest_main="${manifest_main}${package_name}:${package_ver}," + + read dep_packages < <(get_dep_packages "${package_name}") + if test -z "${dep_packages}"; then + dep_packages_text="none"; + else + dep_packages_text="${dep_packages}" + fi + + log "- ${package_name}" + log " * Version: ${package_ver}" + log " * Dependencies: ${dep_packages_text}" + log " * Installing..." # Zero interaction while installing or upgrading the system via apt. - sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null + sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package_name}" > /dev/null echo "done." - for cache_package in "${package_deps}"; do + for cache_package in ${package_name}:${package_ver} ${dep_packages}; do cache_filepath="${cache_dir}/${cache_package}.tar.gz" if test ! -f "${cache_filepath}"; then - get_package_name_ver "${cache_package}" # -> package_name, package_ver - echo -n " Caching ${package_name} to ${cache_filepath}..." + read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}") + log " * Caching ${cache_package_name} to ${cache_filepath}..." # Pipe all package files (no folders) to Tar. - dpkg -L "${package_name}" | + dpkg -L "${cache_package_name}" | while IFS= read -r f; do - if test -f $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows + if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows done | - xargs tar -czf "${cache_filepath}" -C / - echo "done." - # Add package to manifest - manifest="${manifest}${package_name}:$(dpkg -s "${package_name}" | grep Version | awk '{print $2}')," + xargs tar -czf "${cache_filepath}" -C / + log "done (compressed size $(du -h "${cache_filepath}" | cut -f1))." fi - done -done -echo "done." -manifest_filepath="${cache_dir}/manifest.log" -echo -n "Writing package manifest to ${manifest_filepath}..." -# Remove trailing comma and write to manifest file. -echo "${manifest:0:-1}" > "${manifest_filepath}" -echo "done." \ No newline at end of file + # Comma delimited name:ver pairs in the all packages manifest. + manifest_all="${manifest_all}${cache_package_name}:${cache_package_ver}," + done +done +log "done." + +write_manifest "all" "${manifest_all}" "${cache_dir}/manifest_all.log" +write_manifest "main" "${manifest_main}" "${cache_dir}/manifest_main.log" diff --git a/lib.sh b/lib.sh index 9e52157..74255ca 100755 --- a/lib.sh +++ b/lib.sh @@ -1,19 +1,37 @@ -#!/bin/bash -x +#!/bin/bash # Sort these packages by name and split on commas. function normalize_package_list { - stripped="$(echo ${1} | sed 's/,//g')" + local stripped=$(echo "${1}" | sed 's/,//g') # Remove extraneous spaces at the middle, beginning, and end. - trimmed="$(echo ${stripped} | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" - echo "${trimmed}" | sort + local trimmed="$(echo "${stripped}" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" + local sorted="$(echo ${trimmed} | tr ' ' '\n' | sort | tr '\n' ' ')" + echo "${sorted}" +} + +# Gets a package list of dependencies as common delimited pairs +# :,... +function get_dep_packages { + echo $(apt-get install --dry-run --yes "${1}" | \ + grep "^Inst" | sort | awk '{print $2 $3}' | \ + tr '(' ':' | grep -v "${1}:") } # Split fully qualified package into name and version function get_package_name_ver { - IFS=\= read name ver <<< "${1}" + IFS=\: read name ver <<< "${1}" # If version not found in the fully qualified package value. if test -z "${ver}"; then ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')" fi - echo 'package_name="${name}"; package_ver="${ver}"' + echo "${name}" "${ver}" } + +function log { echo "$(date +%H:%M:%S)" "${@}"; } + +function write_manifest { + log "Writing ${1} packages manifest to ${3}..." + # 0:-1 to remove trailing comma, delimit by newline and sort + echo "${2:0:-1}" | tr ',' '\n' | sort > ${3} + log "done." +} \ No newline at end of file diff --git a/post_cache_action.sh b/post_cache_action.sh index 5503013..144fe76 100755 --- a/post_cache_action.sh +++ b/post_cache_action.sh @@ -3,24 +3,28 @@ # Fail on any error. set -e +# Include library. +script_dir="$(dirname -- "$(realpath -- "${0}")")" +source "${script_dir}/lib.sh" + # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # Root directory to untar the cached packages to. # Typically filesystem root '/' but can be changed for testing. -cache_restore_root=$2 +cache_restore_root="${2}" # Indicates that the cache was found. -cache_hit=$3 +cache_hit="${3}" # List of the packages to use. packages="${@:4}" -script_dir=$(dirname $0) +script_dir="$(dirname -- "$(realpath -- "${0}")")" if [ "$cache_hit" == true ]; then - $script_dir/restore_pkgs.sh ~/cache-apt-pkgs $cache_restore_root + ${script_dir}/restore_pkgs.sh ~/cache-apt-pkgs "${cache_restore_root}" else - $script_dir/install_and_cache_pkgs.sh ~/cache-apt-pkgs $packages + ${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${packages} fi echo "" diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 70f3f87..7921adb 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -8,7 +8,7 @@ source "${script_dir}/lib.sh" cache_dir="${1}" # Version of the cache to create or load. -cache_version="${2}" +version="${2}" # List of the packages to use. input_packages="${@:3}" @@ -17,57 +17,53 @@ input_packages="${@:3}" packages="$(normalize_package_list "${input_packages}")" # Create cache directory so artifacts can be saved. -mkdir -p "${cache_dir}" +mkdir -p ${cache_dir} -echo -n "Validating action arguments (version='${cache_version}', packages='${packages}')..."; -if grep -q " " <<< "${cache_version}"; then - echo "aborted." - echo "Version value '${cache_version}' cannot contain spaces." >&2 +log -n "Validating action arguments (version='${version}', packages='${packages}')..."; +if grep -q " " <<< "${version}"; then + log "aborted." + log "Version value '${version}' cannot contain spaces." >&2 exit 1 fi # Is length of string zero? if test -z "${packages}"; then - echo "aborted." - echo "Packages argument cannot be empty." >&2 + log "aborted." + log "Packages argument cannot be empty." >&2 exit 2 fi -echo "done." - -echo -n "Updating APT package list..." -sudo apt-get update > /dev/null -echo "done." +log "done." versioned_packages="" -echo -n "Verifying packages..." +log -n "Verifying packages..." for package in ${packages}; do if test ! "$(apt show "${package}")"; then echo "aborted." - echo "Package '${package}' not found." >&2 + log "Package '${package}' not found." >&2 exit 3 fi - get_package_name_ver "${package}" # -> package_name, package_ver - versioned_packages="${versioned_packages} ${package_name}=${package_ver}" + read package_name package_ver < <(get_package_name_ver "${package}") + versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}"" done echo "done." # Abort on any failure at this point. set -e -echo "Creating cache key..." +log "Creating cache key..." # TODO Can we prove this will happen again? normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")" -echo "- Normalized package list is '${normalized_versioned_packages}'." +log "- Normalized package list is '${normalized_versioned_packages}'." -value="$(echo "${normalized_versioned_packages} @ ${cache_version}")" -echo "- Value to hash is '${value}'." +value="${normalized_versioned_packages} @ ${version}" +log "- Value to hash is '${value}'." key="$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')" -echo "- Value hashed as '${key}'." +log "- Value hashed as '${key}'." -echo "done." +log "done." key_filepath="${cache_dir}/cache_key.md5" -echo "${key}" > "${key_filepath}" -echo "Hash value written to ${key_filepath}" +echo ${key} > ${key_filepath} +log "Hash value written to ${key_filepath}" diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 4c710f3..ec6c475 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -3,32 +3,36 @@ # Fail on any error. set -e +# Include library. +script_dir="$(dirname -- "$(realpath -- "${0}")")" +source "${script_dir}/lib.sh" + # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # Root directory to untar the cached packages to. # Typically filesystem root '/' but can be changed for testing. -cache_restore_root=$2 +cache_restore_root="${2}" -cache_filepaths=$(ls -1 $cache_dir | sort) -echo "Found $(echo $cache_filepaths | wc -w) files in the cache." -for cache_filepath in $cache_filepaths; do - echo "- $(basename $cache_filepath)" +cache_filepaths="$(ls -1 "${cache_dir}" | sort)" +log "Found $(echo ${cache_filepaths} | wc -w) files in the cache." +for cache_filepath in ${cache_filepaths}; do + log "- "$(basename ${cache_filepath})"" done -echo "Reading from manifest..." -for logline in "$(cat "${cache_dir}/manifest.log" | tr ',' '\n' )"; do - echo "- $(echo "${logline}" | tr ':' ' ')" +log "Reading from main requested packages manifest..." +for logline in $(cat "${cache_dir}/manifest_main.log" | tr ',' '\n' ); do + log "- $(echo "${logline}" | tr ':' ' ')" done -echo "done." +log "done." # Only search for archived results. Manifest and cache key also live here. -cache_pkg_filepaths=$(ls -1 $cache_dir/*.tar.gz | sort) -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." +cache_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar.gz | sort) +cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w) +log "Restoring ${cache_pkg_filecount} packages from cache..." +for cache_pkg_filepath in ${cache_pkg_filepaths}; do + log "- $(basename "${cache_pkg_filepath}") restoring..." + sudo tar -xf "${cache_pkg_filepath}" -C "${cache_restore_root}" > /dev/null + log "done." done -echo "done." +log "done." From 1e75d90dc9568a8011586f603ee8df2e68c5f67a Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 15:38:51 -0700 Subject: [PATCH 10/12] Merge script include snippet. --- lib.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib.sh b/lib.sh index 74255ca..ba1a9e7 100755 --- a/lib.sh +++ b/lib.sh @@ -29,7 +29,8 @@ function get_package_name_ver { function log { echo "$(date +%H:%M:%S)" "${@}"; } -function write_manifest { +function write_manifest { + echo "manifest list ${1}" log "Writing ${1} packages manifest to ${3}..." # 0:-1 to remove trailing comma, delimit by newline and sort echo "${2:0:-1}" | tr ',' '\n' | sort > ${3} From 8af57b352e8ab23afadb98592ec4244fc98a296a Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 16:11:14 -0700 Subject: [PATCH 11/12] Fix all packages reporting. --- lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib.sh b/lib.sh index ba1a9e7..7c88180 100755 --- a/lib.sh +++ b/lib.sh @@ -30,7 +30,7 @@ function get_package_name_ver { function log { echo "$(date +%H:%M:%S)" "${@}"; } function write_manifest { - echo "manifest list ${1}" + echo "manifest list ${2}" log "Writing ${1} packages manifest to ${3}..." # 0:-1 to remove trailing comma, delimit by newline and sort echo "${2:0:-1}" | tr ',' '\n' | sort > ${3} From f1a79a837b87d847b1cc31a4a6f1fa6224dd664d Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 16:14:30 -0700 Subject: [PATCH 12/12] Minor bug. --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index ee46a33..9a5ce3d 100644 --- a/action.yml +++ b/action.yml @@ -30,7 +30,7 @@ outputs: value: ${{ steps.post-cache.outputs.package-version-list }} all-package-version-list: description: 'All the pulled in packages and versions, including dependencies, that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. ::).' - value: ${{ steps.post-cache.outputs.package-version-list }} + value: ${{ steps.post-cache.outputs.all-package-version-list }} runs: using: "composite"