From 0a97226723f589f94c4e7a66f9d0b36e0359b038 Mon Sep 17 00:00:00 2001 From: Nelson Araujo Date: Sat, 28 Dec 2024 00:22:05 -0800 Subject: [PATCH 1/2] Allow restoring directories --- backends/cache_fs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backends/cache_fs b/backends/cache_fs index 6fd472b..75fbe76 100755 --- a/backends/cache_fs +++ b/backends/cache_fs @@ -22,7 +22,11 @@ restore_cache() { # shellcheck disable=2064 # actually want variable interpolated here trap "release_lock_folder '${from}'" SIGINT SIGTERM SIGQUIT - cp -a "$from" "$to" + if [ -d "$from" ]; then + cp -a "${from}/." "$to" # copy as folder + else + cp -a "$from" "$to" + fi release_lock "${from}" } From c16e461df2f0f744cd9b94a9755da45ad7898d93 Mon Sep 17 00:00:00 2001 From: Nelson Araujo Date: Sat, 28 Dec 2024 01:26:25 -0800 Subject: [PATCH 2/2] Allow selecting 'rsync' as copy tool --- backends/cache_fs | 13 +++++++++++-- hooks/post-checkout | 10 ++++++++-- lib/plugin.bash | 13 +++++++++++++ plugin.yml | 5 +++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/backends/cache_fs b/backends/cache_fs index 75fbe76..363b7d2 100755 --- a/backends/cache_fs +++ b/backends/cache_fs @@ -17,15 +17,24 @@ DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" restore_cache() { local from="${CACHE_FOLDER}/$1" local to="$2" + local fs_copy_tool="$3" wait_and_lock "${from}" # shellcheck disable=2064 # actually want variable interpolated here trap "release_lock_folder '${from}'" SIGINT SIGTERM SIGQUIT if [ -d "$from" ]; then - cp -a "${from}/." "$to" # copy as folder + if [ "$fs_copy_tool" = "rsync" ]; then + rsync -a --delete "${from}/." "$to" # copy as folder + else + cp -a "${from}/." "$to" # copy as folder + fi else - cp -a "$from" "$to" + if [ "$fs_copy_tool" = "rsync" ]; then + rsync -a --delete "$from" "$to" + else + cp -a "$from" "$to" + fi fi release_lock "${from}" diff --git a/hooks/post-checkout b/hooks/post-checkout index a98fcfb..d7b6a95 100755 --- a/hooks/post-checkout +++ b/hooks/post-checkout @@ -30,6 +30,12 @@ if ! validate_compression "${COMPRESS}"; then exit 1 fi +FS_COPY_TOOL=$(plugin_read_config FS_COPY_TOOL 'cp') +if ! validate_fs_copy_tool "${FS_COPY_TOOL}"; then + echo "+++ 🚨 Invalid value for fs_copy_tool option" + exit 1 +fi + build_key "${MAX_LEVEL}" "${RESTORE_PATH}" >/dev/null # to validate the level SORTED_LEVELS=(file step branch pipeline all) @@ -45,10 +51,10 @@ for CURRENT_LEVEL in "${SORTED_LEVELS[@]}"; do if compression_active; then TEMP_PATH=$(mktemp) - backend_exec get "${KEY}" "${TEMP_PATH}" + backend_exec get "${KEY}" "${TEMP_PATH}" "cp" uncompress "${TEMP_PATH}" "${RESTORE_PATH}" else - backend_exec get "${KEY}" "${RESTORE_PATH}" + backend_exec get "${KEY}" "${RESTORE_PATH}" "${FS_COPY_TOOL}" fi exit 0 diff --git a/lib/plugin.bash b/lib/plugin.bash index 70676c6..43af7f1 100644 --- a/lib/plugin.bash +++ b/lib/plugin.bash @@ -59,4 +59,17 @@ function plugin_read_config() { local var="BUILDKITE_PLUGIN_${PLUGIN_PREFIX}_${1}" local default="${2:-}" echo "${!var:-$default}" +} + +function validate_fs_copy_tool() { + local FS_COPY_TOOL="$1" + + VALID_FS_COPY_TOOL=(cp rsync) + for VALID in "${VALID_FS_COPY_TOOL[@]}"; do + if [ "${FS_COPY_TOOL}" = "${VALID}" ]; then + return 0 + fi + done + + return 1 } \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index 500b494..67943ce 100644 --- a/plugin.yml +++ b/plugin.yml @@ -35,6 +35,11 @@ configuration: - type: array items: $ref: '#/$defs/valid_levels' + fs_copy_tool: + type: string + enum: + - cp + - rsync additionalProperties: false anyOf: - required: [ path, save ]