Skip to content

Commit 4fab8ba

Browse files
authored
Merge pull request #118 from buildkite-plugins/toote_cleanup
Some cleanup
2 parents 466369b + c8a5ef4 commit 4fab8ba

5 files changed

Lines changed: 51 additions & 17 deletions

File tree

hooks/post-command

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ for LEVEL in "${SAVE_LEVELS[@]}"; do
6262
echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}"
6363
if compression_active && [ "${already_compressed:-false}" = 'false' ]; then
6464
ACTUAL_PATH=$(mktemp)
65+
66+
# TERM in case it is cancelled
67+
# EXIT when the script ends
68+
# shellcheck disable=SC2064 # we want the expansion to happen now
69+
trap "cleanup_compression_tempfile '${ACTUAL_PATH}'" TERM EXIT
70+
6571
compress "${CACHE_PATH}" "${ACTUAL_PATH}"
6672
already_compressed='true' # avoid re-compressing the files
6773
fi
@@ -71,10 +77,3 @@ for LEVEL in "${SAVE_LEVELS[@]}"; do
7177
echo "Cache of ${LEVEL} already exists, skipping"
7278
fi
7379
done
74-
75-
# remove temporary compressed artifacts
76-
if [ "$(plugin_read_config KEEP_COMPRESSED_ARTIFACTS 'false')" = 'false' ]; then
77-
if compression_active && [ "${already_compressed:-false}" == 'true' ] && [ -e "${ACTUAL_PATH}" ]; then
78-
rm -rf "${ACTUAL_PATH}"
79-
fi
80-
fi

hooks/pre-command

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@ for CURRENT_LEVEL in "${SORTED_LEVELS[@]}"; do
4141

4242
if compression_active; then
4343
TEMP_PATH=$(mktemp)
44+
45+
# TERM in case it is cancelled
46+
# EXIT when the script ends
47+
# shellcheck disable=SC2064 # we want the expansion to happen now
48+
trap "cleanup_compression_tempfile '${TEMP_PATH}'" TERM EXIT
49+
4450
backend_exec get "${KEY}" "${TEMP_PATH}"
4551
decompress "${TEMP_PATH}" "${RESTORE_PATH}"
46-
if [ "$(plugin_read_config KEEP_COMPRESSED_ARTIFACTS 'false')" = 'false' ]; then
47-
rm -rf "${TEMP_PATH}"
48-
fi
4952
else
5053
backend_exec get "${KEY}" "${RESTORE_PATH}"
5154
fi
5255

5356
exit 0
5457
elif [ "${CURRENT_LEVEL}" = "${MAX_LEVEL}" ]; then
55-
echo "Cache miss up to ${CURRENT_LEVEL}-level, sorry"
58+
echo "Cache miss up to ${CURRENT_LEVEL}-level for ${RESTORE_PATH}, sorry"
5659
break
5760
fi
5861
done

lib/compression.bash

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ decompress() {
3737
PATH="${PATH}:${DIR}/../compression" "${COMPRESSION}_wrapper" "decompress" "${FILE}" "${RESTORE_PATH}"
3838
fi
3939
}
40+
41+
cleanup_compression_tempfile() {
42+
FILE="$1"
43+
44+
if [ "$(plugin_read_config KEEP_COMPRESSED_ARTIFACTS 'false')" = 'false' ]; then
45+
rm -rf "${FILE}"
46+
fi
47+
}

tests/compression.bats

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,27 @@ setup() {
8181

8282
unstub dummy_decompress_wrapper
8383
}
84+
85+
@test 'cleanup_compression_tempfile works' {
86+
touch temp_file
87+
88+
# by default it removes the file
89+
run cleanup_compression_tempfile temp_file
90+
assert_success
91+
assert [ ! -e temp_file ]
92+
93+
# if we want to keep the file, it should not be removed
94+
export BUILDKITE_PLUGIN_CACHE_KEEP_COMPRESSED_ARTIFACTS=true
95+
touch temp_file
96+
97+
run cleanup_compression_tempfile temp_file
98+
assert_success
99+
assert [ -e temp_file ]
100+
101+
# but we can also ask specifically to have it removed
102+
export BUILDKITE_PLUGIN_CACHE_KEEP_COMPRESSED_ARTIFACTS=false
103+
104+
run cleanup_compression_tempfile temp_file
105+
assert_success
106+
assert [ ! -e temp_file ]
107+
}

tests/pre-command.bats

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ teardown() {
6969
run "$PWD/hooks/pre-command"
7070

7171
assert_success
72-
assert_output --partial 'Cache miss up to file-level, sorry'
72+
assert_output --partial 'Cache miss up to file-level'
7373

7474
unstub cache_dummy
7575
}
@@ -84,7 +84,7 @@ teardown() {
8484
run "$PWD/hooks/pre-command"
8585

8686
assert_success
87-
assert_output --partial 'Cache miss up to step-level, sorry'
87+
assert_output --partial 'Cache miss up to step-level'
8888

8989
unstub cache_dummy
9090
}
@@ -99,7 +99,7 @@ teardown() {
9999
run "$PWD/hooks/pre-command"
100100

101101
assert_success
102-
assert_output --partial 'Cache miss up to step-level, sorry'
102+
assert_output --partial 'Cache miss up to step-level'
103103

104104
unstub cache_dummy
105105
}
@@ -115,7 +115,7 @@ teardown() {
115115
run "$PWD/hooks/pre-command"
116116

117117
assert_success
118-
assert_output --partial 'Cache miss up to branch-level, sorry'
118+
assert_output --partial 'Cache miss up to branch-level'
119119

120120
unstub cache_dummy
121121
}
@@ -131,7 +131,7 @@ teardown() {
131131
run "$PWD/hooks/pre-command"
132132

133133
assert_success
134-
assert_output --partial 'Cache miss up to pipeline-level, sorry'
134+
assert_output --partial 'Cache miss up to pipeline-level'
135135

136136
unstub cache_dummy
137137
}
@@ -149,7 +149,7 @@ teardown() {
149149
run "$PWD/hooks/pre-command"
150150

151151
assert_success
152-
assert_output --partial 'Cache miss up to all-level, sorry'
152+
assert_output --partial 'Cache miss up to all-level'
153153

154154
unstub cache_dummy
155155
}

0 commit comments

Comments
 (0)