Skip to content

Commit c8bd9c3

Browse files
authored
Merge pull request #83 from opzkit/main
feat: support absolute caching path
2 parents a540f5d + e52461f commit c8bd9c3

File tree

7 files changed

+131
-22
lines changed

7 files changed

+131
-22
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ steps:
1313
- label: ':nodejs: Install dependencies'
1414
command: npm ci
1515
plugins:
16-
- cache#v1.1.0:
16+
- cache#v1.3.0:
1717
manifest: package-lock.json
1818
path: node_modules
1919
restore: file
@@ -131,7 +131,7 @@ steps:
131131
- label: ':nodejs: Install dependencies'
132132
command: npm ci
133133
plugins:
134-
- cache#v1.1.0:
134+
- cache#v1.3.0:
135135
manifest: package-lock.json
136136
path: node_modules
137137
restore: pipeline
@@ -142,7 +142,7 @@ steps:
142142
- label: ':test_tube: Run tests'
143143
command: npm test # does not save cache, not necessary
144144
plugins:
145-
- cache#v1.1.0:
145+
- cache#v1.3.0:
146146
manifest: package-lock.json
147147
path: node_modules
148148
restore: file
@@ -151,7 +151,7 @@ steps:
151151
if: build.branch == "master"
152152
command: npm run deploy
153153
plugins:
154-
- cache#v1.1.0:
154+
- cache#v1.3.0:
155155
manifest: package-lock.json
156156
path: node_modules
157157
restore: file

hooks/post-checkout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
set -euo pipefail
33

4-
DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
4+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55

66
# shellcheck source=lib/shared.bash
77
. "${DIR}/../lib/shared.bash"
@@ -10,7 +10,7 @@ DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
1010

1111
RESTORE_PATH=$(plugin_read_config PATH)
1212

13-
if [ -z "${RESTORE_PATH}" ] ; then
13+
if [ -z "${RESTORE_PATH}" ]; then
1414
echo "+++ 🚨 Missing path option in the cache plugin to restore"
1515
exit 1
1616
fi

lib/compression.bash

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
3+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
44

55
# shellcheck source=lib/plugin.bash
66
. "${DIR}/plugin.bash"
@@ -35,28 +35,68 @@ compress() {
3535
echo "Compressing ${COMPRESSED_FILE} with ${COMPRESSION}..."
3636

3737
if [ "${COMPRESSION}" = 'tgz' ]; then
38-
tar czf "${FILE}" "${COMPRESSED_FILE}"
38+
TAR_OPTS='cz'
39+
if is_absolute_path "${COMPRESSED_FILE}"; then
40+
TAR_OPTS="${TAR_OPTS}"P
41+
fi
42+
43+
tar "${TAR_OPTS}"f "${FILE}" "${COMPRESSED_FILE}"
3944
elif [ "${COMPRESSION}" = 'zip' ]; then
40-
# because ZIP complains if the file does not end with .zip
41-
zip -r "${FILE}.zip" "${COMPRESSED_FILE}"
42-
mv "${FILE}.zip" "${FILE}"
45+
if is_absolute_path "${COMPRESSED_FILE}"; then
46+
local COMPRESS_DIR
47+
COMPRESS_DIR="$(dirname "${COMPRESSED_FILE}")"
48+
( # subshell to avoid changing the working directory
49+
# shellcheck disable=SC2164 # we will exit anyway
50+
cd "${COMPRESS_DIR}"
51+
# because ZIP complains if the file does not end with .zip
52+
zip -r "${FILE}.zip" "${COMPRESSED_FILE}"
53+
mv "${FILE}.zip" "${FILE}"
54+
)
55+
else
56+
# because ZIP complains if the file does not end with .zip
57+
zip -r "${FILE}.zip" "${COMPRESSED_FILE}"
58+
mv "${FILE}.zip" "${FILE}"
59+
fi
4360
fi
4461
}
4562

4663
uncompress() {
4764
local FILE="$1"
48-
local _RESTORE_PATH="$2" # pretty sure this is not necessary
65+
local RESTORE_PATH="$2"
4966

5067
local COMPRESSION=''
5168
COMPRESSION="$(plugin_read_config COMPRESSION 'none')"
5269

5370
echo "Cache is compressed, uncompressing with ${COMPRESSION}..."
5471

5572
if [ "${COMPRESSION}" = 'tgz' ]; then
56-
tar xzf "${FILE}"
73+
TAR_OPTS='xz'
74+
if is_absolute_path "${RESTORE_PATH}"; then
75+
TAR_OPTS="${TAR_OPTS}"P
76+
fi
77+
78+
tar "${TAR_OPTS}"f "${FILE}" "${RESTORE_PATH}"
5779
elif [ "${COMPRESSION}" = 'zip' ]; then
58-
# because ZIP complains if the file does not end with .zip
59-
mv "${FILE}" "${FILE}.zip"
60-
unzip -o "${FILE}.zip"
80+
if is_absolute_path "${RESTORE_PATH}"; then
81+
local RESTORE_DIR
82+
RESTORE_DIR="$(dirname "${RESTORE_PATH}")"
83+
( # subshell to avoid changing the working directory
84+
mkdir -p "${RESTORE_DIR}"
85+
# shellcheck disable=SC2164 # we will exit anyway
86+
cd "${RESTORE_DIR}"
87+
mv "${FILE}" "${RESTORE_DIR}/compressed.zip"
88+
unzip -o "compressed.zip"
89+
rm "compressed.zip"
90+
)
91+
else
92+
# because ZIP complains if the file does not end with .zip
93+
mv "${FILE}" "${FILE}.zip"
94+
unzip -o "${FILE}.zip"
95+
fi
6196
fi
6297
}
98+
99+
is_absolute_path() {
100+
local FILEPATH="${1}"
101+
[ "${FILEPATH:0:1}" = "/" ]
102+
}

