Skip to content

Commit f72c138

Browse files
authored
Merge pull request #62 from buildkite-plugins/toote_s3_backend_sync
s3 backend sync
2 parents 5662a64 + 6294732 commit f72c138

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

backends/cache_s3

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ build_key() {
1313
fi
1414
}
1515

16-
s3_sync() {
16+
s3_cmd() {
1717
local from="$1"
1818
local to="$2"
19+
local use_sync="${3:-1}"
1920

20-
aws_cmd=(aws s3 sync)
21+
aws_cmd=(aws s3)
22+
23+
if [ "${use_sync}" = 'true' ]; then
24+
aws_cmd+=(sync)
25+
else
26+
aws_cmd+=(cp --recursive)
27+
fi
2128

2229
if [ -n "${BUILDKITE_PLUGIN_S3_CACHE_ONLY_SHOW_ERRORS}" ]; then
2330
aws_cmd+=(--only-show-errors)
@@ -46,13 +53,20 @@ s3_listobjects() {
4653
restore_cache() {
4754
local from="$1"
4855
local to="$2"
49-
s3_sync "s3://${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}/$(build_key "${from}")" "${to}"
56+
57+
# can not use sync as it may be a single file
58+
s3_cmd "s3://${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}/$(build_key "${from}")" "${to}" 'false'
5059
}
5160

5261
save_cache() {
5362
local to="$1"
5463
local from="$2"
55-
s3_sync "${from}" "s3://${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}/$(build_key "${to}")"
64+
local use_sync='true'
65+
if [ -f "${from}" ]; then
66+
use_sync='false'
67+
fi
68+
69+
s3_cmd "${from}" "s3://${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}/$(build_key "${to}")"
5670
}
5771

5872
exists_cache() {

tests/cache_s3.bats

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ setup() {
6161
@test 'Verbose flag passed when environment is set' {
6262
export BUILDKITE_PLUGIN_S3_CACHE_ONLY_SHOW_ERRORS=1
6363
stub aws \
64-
's3 sync --only-show-errors \* \* : echo ' \
65-
's3 sync --only-show-errors \* \* : echo ' \
66-
's3 sync \* \* : echo ' \
67-
's3 sync \* \* : echo '
64+
's3 cp --recursive --only-show-errors \* \* : echo ' \
65+
's3 cp --recursive --only-show-errors \* \* : echo ' \
66+
's3 cp --recursive \* \* : echo ' \
67+
's3 cp --recursive \* \* : echo '
6868

6969
run "${PWD}/backends/cache_s3" save from to
7070

71-
assert_success
72-
assert_output ''
71+
assert_success
72+
assert_output ''
7373

7474
run "${PWD}/backends/cache_s3" get from to
7575

@@ -95,11 +95,11 @@ setup() {
9595
export BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT=https://s3.somewhere.com
9696

9797
stub aws \
98-
's3 sync --endpoint-url https://s3.somewhere.com \* \* : echo ' \
99-
's3 sync --endpoint-url https://s3.somewhere.com \* \* : echo ' \
100-
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents --endpoint-url https://s3.somewhere.com : echo exists' \
101-
's3 sync \* \* : echo ' \
102-
's3 sync \* \* : echo ' \
98+
's3 cp --recursive --endpoint-url https://s3.somewhere.com \* \* : echo ' \
99+
's3 cp --recursive --endpoint-url https://s3.somewhere.com \* \* : echo ' \
100+
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents --endpoint-url https://s3.somewhere.com : echo exists' \
101+
's3 cp --recursive \* \* : echo ' \
102+
's3 cp --recursive \* \* : echo ' \
103103
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents : echo exists'
104104

105105
run "${PWD}/backends/cache_s3" save from to
@@ -142,9 +142,9 @@ setup() {
142142
mkdir "${BATS_TEST_TMPDIR}/s3-cache"
143143
stub aws \
144144
"echo null" \
145-
"ln -s \$3 $BATS_TEST_TMPDIR/s3-cache/\$(echo \$4 | md5sum | cut -c-32)" \
145+
"ln -s \$4 $BATS_TEST_TMPDIR/s3-cache/\$(echo \$5 | md5sum | cut -c-32)" \
146146
"echo 'exists'" \
147-
"cp -r $BATS_TEST_TMPDIR/s3-cache/\$(echo \$3 | md5sum | cut -c-32) \$4"
147+
"cp -r $BATS_TEST_TMPDIR/s3-cache/\$(echo \$4 | md5sum | cut -c-32) \$5"
148148

149149
run "${PWD}/backends/cache_s3" exists new-file
150150

@@ -180,9 +180,9 @@ setup() {
180180

181181
stub aws \
182182
"echo null" \
183-
"ln -s \$3 $BATS_TEST_TMPDIR/s3-cache/\$(echo \$4 | md5sum | cut -c-32)" \
183+
"ln -s \$4 $BATS_TEST_TMPDIR/s3-cache/\$(echo \$5 | md5sum | cut -c-32)" \
184184
"echo 'exists'" \
185-
"cp -r $BATS_TEST_TMPDIR/s3-cache/\$(echo \$3 | md5sum | cut -c-32) \$4"
185+
"cp -r $BATS_TEST_TMPDIR/s3-cache/\$(echo \$4 | md5sum | cut -c-32) \$5"
186186

187187
run "${PWD}/backends/cache_s3" exists new-folder
188188

0 commit comments

Comments
 (0)