tests/post-checkout-tgz.bats

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ setup() {
2424

2525
# stub is the same for all tests
2626
stub tar \
27-
"xzf \* \* : echo uncompressed \$2 into \$3"
27+
"\* \* \* : echo uncompressed \$2 into \$3 with options \$1"
28+
2829
}
2930

3031
teardown() {
@@ -45,6 +46,7 @@ teardown() {
4546
assert_success
4647
assert_output --partial 'Cache hit at file level'
4748
assert_output --partial 'Cache is compressed, uncompressing with tgz'
49+
assert_output --partial "with options xzf"
4850

4951
unstub cache_dummy
5052
}
@@ -155,3 +157,21 @@ teardown() {
155157

156158
unstub cache_dummy
157159
}
160+
161+
@test 'Existing file-based restore to absolute path' {
162+
export BUILDKITE_PLUGIN_CACHE_RESTORE=file
163+
export BUILDKITE_PLUGIN_CACHE_PATH=/tmp/tests/data/my_files
164+
165+
stub cache_dummy \
166+
'exists \* : exit 0' \
167+
"get \* \* : echo restoring \$2 to \$3"
168+
169+
run "$PWD/hooks/post-checkout"
170+
171+
assert_success
172+
assert_output --partial 'Cache hit at file level'
173+
assert_output --partial 'Cache is compressed, uncompressing with tgz...'
174+
assert_output --partial "with options xzPf"
175+
176+
unstub cache_dummy
177+
}

tests/post-checkout-zip.bats

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ setup() {
2424

2525
# stub is the same for all tests
2626
stub unzip \
27-
"-o \* \* : echo uncompressed \$3 into \$4"
27+
"-o \* \* : echo uncompressed \$2 into \$3"
2828
}
2929

3030
teardown() {
@@ -138,7 +138,6 @@ teardown() {
138138
unstub cache_dummy
139139
}
140140

141-
142141
@test 'Existing lower level restore works' {
143142
export BUILDKITE_PLUGIN_CACHE_RESTORE=all
144143

@@ -156,3 +155,21 @@ teardown() {
156155

157156
unstub cache_dummy
158157
}
158+
159+
@test 'Existing file-based restore to absolute path' {
160+
export BUILDKITE_PLUGIN_CACHE_RESTORE=all
161+
export BUILDKITE_PLUGIN_CACHE_PATH=/tmp/tests/data/my_files
162+
163+
# Need to create a file here to be able to move it to restore path later were it will be removed
164+
stub cache_dummy \
165+
'exists \* : exit 0' \
166+
"get \* \* : mkdir -p $(dirname \$3) && touch \$3 && echo restoring \$2 to \$3"
167+
168+
run "$PWD/hooks/post-checkout"
169+
170+
assert_success
171+
assert_output --partial 'Cache hit at file level'
172+
assert_output --partial "Cache is compressed, uncompressing with zip"
173+
174+
unstub cache_dummy
175+
}

tests/post-command-tgz.bats

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ setup() {
2727
export BUILDKITE_PIPELINE_SLUG="cache-pipeline"
2828

2929
# stubs are the same for every test
30+
stub tar \
31+
"\* \* \* : echo compressed \$2 into \$3 with options \$1"
32+
3033
stub cache_dummy \
3134
"save \* \* : echo saving \$3 in \$2"
32-
33-
stub tar \
34-
"czf \* \* : echo compressed \$2 into \$3"
3535
}
3636

3737
teardown() {
@@ -50,6 +50,7 @@ teardown() {
5050
assert_success
5151
assert_output --partial 'Saving file-level cache'
5252
assert_output --partial 'Compressing tests/data/my_files with tgz'
53+
assert_output --partial "with options czf"
5354
}
5455

5556
@test "Step-level saving" {
@@ -106,3 +107,19 @@ teardown() {
106107
assert_output --partial 'Saving all-level cache'
107108
assert_output --partial 'Saving pipeline-level cache'
108109
}
110+
111+
112+
@test 'Pipeline-level saving with absolute cache path' {
113+
BUILDKITE_PLUGIN_CACHE_PATH="$(mktemp -d)"
114+
export BUILDKITE_PLUGIN_CACHE_PATH
115+
export BUILDKITE_PLUGIN_CACHE_SAVE=pipeline
116+
117+
run "$PWD/hooks/post-command"
118+
119+
assert_success
120+
assert_output --partial 'Saving pipeline-level cache'
121+
assert_output --partial "Compressing ${BUILDKITE_PLUGIN_CACHE_PATH} with tgz..."
122+
assert_output --partial "with options czPf"
123+
124+
rm -rf "${BUILDKITE_PLUGIN_CACHE_PATH}"
125+
}

tests/post-command-zip.bats

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,18 @@ teardown() {
107107
assert_output --partial 'Saving all-level cache'
108108
assert_output --partial 'Saving pipeline-level cache'
109109
}
110+
111+
@test 'Pipeline-level saving with absolute cache path' {
112+
BUILDKITE_PLUGIN_CACHE_PATH="$(mktemp -d)"
113+
export BUILDKITE_PLUGIN_CACHE_PATH
114+
export BUILDKITE_PLUGIN_CACHE_SAVE=pipeline
115+
116+
run "$PWD/hooks/post-command"
117+
118+
assert_success
119+
assert_output --partial "Compressing ${BUILDKITE_PLUGIN_CACHE_PATH} with zip..."
120+
assert_output --partial 'Saving pipeline-level cache'
121+
122+
rm -rf "${BUILDKITE_PLUGIN_CACHE_PATH}"
123+
}
124+

0 commit comments

Comments
 (0)